-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhydrate.py
55 lines (39 loc) · 1.27 KB
/
hydrate.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
# Hydarate files from 2020-01', '2020-02', '2020-03', '2020-04', '2020-05'
import gzip
import json
from tqdm import tqdm
from twarc import Twarc
from pathlib import Path
twarc = Twarc()
data_dirs = ['2020-01', '2020-02', '2020-03', '2020-04', '2020-05']
def main():
for data_dir in data_dirs:
for path in Path(data_dir).iterdir():
if path.name.endswith('.txt'):
hydrate(path)
def _reader_generator(reader):
b = reader(1024 * 1024)
while b:
yield b
b = reader(1024 * 1024)
def raw_newline_count(fname):
"""
Counts number of lines in file
"""
f = open(fname, 'rb')
f_gen = _reader_generator(f.raw.read)
return sum(buf.count(b'\n') for buf in f_gen)
def hydrate(id_file):
print('hydrating {}'.format(id_file))
gzip_path = id_file.with_suffix('.jsonl.gz')
if gzip_path.is_file():
print('skipping json file already exists: {}'.format(gzip_path))
return
num_ids = raw_newline_count(id_file)
with gzip.open(gzip_path, 'w') as output:
with tqdm(total=num_ids) as pbar:
for tweet in twarc.hydrate(id_file.open()):
output.write(json.dumps(tweet).encode('utf8') + b"\n")
pbar.update(1)
if __name__ == "__main__":
main()