This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
ExtractCCLinks.py
284 lines (214 loc) · 10.4 KB
/
ExtractCCLinks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#Common Crawl data extraction
"""Identify all links to Creative Commons in the web crawl data"""
from pyspark import SparkContext
from pyspark.sql import SQLContext
from pyspark.sql import SparkSession
from pyspark.sql.types import *
import boto3
import botocore
from botocore.handlers import disable_signing
import gzip
from urllib.parse import urlparse
import requests
from collections import Counter
import random
from warcio.archiveiterator import ArchiveIterator
import ujson as json
from io import StringIO
import sys
import re
import logging
from datetime import date, datetime, timedelta
class CCLinks:
logging.getLogger('ExtractCCLinks')
logging.basicConfig(format='%(asctime)s: [%(levelname)s - ExtractCCLinks] =======> %(message)s', level=logging.INFO)
def __init__(self, _index, _ptn=2500):
"""
CCLinks constructor: Validate the user-defined index based on Common Crawl's expected format.
If the pattern is valid, it generates 1) a url for the WAT path and 2) the location to output the results.
Parameters
------------------
_index: string
The common crawl index name
_ptn: integer
The number of partitions for the spark job
Returns
------------------
None
"""
self.crawlIndex = _index
#check index format
pattern = re.compile('CC-MAIN-\d{4}-\d{2}')
if not pattern.match(_index):
logging.error('Invalid common crawl index format => {}.'.format(_index))
sys.exit()
self.numPartitions = _ptn
self.url = 'https://commoncrawl.s3.amazonaws.com/crawl-data/{}/wat.paths.gz'.format(self.crawlIndex)
self.output = 'output/{}'.format(self.crawlIndex)
def loadWATFile(self):
#load the WAT file paths
"""
Make a request for a WAT file using the url, that was defined in the constructor.
Parameters
------------------
None
Returns
------------------
list
A list of WAT path locations.
"""
logging.info('Loading file {}'.format(self.url))
try:
response = requests.get(self.url)
if response.status_code == requests.codes.ok:
content = StringIO(response.content)
fh = gzip.GzipFile(fileobj=content)
watPaths = fh.read().split()
return watPaths
else:
raise Exception
except Exception as e:
logging.error('There was a problem loading the file.')
logging.error('{}: {}'.format(type(e).__name__, e))
#sys.exit()
def processFile(self, _iterator):
"""
Parse each WAT file to identify domains with a hyperlink to creativecommons.org.
Parameters
------------------
_iterator: iterator object
The iterator for the RDD partition that was assigned to the current process.
Returns
------------------
list
A list of domains and their respective content path and query string, the hyperlink to creative commons (which may reference a license), the location of the domain in the current warc file and a count of the number of links and images.
"""
logging.basicConfig(format='%(asctime)s: [%(levelname)s - ExtractCCLinks] =======> %(message)s', level=logging.INFO)
bucket = 'commoncrawl'
#connect to s3 using boto3
s3 = boto3.resource('s3')
#s3.meta.client.meta.events.register('choose-signer.s3.*', disable_signing)
try:
#verify bucket
s3.meta.client.head_bucket(Bucket=bucket)
except botocore.exceptions.ClientError as e:
error = int(e.response['Error']['Code'])
if error == 404:
logging.error('Bucket not found!')
sys.exit()
else:
#iterate over the keys and load the respective wat files
for uri in _iterator:
try:
#verify key
s3.Object(bucket, uri.strip()).load()
except botocore.client.ClientError as e:
logging.warning('{}: {}.'.format(uri.strip(), e.response['Error']['Message']))
pass
else:
try:
resp = requests.get('https://commoncrawl.s3.amazonaws.com/{}'.format(uri.strip()), stream=True)
except Exception as e:
#ConnectionError: HTTPSConnectionPool
logging.error('Exception type: {0}, Message: {1}'.format(type(e).__name__, e))
pass
else:
for record in ArchiveIterator(resp.raw, arc2warc=True):
if record.rec_headers['Content-Type'] == 'application/json':
try:
content = json.loads(record.content_stream().read())
except Exception as e:
logging.warning('JSON payload file: {0}. Exception type: {1}, Message: {2}'.format(uri.strip(), type(e).__name__, e))
pass
else:
if content['Envelope']['WARC-Header-Metadata']['WARC-Type'] != 'response':
continue
elif 'HTML-Metadata' not in content['Envelope']['Payload-Metadata']['HTTP-Response-Metadata']:
continue
elif 'Links' not in content['Envelope']['Payload-Metadata']['HTTP-Response-Metadata']['HTML-Metadata']:
continue
try:
segment = uri.split('/wat/')[0].strip()
targetURI = urlparse(content['Envelope']['WARC-Header-Metadata']['WARC-Target-URI'].strip())
offset = int(content['Container']['Offset'].strip())
filename = content['Container']['Filename'].strip()
dftLength = int(content['Container']['Gzip-Metadata']['Deflate-Length'].strip())
links = filter(lambda x: 'url' in x, content['Envelope']['Payload-Metadata']['HTTP-Response-Metadata']['HTML-Metadata']['Links'])
result = map(lambda x: (targetURI.netloc, targetURI.path, targetURI.query, urlparse(x['url']).netloc,
urlparse(x['url']).path, segment, filename, offset, dftLength,
json.dumps({
'Images': len(list(set(map(lambda i: i['url'], filter(lambda z: 'IMG@/src' in z['path'], links))))),
'Links': Counter(map(lambda l: urlparse(l['url']).netloc, filter(lambda z: 'A@/href' in z['path'] and targetURI.netloc not in z['url'] and urlparse(z['url']).netloc != '', links)))
})
),
filter(lambda y: 'creativecommons.org' in y['url'], links))
except (KeyError, ValueError) as e:
logging.error('{}:{}, File:{}'.format(type(e).__name__, e, uri.strip()))
pass
else:
if result:
for res in result:
yield res
def generateParquet(self, _data):
"""
Create a parquet file with the extracted content.
Parameters
------------------
_data: generator
A list containing the extracted domains and their associated meta-data.
Returns
------------------
None
"""
schema = StructType([
StructField('provider_domain', StringType(), True),
StructField('content_path', StringType(), True),
StructField('content_query_string', StringType(), True),
StructField('cc_domain', StringType(), True),
StructField('cc_license', StringType(), True),
StructField('warc_segment', StringType(), True),
StructField('warc_filename', StringType(), True),
StructField('content_offset', LongType(), True),
StructField('deflate_length', LongType(), True),
StructField('html_metadata', StringType(), True),
])
spk = SparkSession.builder.getOrCreate()
df = spk.createDataFrame(_data, schema=schema)
df.write.format('parquet').mode('overwrite').save(self.output)
def main():
args = sys.argv[1]
crawlIndex = args.strip()
if crawlIndex.lower() == '--default':
bucket = 'commoncrawl'
s3 = boto3.client('s3')
#verify bucket
contents = []
prefix = 'cc-index/collections/CC-MAIN-'
botoArgs = {'Bucket': bucket, 'Prefix': prefix}
while True:
objects = s3.list_objects_v2(**botoArgs)
for obj in objects['Contents']:
key = obj['Key']
if 'indexes' in key:
cIndex = key.split('/indexes/')[0].split('/')
cIndex = cIndex[len(cIndex)-1]
if str(cIndex) not in contents:
contents.append(str(cIndex))
try:
botoArgs['ContinuationToken'] = objects['NextContinuationToken']
except KeyError:
break
if contents:
crawlIndex = contents[-1]
sc = SparkContext(appName='ExtractCCLinks')
ccLinks = CCLinks(crawlIndex.upper(), sc.defaultParallelism)
watPaths = ccLinks.loadWATFile()
if watPaths is None:
sc.stop()
sys.exit()
watRDD = sc.parallelize(watPaths, ccLinks.numPartitions)
result = watRDD.mapPartitions(ccLinks.processFile)
ccLinks.generateParquet(result)
sc.stop()
if __name__ == '__main__':
main()