-
Notifications
You must be signed in to change notification settings - Fork 112
/
last_updated.py
70 lines (55 loc) · 2.13 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
64
65
66
67
68
69
70
#!/usr/bin/env python
# coding=utf-8
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 {
'amiibo_sha1': data['amiibo_sha1'],
'game_info_sha1': data['game_info_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, amiibo_sha1, game_info_sha1, timestamp):
with open(self.file, 'w') as f:
json.dump({
'amiibo_sha1': amiibo_sha1,
'game_info_sha1': game_info_sha1,
'timestamp': timestamp.isoformat(),
}, f, sort_keys=True)
def hash(self, data):
return hashlib.sha1(data).hexdigest()
def update(self, data, data1):
amiibo_sha1 = self.hash(data)
game_info_sha1 = self.hash(data1)
try:
last_update = self.read()
except Exception as e:
print(e)
last_update = None
updated = False
if last_update is None or last_update['amiibo_sha1'] != amiibo_sha1 or last_update['game_info_sha1'] != game_info_sha1:
last_update = {
'amiibo_sha1': amiibo_sha1,
'game_info_sha1': game_info_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:
with open('database/games_info.json', 'rb') as g:
last_update, updated = last_updater.update(f.read(), g.read())
if updated:
print('Updated: {}'.format(last_updater.file))
print('amiibo_sha1: {}'.format(last_update['amiibo_sha1']))
print('game_info_sha1: {}'.format(last_update['game_info_sha1']))
print('timestamp: {}'.format(last_update['timestamp'].isoformat()))