-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathload_json.py
77 lines (66 loc) · 2.69 KB
/
load_json.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
76
77
# -*- coding: UTF-8 -*-
import json
import random
import time
import requests
from log_support import LogSupport
import os
# 初始化日志
ls = LogSupport()
def load_response():
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4037.2 Safari/537.36',
'Connection': 'keep - alive',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'Accept': '*/*',
'Sec-Fetch-Site': 'cross-site',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-User': '?1',
'Sec-Fetch-Dest': 'document',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en-AU;q=0.8,en;q=0.7,zh-TW;q=0.6',
'Host': 'service-f9fjwngp-1252021671.bj.apigw.tencentcs.com'
}
api = 'https://service-f9fjwngp-1252021671.bj.apigw.tencentcs.com/release/pneumonia'
response = json.loads(requests.get(api, headers=headers).text)
if not response['data']['listByArea']:
raise Exception(response)
ls.logging.info('json loaded at time {}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
return response
except Exception as e:
ls.logging.error('json load failed, waiting for around 15 seconds')
ls.logging.exception(e)
time.sleep(15 + 5 * random.random())
return load_response()
def load_json(file_name='./epidemic_history/latest.json'):
with open(file_name, 'r+') as f:
return json.load(f)
def write_json(file_name, js):
with open(file_name, 'w+') as f:
json.dump(js, f, sort_keys=True, indent=2)
def update():
response = load_json()
i = 0
while True:
time.sleep(30 + 60 * random.random())
latest_response = load_response()
if latest_response['data']['listByArea'] != response['data']['listByArea']:
write_json('epidemic_history/{}.json'.format(int(time.time())), latest_response)
write_json('epidemic_history/latest.json', latest_response)
ls.logging.info('json updated at time {}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
response = latest_response
i += 1
if i % 10 == 0:
git_upload()
i = 0
time.sleep(30 + 60 * random.random())
continue
def git_upload():
os.system('git add epidemic_history/')
os.system('git commit -m "update jsosn"')
os.system('git push')
ls.logging.info('jsons uploaded at time {}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
if __name__ == "__main__":
update()