-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathmedia2warc.py
executable file
·283 lines (228 loc) · 8.32 KB
/
media2warc.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
#!/usr/bin/env python
"""
This utility extracts media urls from tweet jsonl.gz and save them as warc records.
Warcio (https://github.com/webrecorder/warcio) is a dependency and before you can use it you need to:
% pip install warcio
You run it like this:
% python media2warc.py /mnt/tweets/ferguson/tweets-0001.jsonl.gz /mnt/tweets/ferguson/tweets-0001.warc.gz
The input file will be checked for duplicate urls to avoid duplicates within the input file. Subsequent runs
will be deduplicated using a sqlite db. If an identical-payload-digest is found a revist record is created.
The script is able to fetch media resources in multiple threads (maximum 2) by passing --threads <int> (default to a single thread).
Please be careful modifying this script to use more than two threads since it can be interpreted as a DoS-attack.
"""
import os
import gzip
import json
import time
import queue
import hashlib
import logging
import sqlite3
import argparse
import requests
import threading
from datetime import timedelta
from warcio.warcwriter import WARCWriter
from warcio.statusandheaders import StatusAndHeaders
q = queue.Queue()
out_queue = queue.Queue()
BLOCK_SIZE = 25600
class GetResource(threading.Thread):
def __init__(self, q):
threading.Thread.__init__(self)
self.q = q
self.rlock = threading.Lock()
self.out_queue = out_queue
self.d = Dedup()
def run(self):
while True:
host = self.q.get()
try:
r = requests.get(
host, headers={"Accept-Encoding": "identity"}, stream=True
)
data = [r.raw.headers.items(), r.raw, host, r.status_code, r.reason]
print(data[2])
self.out_queue.put(data)
self.q.task_done()
except requests.exceptions.RequestException as e:
logging.error("%s for %s", e, data[2])
print(e)
self.q.task_done()
continue
class WriteWarc(threading.Thread):
def __init__(self, out_queue, warcfile):
threading.Thread.__init__(self)
self.out_queue = out_queue
self.lock = threading.Lock()
self.warcfile = warcfile
self.dedup = Dedup()
def run(self):
with open(self.warcfile, "ab") as output:
while True:
self.lock.acquire()
data = self.out_queue.get()
writer = WARCWriter(output, gzip=False)
headers_list = data[0]
http_headers = StatusAndHeaders(
"{} {}".format(data[3], data[4]), headers_list, protocol="HTTP/1.0"
)
record = writer.create_warc_record(
data[2], "response", payload=data[1], http_headers=http_headers
)
h = hashlib.sha1()
h.update(record.raw_stream.read(BLOCK_SIZE))
if self.dedup.lookup(h.hexdigest()):
record = writer.create_warc_record(
data[2], "revisit", http_headers=http_headers
)
writer.write_record(record)
self.out_queue.task_done()
self.lock.release()
else:
self.dedup.save(h.hexdigest(), data[2])
record.raw_stream.seek(0)
writer.write_record(record)
self.out_queue.task_done()
self.lock.release()
class Dedup:
"""
Stolen from warcprox
https://github.com/internetarchive/warcprox/blob/master/warcprox/dedup.py
"""
def __init__(self):
self.file = os.path.join(args.archive_dir, "dedup.db")
def start(self):
conn = sqlite3.connect(self.file)
conn.execute(
"create table if not exists dedup ("
" key varchar(300) primary key,"
" value varchar(4000)"
");"
)
conn.commit()
conn.close()
def save(self, digest_key, url):
conn = sqlite3.connect(self.file)
conn.execute(
"insert or replace into dedup (key, value) values (?, ?)", (digest_key, url)
)
conn.commit()
conn.close()
def lookup(self, digest_key, url=None):
result = False
conn = sqlite3.connect(self.file)
cursor = conn.execute("select value from dedup where key = ?", (digest_key,))
result_tuple = cursor.fetchone()
conn.close()
if result_tuple:
result = True
return result
def parse_extended_entities(extended_entities_dict):
"""Parse media file URL:s form tweet data
:extended_entities_dict:
:returns: list of media file urls
"""
urls = []
if "media" in extended_entities_dict.keys():
for item in extended_entities_dict["media"]:
# add static image
urls.append(item["media_url_https"])
# add best quality video file
if "video_info" in item.keys():
max_bitrate = -1 # handle twitters occasional bitrate=0
video_url = None
for video in item["video_info"]["variants"]:
if "bitrate" in video.keys() and "content_type" in video.keys():
if video["content_type"] == "video/mp4":
if int(video["bitrate"]) > max_bitrate:
max_bitrate = int(video["bitrate"])
video_url = video["url"]
if not video_url:
print("Error: No bitrate / content_type")
print(item["video_info"])
else:
urls.append(video_url)
return urls
def parse_binlinks_from_tweet(tweetdict):
"""Parse binary file url:s from a single tweet.
:tweetdict: json data dict for tweet
:returns: list of urls for media files
"""
urls = []
if "user" in tweetdict.keys():
urls.append(tweetdict["user"]["profile_image_url_https"])
urls.append(tweetdict["user"]["profile_background_image_url_https"])
if "extended_entities" in tweetdict.keys():
urls.extend(parse_extended_entities(tweetdict["extended_entities"]))
return urls
def main():
start = time.time()
if not os.path.isdir(args.archive_dir):
os.mkdir(args.archive_dir)
logging.basicConfig(
filename=os.path.join(args.archive_dir, "media_harvest.log"),
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
)
logging.getLogger(__name__)
logging.info("Logging media harvest for %s", args.tweet_file)
urls = []
d = Dedup()
d.start()
uniqueUrlCount = 0
duplicateUrlCount = 0
if args.tweet_file.endswith(".gz"):
tweetfile = gzip.open(args.tweet_file, "r")
else:
tweetfile = open(args.tweet_file, "r")
logging.info("Checking for duplicate urls")
for line in tweetfile:
tweet = json.loads(line)
tweet_urls = parse_binlinks_from_tweet(tweet)
for url in tweet_urls:
if not url in urls:
urls.append(url)
q.put(url)
uniqueUrlCount += 1
else:
duplicateUrlCount += 1
logging.info(
"Found %s total media urls %s unique and %s duplicates",
uniqueUrlCount + duplicateUrlCount,
uniqueUrlCount,
duplicateUrlCount,
)
threads = int(args.threads)
if threads > 2:
threads = 2
for i in range(threads):
t = GetResource(q)
t.daemon = True
t.start()
wt = WriteWarc(out_queue, os.path.join(args.archive_dir, "warc.warc"))
wt.daemon = True
wt.start()
q.join()
out_queue.join()
logging.info(
"Finished media harvest in %s", str(timedelta(seconds=(time.time() - start)))
)
if __name__ == "__main__":
parser = argparse.ArgumentParser("archive")
parser.add_argument(
"tweet_file", action="store", help="a twitter jsonl.gz input file"
)
parser.add_argument(
"archive_dir",
action="store",
help="a directory where the resulting warc is stored",
)
parser.add_argument(
"--threads",
action="store",
default=1,
help="Number of threads that fetches media resources",
)
args = parser.parse_args()
main()