aboutsummaryrefslogtreecommitdiffstats
path: root/backfill
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@archive.org>2018-03-30 11:03:16 -0700
committerBryan Newbold <bnewbold@archive.org>2018-03-30 11:03:16 -0700
commit1ee37422c649aeb11a3f0bbe873b9c479992a5aa (patch)
treeaf1a6845e705c1bcb2b8e91ee354805d1dfa35fd /backfill
parentb67d08495cf8caa3fa52cf8f8f80389250685218 (diff)
downloadsandcrawler-1ee37422c649aeb11a3f0bbe873b9c479992a5aa.tar.gz
sandcrawler-1ee37422c649aeb11a3f0bbe873b9c479992a5aa.zip
renames
Diffstat (limited to 'backfill')
-rw-r--r--backfill/Pipfile21
-rw-r--r--backfill/Pipfile.lock75
-rwxr-xr-xbackfill/cdx_fulltext_to_hbase.py169
3 files changed, 265 insertions, 0 deletions
diff --git a/backfill/Pipfile b/backfill/Pipfile
new file mode 100644
index 0000000..83dc463
--- /dev/null
+++ b/backfill/Pipfile
@@ -0,0 +1,21 @@
+[[source]]
+
+url = "https://pypi.python.org/simple"
+verify_ssl = true
+name = "pypi"
+
+
+[dev-packages]
+
+
+
+[packages]
+
+happybase = "*"
+happybase-mock = "*"
+nose = "*"
+
+
+[requires]
+
+python_version = "3.5"
diff --git a/backfill/Pipfile.lock b/backfill/Pipfile.lock
new file mode 100644
index 0000000..b525871
--- /dev/null
+++ b/backfill/Pipfile.lock
@@ -0,0 +1,75 @@
+{
+ "_meta": {
+ "hash": {
+ "sha256": "ede8d6f8c246a58f84fffe98979d8c17f624535aba1e23aa0539308859f2ffa1"
+ },
+ "host-environment-markers": {
+ "implementation_name": "cpython",
+ "implementation_version": "3.5.3",
+ "os_name": "posix",
+ "platform_machine": "x86_64",
+ "platform_python_implementation": "CPython",
+ "platform_release": "4.9.0-6-amd64",
+ "platform_system": "Linux",
+ "platform_version": "#1 SMP Debian 4.9.82-1+deb9u3 (2018-03-02)",
+ "python_full_version": "3.5.3",
+ "python_version": "3.5",
+ "sys_platform": "linux"
+ },
+ "pipfile-spec": 6,
+ "requires": {
+ "python_version": "3.5"
+ },
+ "sources": [
+ {
+ "name": "pypi",
+ "url": "https://pypi.python.org/simple",
+ "verify_ssl": true
+ }
+ ]
+ },
+ "default": {
+ "happybase": {
+ "hashes": [
+ "sha256:e20376e2e32291798d2226502994134c1c4e175136d8375b3c517a234fa22481"
+ ],
+ "version": "==1.1.0"
+ },
+ "happybase-mock": {
+ "hashes": [
+ "sha256:327203ff63171a83c9fab34b249636b6a55550041273d2acddc0723433bdf260",
+ "sha256:bd4583551f40e8b7f622ffd462f8e7ed1d34d14d73fa1758f0a5f413b1949f50"
+ ],
+ "version": "==0.9.0"
+ },
+ "nose": {
+ "hashes": [
+ "sha256:dadcddc0aefbf99eea214e0f1232b94f2fa9bd98fa8353711dacb112bfcbbb2a",
+ "sha256:9ff7c6cc443f8c51994b34a667bbcf45afd6d945be7477b52e97516fd17c53ac",
+ "sha256:f1bffef9cbc82628f6e7d7b40d7e255aefaa1adb6a1b1d26c69a8b79e6208a98"
+ ],
+ "version": "==1.3.7"
+ },
+ "ply": {
+ "hashes": [
+ "sha256:096f9b8350b65ebd2fd1346b12452efe5b9607f7482813ffca50c22722a807ce",
+ "sha256:00c7c1aaa88358b9c765b6d3000c6eec0ba42abca5351b095321aef446081da3"
+ ],
+ "version": "==3.11"
+ },
+ "six": {
+ "hashes": [
+ "sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb",
+ "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9"
+ ],
+ "version": "==1.11.0"
+ },
+ "thriftpy": {
+ "hashes": [
+ "sha256:309e57d97b5bfa01601393ad4f245451e989d6206a59279e56866b264a99796d"
+ ],
+ "version": "==0.3.9"
+ }
+ },
+ "develop": {}
+}
diff --git a/backfill/cdx_fulltext_to_hbase.py b/backfill/cdx_fulltext_to_hbase.py
new file mode 100755
index 0000000..757794a
--- /dev/null
+++ b/backfill/cdx_fulltext_to_hbase.py
@@ -0,0 +1,169 @@
+#!/usr/bin/env python3
+"""
+Streaming Hadoop script to import CDX metadata into the HBase fulltext table,
+primarily for URL-agnostic crawl de-duplication. Takes only "fulltext" file
+formats.
+
+Requires:
+- happybase
+
+TODO:
+- argparse
+- refactor into an object
+- tests in separate file
+- nose tests
+- sentry integration for error reporting
+"""
+
+import sys
+import json
+import happybase
+
+NORMAL_MIME = (
+ 'application/pdf',
+ 'application/postscript',
+ 'text/html',
+ 'text/xml',
+ #'application/xml',
+)
+
+def normalize_mime(raw):
+ raw = raw.lower()
+ for norm in NORMAL_MIME:
+ if raw.startswith(norm):
+ return norm
+
+ # Special cases
+ if raw.startswith('application/xml'):
+ return 'text/xml'
+ if raw.startswith('application/x-pdf'):
+ return 'application/pdf'
+ return None
+
+def test_normalize_mime():
+ assert normalize_mime("asdf") == None
+ assert normalize_mime("application/pdf") == "application/pdf"
+ assert normalize_mime("application/pdf+journal") == "application/pdf"
+ assert normalize_mime("Application/PDF") == "application/pdf"
+ assert normalize_mime("application/p") == None
+ assert normalize_mime("application/xml+stuff") == "text/xml"
+
+def transform_line(raw_cdx):
+
+ cdx = raw_cdx.split()
+ if len(cdx) < 11:
+ return None
+
+ surt = cdx[0]
+ dt = cdx[1]
+ url = cdx[2]
+ mime = normalize_mime(cdx[3])
+ http_status = cdx[4]
+ if http_status != "200":
+ return None
+ key = cdx[5]
+ c_size = cdx[8]
+ offset = cdx[9]
+ warc = cdx[10]
+ info = dict(surt=surt, dt=dt, url=url, c_size=c_size, offset=offset,
+ warc=warc)
+ return {'key': key, 'file:mime': mime, 'file:cdx': info}
+
+def test_transform_line():
+
+ raw = "edu,upenn,ldc)/sites/www.ldc.upenn.edu/files/medar2009-large-arabic-broadcast-collection.pdf 20170828233154 https://www.ldc.upenn.edu/sites/www.ldc.upenn.edu/files/medar2009-large-arabic-broadcast-collection.pdf application/pdf 200 WL3FEA62TEU4F52Y5DOVQ62VET4QJW7G - - 210251 931661233 SEMSCHOLAR-PDF-CRAWL-2017-08-04-20170828231135742-00000-00009-wbgrp-svc284/SEMSCHOLAR-PDF-CRAWL-2017-08-04-20170828232253025-00005-3480~wbgrp-svc284.us.archive.org~8443.warc.gz"
+ correct = {
+ 'key': "WL3FEA62TEU4F52Y5DOVQ62VET4QJW7G",
+ 'file:mime': "application/pdf",
+ 'file:cdx': {
+ 'surt': "edu,upenn,ldc)/sites/www.ldc.upenn.edu/files/medar2009-large-arabic-broadcast-collection.pdf",
+ 'url': "https://www.ldc.upenn.edu/sites/www.ldc.upenn.edu/files/medar2009-large-arabic-broadcast-collection.pdf",
+ 'dt': "20170828233154",
+ 'warc': "SEMSCHOLAR-PDF-CRAWL-2017-08-04-20170828231135742-00000-00009-wbgrp-svc284/SEMSCHOLAR-PDF-CRAWL-2017-08-04-20170828232253025-00005-3480~wbgrp-svc284.us.archive.org~8443.warc.gz",
+ 'offset': "931661233",
+ 'c_size': "210251",
+ }
+ }
+
+ assert transform_line(raw) == correct
+ assert transform_line(raw + "\n") == correct
+ assert transform_line(raw + " extra_field") == correct
+
+
+def run(in_lines, out_lines, status_lines, table, mime_filter=None):
+
+ if mime_filter is None:
+ mime_filter = ['application/pdf']
+ count_skip = count_invalid = count_fail = count_success = 0
+
+ for raw_cdx in in_lines.readlines():
+ if (raw_cdx.startswith(' ') or raw_cdx.startswith('filedesc') or
+ raw_cdx.startswith('#')):
+ # Skip line
+ count_skip += 1
+ continue
+
+ info = transform_line(raw_cdx)
+ if info is None:
+ count_invalid += 1
+ continue
+ if info['file:mime'] not in mime_filter:
+ count_skip += 1
+ continue
+
+ key = info.pop('key')
+ info['file:cdx'] = json.dumps(info['file:cdx'], sort_keys=True,
+ indent=None)
+ try:
+ table.put(key, info)
+ count_success += 1
+ except:
+ status_lines.write("ERROR\n") # TODO:
+ count_fail += 1
+
+ status_lines.write('\n')
+ status_lines.write('skip\t{}\n'.format(count_skip))
+ status_lines.write('invalid\t{}\n'.format(count_invalid))
+ status_lines.write('fail\t{}\n'.format(count_fail))
+ status_lines.write('success\t{}\n'.format(count_success))
+
+
+def test_run():
+
+ import io
+ import happybase_mock
+
+ out = io.StringIO()
+ status = io.StringIO()
+ raw = io.StringIO("""
+com,sagepub,cep)/content/28/9/960.full.pdf 20170705062200 http://cep.sagepub.com/content/28/9/960.full.pdf application/pdf 301 3I42H3S6NNFQ2MSVX7XZKYAYSCX5QBYJ - - 401 313356621 CITESEERX-CRAWL-2017-06-20-20170705061647307-00039-00048-wbgrp-svc284/CITESEERX-CRAWL-2017-06-20-20170705062052659-00043-31209~wbgrp-svc284.us.archive.org~8443.warc.gz
+eu,eui,cadmus)/bitstream/handle/1814/36635/rscas_2015_03.pdf;jsessionid=761393014319a39f40d32ae3eb3a853f?sequence=1 20170705062202 http://cadmus.eui.eu/bitstream/handle/1814/36635/RSCAS_2015_03.pdf%3Bjsessionid%3D761393014319A39F40D32AE3EB3A853F?sequence%3D1 application/PDF 200 MPCXVWMUTRUGFP36SLPHKDLY6NGU4S3J - - 854156 328850624 CITESEERX-CRAWL-2017-06-20-20170705061647307-00039-00048-wbgrp-svc284/CITESEERX-CRAWL-2017-06-20-20170705062052659-00043-31209~wbgrp-svc284.us.archive.org~8443.warc.gz
+com,pbworks,educ333b)/robots.txt 20170705063311 http://educ333b.pbworks.com/robots.txt text/plain 200 6VAUYENMOU2SK2OWNRPDD6WTQTECGZAD - - 638 398190140 CITESEERX-CRAWL-2017-06-20-20170705062707827-00049-00058-wbgrp-svc284/CITESEERX-CRAWL-2017-06-20-20170705063158203-00053-31209~wbgrp-svc284.us.archive.org~8443.warc.gz
+""")
+
+ conn = happybase_mock.Connection()
+ conn.create_table('wbgrp-journal-extract-test', {'file': {}, 'grobid0': {}})
+
+ table = conn.table('wbgrp-journal-extract-test')
+ run(raw, out, status, table)
+
+ print(status.getvalue())
+
+ assert table.row(b'1') == {}
+ # HTTP 301
+ assert table.row(b'3I42H3S6NNFQ2MSVX7XZKYAYSCX5QBYJ') == {}
+ # valid
+ assert table.row(b'MPCXVWMUTRUGFP36SLPHKDLY6NGU4S3J') != {}
+ # text/plain
+ assert table.row(b'6VAUYENMOU2SK2OWNRPDD6WTQTECGZAD') == {}
+
+ row = table.row(b'MPCXVWMUTRUGFP36SLPHKDLY6NGU4S3J')
+ assert row[b'file:mime'] == b"application/pdf"
+ json.loads(row[b'file:cdx'].decode('utf-8'))
+
+if __name__=="__main__":
+ hb = happybase.Connection(host='')
+ with hb.connection() as conn:
+ table = conn.table('wbgrp-journal-extract-0-qa')
+ run(sys.stdin, sys.stdout, sys.stderr, table)
+