forked from N3evin/AmiiboAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast_updated.py
63 lines (48 loc) · 1.62 KB
/
last_updated.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
#!/usr/bin/env python
import datetime
import hashlib
import json
class LastUpdated():
def __init__(self, file='last-updated.json'):
self.file = file
def read(self):
with open(self.file, 'r') as f:
data = json.load(f)
return {
'sha1': data['sha1'],
'timestamp': datetime.datetime.strptime(data['timestamp'], '%Y-%m-%dT%H:%M:%S.%f'),
}
def read_timestamp(self):
return self.read()['timestamp']
def write(self, sha1, timestamp):
with open(self.file, 'w') as f:
json.dump({
'sha1': sha1,
'timestamp': timestamp.isoformat(),
}, f, sort_keys=True)
def hash(self, data):
return hashlib.sha1(data).hexdigest()
def update(self, data):
sha1 = self.hash(data)
try:
last_update = self.read()
except Exception as e:
print(e)
last_update = None
updated = False
if last_update is None or last_update['sha1'] != sha1:
last_update = {
'sha1': sha1,
'timestamp': datetime.datetime.utcnow(),
}
self.write(**last_update)
updated = True
return last_update, updated
if __name__ == '__main__':
last_updater = LastUpdated()
with open('database/amiibo.json', 'rb') as f:
last_update, updated = last_updater.update(f.read())
if updated:
print('Updated: {}'.format(last_updater.file))
print('sha1: {}'.format(last_update['sha1']))
print('timestamp: {}'.format(last_update['timestamp'].isoformat()))