warc_extractor.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. #!/usr/bin/env python3
  2. """
  3. warc-extractor, a simple command line tool for expanding warc files.
  4. Copyright (C) 2014 Ryan Chartier
  5. Portions (C) 2012 Internet Archive
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. warc.utils
  17. ~~~~~~~~~~
  18. This file is part of warc
  19. :copyright: (c) 2012 Internet Archive
  20. warc.warc
  21. ~~~~~~~~~
  22. Python library to work with WARC files.
  23. :copyright: (c) 2012 Internet Archive
  24. """
  25. from collections.abc import MutableMapping
  26. from http.client import HTTPMessage
  27. from urllib.parse import urlparse, unquote
  28. from pprint import pprint
  29. import os
  30. import argparse
  31. import mimetypes
  32. import email.parser
  33. import gzip
  34. import datetime
  35. import uuid
  36. import re
  37. import io
  38. import hashlib
  39. # ---------------------------------------------------
  40. # warc.utils -
  41. # ---------------------------------------------------
  42. SEP = re.compile("[;:=]")
  43. class CaseInsensitiveDict(MutableMapping):
  44. """Almost like a dictionary, but keys are case-insensitive.
  45. >>> d = CaseInsensitiveDict(foo=1, Bar=2)
  46. >>> d['foo']
  47. 1
  48. >>> d['bar']
  49. 2
  50. >>> d['Foo'] = 11
  51. >>> d['FOO']
  52. 11
  53. >>> d.keys()
  54. ["foo", "bar"]
  55. """
  56. def __init__(self, *args, **kwargs):
  57. self._d = {}
  58. self.update(dict(*args, **kwargs))
  59. def __setitem__(self, name, value):
  60. self._d[name.lower()] = value
  61. def __getitem__(self, name):
  62. return self._d[name.lower()]
  63. def __delitem__(self, name):
  64. del self._d[name.lower()]
  65. def __eq__(self, other):
  66. return isinstance(other, CaseInsensitiveDict) and other._d == self._d
  67. def __iter__(self):
  68. return iter(self._d)
  69. def __len__(self):
  70. return len(self._d)
  71. class FilePart:
  72. """File interface over a part of file.
  73. Takes a file and length to read from the file and returns a file-object
  74. over that part of the file.
  75. """
  76. def __init__(self, fileobj, length):
  77. self.fileobj = fileobj
  78. self.length = length
  79. self.offset = 0
  80. self.buf = b''
  81. def read(self, size=-1):
  82. if size == -1:
  83. size = self.length
  84. if len(self.buf) >= size:
  85. content = self.buf[:size]
  86. self.buf = self.buf[size:]
  87. else:
  88. size = min(size, self.length - self.offset)
  89. content = self.buf + self.fileobj.read(size - len(self.buf))
  90. self.buf = b''
  91. self.offset += len(content)
  92. return content
  93. def unread(self, content):
  94. self.buf = content + self.buf
  95. self.offset -= len(content)
  96. def readline(self, size=1024):
  97. chunks = []
  98. chunk = self.read(size)
  99. while chunk and b"\n" not in chunk:
  100. chunks.append(chunk)
  101. chunk = self.read(size)
  102. if b"\n" in chunk:
  103. index = chunk.index(b"\n")
  104. self.unread(chunk[index + 1:])
  105. chunk = chunk[:index + 1]
  106. chunks.append(chunk)
  107. return b"".join(chunks)
  108. def __iter__(self):
  109. line = self.readline()
  110. while line:
  111. yield line
  112. line = self.readline()
  113. class HTTPObject(CaseInsensitiveDict):
  114. """Small object to help with parsing HTTP warc entries"""
  115. def __init__(self, request_file):
  116. # Parse version line
  117. id_str_raw = request_file.readline()
  118. id_str = id_str_raw.decode("iso-8859-1")
  119. if "HTTP" not in id_str:
  120. # This is not an HTTP object.
  121. request_file.unread(id_str_raw)
  122. raise ValueError("Object is not HTTP.")
  123. words = id_str.split()
  124. command = path = status = error = version = None
  125. # If length is not 3 it is a bad version line.
  126. if len(words) >= 3:
  127. if words[1].isdigit():
  128. version = words[0]
  129. error = words[1]
  130. status = " ".join(words[2:])
  131. else:
  132. command, path, version = words
  133. self._id = {
  134. "vline": id_str_raw,
  135. "command": command,
  136. "path": path,
  137. "status": status,
  138. "error": error,
  139. "version": version,
  140. }
  141. self._header, self.hstring = self._parse_headers(request_file)
  142. super().__init__(self._header)
  143. self.payload = request_file
  144. self._content = None
  145. @staticmethod
  146. def _parse_headers(fp):
  147. """This is a modification of the python3 http.clint.parse_headers function."""
  148. headers = []
  149. while True:
  150. line = fp.readline(65536)
  151. headers.append(line)
  152. if line in (b'\r\n', b'\n', b''):
  153. break
  154. hstring = b''.join(headers)
  155. return email.parser.Parser(_class=HTTPMessage).parsestr(hstring.decode('iso-8859-1')), hstring
  156. def __repr__(self):
  157. return self.vline + str(self._header)
  158. def __getitem__(self, name):
  159. try:
  160. return super().__getitem__(name)
  161. except KeyError:
  162. value = name.lower()
  163. if value == "content_type":
  164. return self.content.type
  165. elif value in self.content:
  166. return self.content[value]
  167. elif value in self._id:
  168. return self._id[value]
  169. else:
  170. raise
  171. def reset(self):
  172. self.payload.unread(self.hstring)
  173. self.payload.unread(self._id['vline'])
  174. def write_to(self, f):
  175. f.write(self._id['vline'])
  176. f.write(self.hstring)
  177. f.write(self.payload.read())
  178. f.write(b"\r\n\r\n")
  179. f.flush()
  180. @property
  181. def content(self):
  182. if self._content is None:
  183. try:
  184. string = self._d["content-type"]
  185. except KeyError:
  186. string = ''
  187. self._content = ContentType(string)
  188. return self._content
  189. @property
  190. def vline(self):
  191. return self._id["vline"].decode("iso-8859-1")
  192. @property
  193. def version(self):
  194. return self._id["version"]
  195. def write_payload_to(self, fp):
  196. encoding = self._header.get("Transfer-Encoding", "None")
  197. if encoding == "chunked":
  198. found = b''
  199. length = int(str(self.payload.readline(), "iso-8859-1").rstrip(), 16)
  200. while length > 0:
  201. found += self.payload.read(length)
  202. self.payload.readline()
  203. length = int(str(self.payload.readline(), "iso-8859-1").rstrip(), 16)
  204. else:
  205. length = int(self._header.get("Content-Length", -1))
  206. found = self.payload.read(length)
  207. fp.write(found)
  208. class ContentType(CaseInsensitiveDict):
  209. def __init__(self, string):
  210. data = {}
  211. self.type = ''
  212. if string:
  213. _list = [i.strip() for i in string.lower().split(";")]
  214. self.type = _list[0]
  215. data["type"] = _list[0]
  216. for i in _list[1:]:
  217. test = [n.strip() for n in re.split(SEP, i)]
  218. # It's only a property if it has two elements.
  219. if len(test) > 1:
  220. data[test[0]] = test[1]
  221. super().__init__(data)
  222. def __repr__(self):
  223. return self.type
  224. # ---------------------------------------------------
  225. # warc.warc -
  226. # ---------------------------------------------------
  227. class WARCHeader(CaseInsensitiveDict):
  228. """The WARC Header object represents the headers of a WARC record.
  229. It provides dictionary like interface for accessing the headers.
  230. The following mandatory fields are accessible also as attributes.
  231. * h.record_id == h['WARC-Record-ID']
  232. * h.content_length == int(h['Content-Length'])
  233. * h.date == h['WARC-Date']
  234. * h.type == h['WARC-Type']
  235. :params headers: dictionary of headers.
  236. :params defaults: If True, important headers like WARC-Record-ID,
  237. WARC-Date, Content-Type and Content-Length are
  238. initialized to automatically if not already present.
  239. """
  240. CONTENT_TYPES = dict(warcinfo='application/warc-fields',
  241. response='application/http; msgtype=response',
  242. request='application/http; msgtype=request',
  243. metadata='application/warc-fields')
  244. KNOWN_HEADERS = {
  245. "type": "WARC-Type",
  246. "date": "WARC-Date",
  247. "record_id": "WARC-Record-ID",
  248. "ip_address": "WARC-IP-Address",
  249. "target_uri": "WARC-Target-URI",
  250. "warcinfo_id": "WARC-Warcinfo-ID",
  251. "request_uri": "WARC-Request-URI",
  252. "content_type": "Content-Type",
  253. "content_length": "Content-Length"
  254. }
  255. def __init__(self, headers, defaults=False):
  256. self.version = "WARC/1.0"
  257. super().__init__(headers)
  258. if defaults:
  259. self.init_defaults()
  260. def __repr__(self):
  261. return "<WARCHeader: type={}, record_id={}>".format(self.type, self.record_id)
  262. def init_defaults(self):
  263. """Initializes important headers to default values, if not already specified.
  264. The WARC-Record-ID header is set to a newly generated UUID.
  265. The WARC-Date header is set to the current datetime.
  266. The Content-Type is set based on the WARC-Type header.
  267. The Content-Length is initialized to 0.
  268. """
  269. if "WARC-Record-ID" not in self:
  270. self['WARC-Record-ID'] = "<urn:uuid:%s>" % uuid.uuid1()
  271. if "WARC-Date" not in self:
  272. self['WARC-Date'] = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
  273. if "Content-Type" not in self:
  274. self['Content-Type'] = WARCHeader.CONTENT_TYPES.get(self.type, "application/octet-stream")
  275. def write_to(self, f):
  276. """Writes this header to a file, in the format specified by WARC.
  277. """
  278. f.write(self.version.encode() + b"\r\n")
  279. for name, value in self.items():
  280. name = name.title()
  281. # Use standard forms for commonly used patterns
  282. name = name.replace("Warc-", "WARC-").replace("-Ip-", "-IP-").replace("-Id", "-ID").replace("-Uri", "-URI")
  283. entry = "{}: {}\r\n".format(str(name), str(value)).encode()
  284. f.write(entry)
  285. # Header ends with an extra CRLF
  286. f.write(b"\r\n")
  287. @property
  288. def content_length(self):
  289. """The Content-Length header as int."""
  290. return int(self['Content-Length'])
  291. @property
  292. def type(self):
  293. """The value of WARC-Type header."""
  294. return self['WARC-Type']
  295. @property
  296. def record_id(self):
  297. """The value of WARC-Record-ID header."""
  298. return self['WARC-Record-ID']
  299. @property
  300. def date(self):
  301. """The value of WARC-Date header."""
  302. return self['WARC-Date']
  303. class WARCRecord(object):
  304. """The WARCRecord object represents a WARC Record.
  305. """
  306. def __init__(self, header=None, payload=None, headers=None, defaults=True):
  307. """Creates a new WARC record.
  308. """
  309. if headers is None:
  310. headers = {}
  311. if header is None and defaults is True:
  312. headers.setdefault("WARC-Type", "response")
  313. self.header = header or WARCHeader(headers, defaults=True)
  314. if defaults is True and 'Content-Length' not in self.header:
  315. if payload:
  316. self.header['Content-Length'] = len(payload)
  317. else:
  318. self.header['Content-Length'] = "0"
  319. if defaults is True and 'WARC-Payload-Digest' not in self.header:
  320. self.header['WARC-Payload-Digest'] = self._compute_digest(payload)
  321. if isinstance(payload, str):
  322. payload = payload.encode()
  323. if isinstance(payload, bytes):
  324. payload = io.BytesIO(payload)
  325. self.payload = payload
  326. self._http = None
  327. self._content = None
  328. @staticmethod
  329. def _compute_digest(payload):
  330. return "sha1:" + hashlib.sha1(payload).hexdigest()
  331. def write_to(self, f):
  332. self.header.write_to(f)
  333. if self.http:
  334. self.http.reset()
  335. f.write(self.payload.read())
  336. f.write(b"\r\n")
  337. f.write(b"\r\n")
  338. f.flush()
  339. @property
  340. def content(self):
  341. if self._content is None:
  342. try:
  343. string = self.header["content-type"]
  344. except KeyError:
  345. string = ''
  346. self._content = ContentType(string)
  347. return self._content
  348. @property
  349. def http(self):
  350. if self._http is None:
  351. if 'application/http' in self.header['content-type']:
  352. self._http = HTTPObject(self.payload)
  353. else:
  354. self._http = False
  355. return self._http
  356. @property
  357. def type(self):
  358. """Record type"""
  359. return self.header.type
  360. @property
  361. def url(self):
  362. """The value of the WARC-Target-URI header if the record is of type "response"."""
  363. return self.header.get('WARC-Target-URI')
  364. @property
  365. def ip_address(self):
  366. """The IP address of the host contacted to retrieve the content of this record.
  367. This value is available from the WARC-IP-Address header."""
  368. return self.header.get('WARC-IP-Address')
  369. @property
  370. def date(self):
  371. """UTC timestamp of the record."""
  372. return self.header.get("WARC-Date")
  373. @property
  374. def checksum(self):
  375. return self.header.get('WARC-Payload-Digest')
  376. def __getitem__(self, name):
  377. try:
  378. return self.header[name]
  379. except KeyError:
  380. if name == "content_type":
  381. return self.content.type
  382. elif name in self.content:
  383. return self.content[name]
  384. def __setitem__(self, name, value):
  385. self.header[name] = value
  386. def __contains__(self, name):
  387. return name in self.header
  388. def __repr__(self):
  389. return "<WARCRecord: type=%r record_id=%s>" % (self.type, self['WARC-Record-ID'])
  390. @staticmethod
  391. def from_response(response):
  392. """Creates a WARCRecord from given response object.
  393. This must be called before reading the response. The response can be
  394. read after this method is called.
  395. :param response: An instance of :class:`requests.models.Response`.
  396. """
  397. # Get the httplib.HTTPResponse object
  398. http_response = response.raw._original_response
  399. # HTTP status line, headers and body as strings
  400. status_line = "HTTP/1.1 %d %s" % (http_response.status, http_response.reason)
  401. headers = str(http_response.msg)
  402. body = http_response.read()
  403. # Monkey-patch the response object so that it is possible to read from it later.
  404. response.raw._fp = io.BytesIO(body)
  405. # Build the payload to create warc file.
  406. payload = status_line + "\r\n" + headers + "\r\n" + body
  407. headers = {
  408. "WARC-Type": "response",
  409. "WARC-Target-URI": response.request.url.encode('utf-8')
  410. }
  411. return WARCRecord(payload=payload, headers=headers)
  412. class WARCFile:
  413. def __init__(self, filename=None, mode=None, fileobj=None, compress=None):
  414. if fileobj is None:
  415. fileobj = open(filename, mode or "rb")
  416. mode = fileobj.mode
  417. # initiaize compress based on filename, if not already specified
  418. if compress is None and filename and filename.endswith(".gz"):
  419. compress = True
  420. if compress:
  421. fileobj = gzip.open(fileobj, mode)
  422. self.fileobj = fileobj
  423. self._reader = None
  424. def __enter__(self):
  425. return self
  426. def __exit__(self, exc_type, exc_value, traceback):
  427. self.close()
  428. def __iter__(self):
  429. return iter(self.reader)
  430. @property
  431. def reader(self):
  432. if self._reader is None:
  433. self._reader = WARCReader(self.fileobj)
  434. return self._reader
  435. def write_record(self, warc_record):
  436. """Adds a warc record to this WARC file.
  437. """
  438. warc_record.write_to(self.fileobj)
  439. def read_record(self):
  440. """Reads a warc record from this WARC file."""
  441. return self.reader.read_record()
  442. def close(self):
  443. self.fileobj.close()
  444. def tell(self):
  445. """Returns the file offset.
  446. """
  447. return self.fileobj.tell()
  448. class WARCReader:
  449. RE_VERSION = re.compile(r"WARC/(\d+.\d+)\r\n")
  450. RE_HEADER = re.compile(r"([a-zA-Z_\-]+): *(.*)\r\n")
  451. SUPPORTED_VERSIONS = ["1.0"]
  452. def __init__(self, fileobj):
  453. self.fileobj = fileobj
  454. self.current_payload = None
  455. def read_header(self, fileobj):
  456. version_line = fileobj.readline().decode("utf-8")
  457. if not version_line:
  458. return None
  459. m = self.RE_VERSION.match(version_line)
  460. if not m:
  461. raise IOError("Bad version line: %r" % version_line)
  462. version = m.group(1)
  463. if version not in self.SUPPORTED_VERSIONS:
  464. raise IOError("Unsupported WARC version: %s" % version)
  465. headers = {}
  466. while True:
  467. line = fileobj.readline().decode("utf-8")
  468. if line == "\r\n": # end of headers
  469. break
  470. m = self.RE_HEADER.match(line)
  471. if not m:
  472. raise IOError("Bad header line: %r" % line)
  473. name, value = m.groups()
  474. headers[name] = value
  475. return WARCHeader(headers)
  476. @staticmethod
  477. def expect(fileobj, expected_line, message=None):
  478. line = fileobj.readline().decode("utf-8")
  479. if line != expected_line:
  480. message = message or "Expected %r, found %r" % (expected_line, line)
  481. raise IOError(message)
  482. def finish_reading_current_record(self):
  483. # consume the footer from the previous record
  484. if self.current_payload:
  485. # consume all data from the current_payload before moving to next record
  486. self.current_payload.read()
  487. self.expect(self.current_payload.fileobj, "\r\n")
  488. self.expect(self.current_payload.fileobj, "\r\n")
  489. self.current_payload = None
  490. def read_record(self):
  491. self.finish_reading_current_record()
  492. fileobj = self.fileobj
  493. header = self.read_header(fileobj)
  494. if header is None:
  495. return None
  496. self.current_payload = FilePart(fileobj, header.content_length)
  497. record = WARCRecord(header, self.current_payload, defaults=False)
  498. return record
  499. @staticmethod
  500. def _read_payload(fileobj, content_length):
  501. size = 0
  502. while size < content_length:
  503. chunk_size = min(1024, content_length - size)
  504. chunk = fileobj.read(chunk_size)
  505. size += chunk_size
  506. yield chunk
  507. def __iter__(self):
  508. record = self.read_record()
  509. while record is not None:
  510. yield record
  511. record = self.read_record()
  512. # ---------------------------------------------------
  513. # Extractor -
  514. # ---------------------------------------------------
  515. counts = {}
  516. class FilterObject:
  517. """Basic object for storing filters."""
  518. def __init__(self, string):
  519. self.result = True
  520. if string[0] == "!":
  521. self.result = False
  522. string = string[1:]
  523. _list = string.lower().split(":")
  524. self.http = (_list[0] == 'http')
  525. if self.http:
  526. del _list[0]
  527. self.k = _list[0]
  528. self.v = _list[1]
  529. def inc(obj, header=None, dic=None):
  530. """Short script for counting entries."""
  531. if header:
  532. try:
  533. obj = obj[header]
  534. except KeyError:
  535. obj = None
  536. holder = counts
  537. if dic:
  538. if dic not in counts:
  539. counts[dic] = {}
  540. holder = counts[dic]
  541. if obj in holder:
  542. holder[obj] += 1
  543. else:
  544. holder[obj] = 1
  545. def warc_records(string, path):
  546. """Iterates over warc records in path."""
  547. for filename in os.listdir(path):
  548. if re.search(string, filename) and ".warc" in filename:
  549. print("parsing", filename)
  550. with WARCFile(path + filename) as warc_file:
  551. for record in warc_file:
  552. yield record
  553. def check_filter(filters, record):
  554. """Check record against filters."""
  555. for i in filters:
  556. if i.http:
  557. if not record.http:
  558. return False
  559. value = record.http
  560. else:
  561. value = record.header
  562. string = value.get(i.k, None)
  563. if not string or (i.v in string) != i.result:
  564. return False
  565. return True
  566. def parse(args):
  567. # Clear output warc file.
  568. if args.dump == "warc":
  569. if args.silence:
  570. print("Recording", args.dump, "to", args.output + ".")
  571. with open(args.output_path + args.output, "wb"):
  572. pass
  573. for record in warc_records(args.string, args.path):
  574. try:
  575. # Filter out unwanted entries.
  576. if not check_filter(args.filter, record):
  577. continue
  578. # Increment Index counters.
  579. if args.silence:
  580. inc("records")
  581. inc(record, "warc-type", "types")
  582. inc(record, "content_type", "warc-content")
  583. if record.http:
  584. inc(record.http, "content_type", "http-content")
  585. inc(record.http, "error", "status")
  586. # Dump records to file.
  587. if args.dump == "warc":
  588. with open(args.output_path + args.output, "ab") as output:
  589. record.write_to(output)
  590. if args.dump == "content":
  591. url = urlparse(unquote(record['WARC-Target-URI']))
  592. # Set up folder
  593. index = url.path.rfind("/") + 1
  594. file = url.path[index:]
  595. path = url.path[:index]
  596. # Process filename
  597. if "." not in file:
  598. path += file
  599. if not path.endswith("/"):
  600. path += "/"
  601. file = 'index.html'
  602. # Final fixes.
  603. path = path.replace(".", "-")
  604. host = url.hostname.replace('www.', '', 1)
  605. path = args.output_path + host + path
  606. # Create new directories
  607. if not os.path.exists(path):
  608. try:
  609. os.makedirs(path)
  610. except OSError:
  611. path = "/".join([i[:25] for i in path.split("/")])
  612. os.makedirs(path)
  613. # Test if file has a proper extension.
  614. index = file.index(".")
  615. suffix = file[index:]
  616. content = record.http.get("content_type", "")
  617. slist = mimetypes.guess_all_extensions(content)
  618. if suffix not in slist:
  619. # Correct suffix if we can.
  620. suffix = mimetypes.guess_extension(content)
  621. if suffix:
  622. file = file[:index] + suffix
  623. else:
  624. inc(record.http, "content_type", "unknown mime type")
  625. # Check for gzip compression.
  626. if record.http.get("content-encoding", None) == "gzip":
  627. file += ".gz"
  628. path += file
  629. # If Duplicate file then insert numbers
  630. index = path.rfind(".")
  631. temp = path
  632. n = 0
  633. while os.path.isfile(temp):
  634. n += 1
  635. temp = path[:index] + "(" + str(n) + ")" + path[index:]
  636. path = temp
  637. # Write file.
  638. try:
  639. with open(path, 'wb') as fp:
  640. record.http.write_payload_to(fp)
  641. except OSError as e:
  642. print("unable to save file due to operating system error:", e)
  643. except Exception:
  644. if args.error:
  645. if args.silence:
  646. print("Error in record. Recording to error.warc.")
  647. with open(args.output_path + "error.warc", "ab") as fp:
  648. record.write_to(fp)
  649. else:
  650. raise
  651. # print results
  652. if args.silence:
  653. print("-----------------------------")
  654. for i in counts:
  655. print("\nCount of {}.".format(i))
  656. pprint(counts[i])
  657. def main():
  658. parser = argparse.ArgumentParser(description='Extracts attributes from warc files.')
  659. parser.add_argument("filter", nargs='*',
  660. help="Attributes to filter by. Entries that do not contain filtered elements are ignored. "
  661. "Example: warc-type:response, would ignore all warc entries that are not responses. "
  662. "Attributes in an HTTP object should be prefixed by 'http'. Example, http:error:200.")
  663. parser.add_argument("-silence", action="store_false", help="Silences output of warc data.")
  664. parser.add_argument("-error", action="store_true",
  665. help="Silences most errors and records problematic warc entries to error.warc.")
  666. parser.add_argument("-string", default="",
  667. help="Regular expression to limit parsed warc files. Defaults to empty string.")
  668. parser.add_argument("-path", default="./", help="Path to folder containing warc files. Defaults to current folder.")
  669. parser.add_argument("-output_path", default="data/",
  670. help="Path to folder to dump content files. Defaults to data/ folder.")
  671. parser.add_argument("-output", default="output.warc",
  672. help="File to output warc contents. Defaults to 'output.warc'.")
  673. parser.add_argument("-dump", choices=['warc', 'content'], type=str,
  674. help="Dumps all entries that survived filter. 'warc' creates a filtered warc file. "
  675. "'content' tries to reproduce file structure of archived websites.")
  676. args = parser.parse_args()
  677. if args.path[-1] != "/":
  678. args.path += "/"
  679. if args.output_path[-1] != "/":
  680. args.output_path += "/"
  681. if args.dump:
  682. if not os.path.exists(args.output_path):
  683. os.makedirs(args.output_path)
  684. # Forced filters
  685. filters = list(args.filter)
  686. if args.dump == "content":
  687. filters.append("warc-type:response")
  688. filters.append("content-type:application/http")
  689. args.filter = [FilterObject(i) for i in filters]
  690. args.string = re.compile(args.string)
  691. parse(args)
  692. if __name__ == "__main__":
  693. main()