This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
risklevel.py
144 lines (124 loc) · 4.2 KB
/
risklevel.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 16 11:30:41 2021
@author: roselily
Modified on Fri Apr 15 14:31:50 2022
@author: panghaibin
"""
import os
import json
import time
import hashlib
import requests
from datetime import datetime
# Location of Json files
PATHS = 'Archive'
ABS_PATH = os.path.dirname(os.path.abspath(__file__))
PATHS = os.path.join(ABS_PATH, PATHS)
def fetch_new():
"""
Fetch the latest risk data from the API
:return: json data
"""
# Params from Ajax.js
key = '3C502C97ABDA40D0A60FBEE50FAAD1DA'
timestamp = str(int(time.time()))
token = '23y0ufFl5YxIyGrI8hWRUZmKkvtSjLQA'
nonce = '123456789abcdefg'
pass_header = 'zdww'
_ = timestamp + token + nonce + timestamp
_ = _.encode('utf-8')
signatureHeader = hashlib.sha256(_).hexdigest().upper()
_ = timestamp + 'fTN2pfuisxTavbTuYVSsNJHetwq5bJvC' + 'QkjjtiLM2dCratiA' + timestamp
_ = _.encode('utf-8')
wif_signature = hashlib.sha256(_).hexdigest().upper()
# Send post requests
url = 'http://bmfw.www.gov.cn/bjww/interface/interfaceJson'
data = {
"appId": "NcApplication",
"paasHeader": pass_header,
"timestampHeader": timestamp,
"nonceHeader": nonce,
"signatureHeader": signatureHeader,
"key": key
}
header = {
"x-wif-nonce": "QkjjtiLM2dCratiA",
"x-wif-paasid": "smt-application",
"x-wif-signature": wif_signature,
"x-wif-timestamp": timestamp,
"Origin": "http://bmfw.www.gov.cn",
"Referer": "http://bmfw.www.gov.cn/yqfxdjcx/risk.html",
"Content-Type": "application/json; charset=UTF-8"
}
r = requests.post(url, data=json.dumps(data), headers=header)
risk_json = r.json()
return risk_json
def save_json(file_path, json_data):
# Save Json file
with open(file_path, 'w', encoding="utf-8") as outfile:
json.dump(json_data, outfile, ensure_ascii=False)
def get_json(file_path):
if not os.path.exists(file_path):
return None
with open(file_path, 'r', encoding="utf-8") as outfile:
json_data = json.load(outfile)
return json_data
def get_info_by_list():
file_list = os.listdir(PATHS)
file_list.sort()
info = {'file_count': 0, 'file_list': []}
for file in file_list:
if '-' in file:
update_time = file.split('-')[0]
update_time += '+0800'
update_time = datetime.strptime(update_time, "%Y%m%d%H%z")
update_time = int(update_time.timestamp())
info_dict = {
'file_name': file,
'update_time': update_time,
}
info['file_list'].append(info_dict)
info['file_count'] += 1
return info
def main():
force_update = os.environ.get('FORCE_UPDATE', '')
force_update = force_update.lower() == 'true'
if not os.path.exists(PATHS):
os.makedirs(PATHS)
data = fetch_new()
update_time = data['data']['end_update_time']
update_time += '+0800'
update_time = datetime.strptime(update_time, "%Y-%m-%d %H时%z")
update_timestamp = int(update_time.timestamp())
update_time = datetime.strftime(update_time, "%Y%m%d%H")
data_hash = hashlib.md5(json.dumps(data, sort_keys=True).encode('utf-8')).hexdigest()
data_hash = data_hash[:8]
time_file_name = f'{update_time}-{data_hash}.json'
time_file_path = os.path.join(PATHS, time_file_name)
if os.path.exists(time_file_path) and not force_update:
print('File %s already exists' % time_file_name)
return False
save_json(time_file_path, data)
print('File %s saved' % time_file_name)
save_json(os.path.join(PATHS, 'latest.json'), data)
print('File latest.json updated')
info_path = os.path.join(PATHS, 'info.json')
info = get_json(info_path)
if info is None or force_update:
info = get_info_by_list()
else:
info['file_count'] += 1
info['file_list'].append({
'file_name': time_file_name,
'update_time': update_timestamp,
'save_time': int(time.time()),
})
save_json(info_path, info)
print('File info.json updated')
return True
if __name__ == '__main__':
if main():
exit(0)
else:
exit(1)