-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtCoderInfoCache.py
More file actions
61 lines (52 loc) · 2.21 KB
/
Copy pathAtCoderInfoCache.py
File metadata and controls
61 lines (52 loc) · 2.21 KB
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
import AtCoderInfo
import json
import sys
cache_path = ''
user_list_path = ''
class AtCoderInfoCache:
def __init__(self, cache_path, user_list_path):
self.cache_path = cache_path
self.user_list_path = user_list_path
self.cache = AtCoderInfoCache.__load_cache(cache_path)
self.user_list = AtCoderInfoCache.__load_user_list(user_list_path)
print(self.user_list)
if self.user_list is None:
raise Error('UserList file not found: {}'.format(user_list_path))
if self.cache is None:
self.cache = AtCoderInfoCache.__get_users(self.user_list)
self.cache = {username:AtCoderInfo.get_user(username) for username in self.user_list}
AtCoderInfoCache.__save_cache(cache_path, self.cache)
def get_latest(self):
records = AtCoderInfoCache.__get_users(self.user_list)
AtCoderInfoCache.__save_cache(self.cache_path, records)
diffs = {username:AtCoderInfoCache.__diff_record(records[username], self.cache[username]) for username in self.user_list if username in self.cache and self.cache[username] is not None}
return records, diffs
def __get_users(user_list):
return {username:AtCoderInfo.get_user(username) for username in user_list}
def __load_cache(path):
try:
with open(path) as f:
cache = json.load(f)
except:
cache = None
return cache
def __load_user_list(path):
try:
with open(path) as f:
ul = f.read().strip().split('\n')
except:
ul = None
return ul
def __save_cache(path, data):
with open(path, 'w') as f:
json.dump(data, f, indent=4)
def __diff_record(new, old):
return {
'name': new['name'],
'global_rank': old['global_rank'] - new['global_rank'],
'local_rank': old['local_rank'] - new['local_rank'],
'rating': new['rating'] - old['rating'],
'highest_rating': new['highest_rating'] - old['highest_rating'],
'competitions': new['competitions'] - old['competitions'],
'wins': new['wins'] - old['wins'],
}