-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomain-size-merge.py
executable file
·47 lines (35 loc) · 1.16 KB
/
domain-size-merge.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
#!/usr/bin/env python3
import sys
import gzip
import re
import os
from io import TextIOWrapper
from collections import Counter, defaultdict
from multiprocessing.pool import Pool
def sum_domains(paths):
counter = Counter()
for path in paths:
with gzip.open(path.rstrip(), 'r') as fh:
for linenr, line in enumerate(TextIOWrapper(fh, encoding='utf-8'), start=1):
match = re.match(r'^\s*([^\s]+)\s+(\d+)$', line.rstrip())
# Tell me I messed up
if not match:
# print("Ignoring line '{}' in {}".format(line.rstrip(), path, linenr), file=sys.stderr)
continue
# Ignore the small stuff
if int(match[2]) < 1000:
break
counter[match[1]] += int(match[2])
return counter
shards = defaultdict(list)
for path in sys.stdin:
_, shard, _ = path.strip().rsplit('/', maxsplit=2)
if os.path.exists(path.strip()):
shards[shard].append(path.strip())
else:
print("File does not exist: {}".format(path.strip()), file=sys.stderr)
print("{} shards".format(len(shards)), file=sys.stderr)
pool = Pool(16)
for counter in pool.imap_unordered(sum_domains, shards.values()):
for domain, count in counter.items():
print("{}\t{}".format(domain, count))