-
Notifications
You must be signed in to change notification settings - Fork 0
/
isCached.py
78 lines (61 loc) · 2.47 KB
/
isCached.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
71
72
73
74
75
import json
import requests
import pathlib
from datetime import datetime
import os
#make dir cache
pathlib.Path('Cache').mkdir(parents=True, exist_ok=True)
## variables
time_format = "%Y-%m-%d %H:%M:%S"
fileJson = 'Cache/isCached.json'
parse_time_now = datetime.now()
time_now = parse_time_now.strftime(time_format)
# cache_time = 8 #hours
baseUrl = 'https://api-football-standings.azharimm.site/leagues/'
data_year = os.environ['data_year']
# data_year = '2020'
class doCache:
@staticmethod
def compare_time(dataId=None, cache_time=2):
d1 = datetime.strptime(get_time_value(dataId), time_format)
d2 = datetime.strptime(time_now, time_format)
daysDiff = (d2-d1).days
hoursDiff = daysDiff * 24
if get_data_year(dataId) != data_year or hoursDiff > cache_time:
write_time(dataId)
save_data(dataId)
return True
else:
return False
def write_time(dataId=None):
with open(fileJson, 'r') as f:
my_list = json.load(f)
for idx, obj in enumerate(my_list):
if obj['id'] == dataId:
obj['last_updated'] = time_now
obj['data_year'] = data_year
with open(fileJson, 'w') as f:
f.write(json.dumps(my_list, separators=(',',': '), indent=4))
return
def save_data(dataId=None):
if (dataId == 'leagues'):
jsonInternet = requests.get(baseUrl) # (json url)
else:
jsonInternet = requests.get(f"{baseUrl}{dataId}/standings?season={data_year}&sort=asc") # (json url)
data = jsonInternet.json()
with open(('Cache/'+dataId+'.json'), 'w') as f:
json.dump(data, f, indent=4, separators=(',',': '))
# write_time(dataId)
def get_time_value(dataId=None):
with open(fileJson) as f:
data = json.load(f)
list1 = list ((p_id.get('last_updated') for p_id in data if p_id.get('id') == dataId))
return ''.join(list1)
def get_data_year(dataId=None):
with open(fileJson) as f:
data = json.load(f)
list1 = list ((p_id.get('data_year') for p_id in data if p_id.get('id') == dataId))
return ''.join(list1)
# save_data("eng.1")
# be = doCache()
# print(be.compare_time("eng.1"))