-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.py
executable file
·64 lines (56 loc) · 2.2 KB
/
dump.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
#!/usr/bin/python3
import hashlib
import datetime
import export_pb2 # https://github.com/google/exposure-notifications-server/blob/main/internal/pb/export/export.proto
import json
import os
import urllib.parse
import urllib.request
import zipfile
cache_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'cache'))
os.makedirs(cache_dir, exist_ok=True)
cdn_prefix = 'https://covid19radar-jpn-prod.azureedge.net/c19r/'
list_urls = [
('list1.json', cdn_prefix + '440/list.json'),
('list2.json', cdn_prefix + '441/list.json'),
]
now = datetime.datetime.now()
def cached_fetch(name, url):
path = os.path.join(cache_dir, name)
cache_hit = False
try:
rv = os.stat(path)
cached = datetime.datetime.fromtimestamp(rv.st_mtime)
cache_hit = now < cached + datetime.timedelta(hours=1)
except FileNotFoundError as e:
pass
if not cache_hit:
urllib.request.urlretrieve(url, path)
return path
file_urls = []
for name, url in list_urls:
with open(cached_fetch(name, url)) as fd:
for item in json.load(fd):
mtime = datetime.datetime.fromtimestamp(item['created'] / 1000.0)
if mtime > now - datetime.timedelta(days=3):
file_urls.append(item['url'])
# i = 0
for url in file_urls:
# i += 1
name = hashlib.sha256(url.encode()).hexdigest()[:8]
# name = os.path.basename(urllib.parse.urlparse(url).path)
# print(name)
with zipfile.ZipFile(cached_fetch(name, url)) as zip:
with zip.open('export.bin') as bin:
assert(bin.read(16) == b'EK Export v1 ')
teke = export_pb2.TemporaryExposureKeyExport.FromString(bin.read())
# print(teke)
for key in teke.keys:
# print(' key_data: {}'.format(key.key_data))
ts = key.rolling_start_interval_number * 10 * 60
# dur = key.rolling_period * 10
print('{}: {}'.format(key.key_data.hex(), datetime.datetime.fromtimestamp(ts)))
# if key.HasField('report_type'):
# name = export_pb2.TemporaryExposureKey.ReportType.Name(key.report_type)
# print(' report_type: {}'.format(name))
# print(key)