卫健委中高风险地区查询
疫情原因很多高校部门都需要时刻关注中高风险区域查询,来密切关注各学生的途径是存在中高风险,来进行人员管控。因此应学习要求,通过各种百度途径得到获取卫健委的中高风险区域查询的接口,并做了一些调整,将获取到的数据存储到oracle数据库中,完整代码如下:
#引入模块
import logging
import time
import traceback
import requests
import cx_Oracle
import os
#oracle数据访问信息
os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.AL32UTF8'
database = cx_Oracle.connect('USR_DFM','123445','127.0.0.0:1521/orcl')
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
data = None
#卫健委数据接口
def get_data():
global data
#url = "http://103.66.32.242:8005/zwfwMovePortal/interface/interfaceJson"
url = 'https://bmfw.www.gov.cn/bjww/interface/interfaceJson'
timestamp = str(int(time.time()))
nonceHeader = "123456789abcdefg"
signatureHeaderStr = timestamp + "23y0ufFl5YxIyGrI8hWRUZmKkvtSjLQA" + nonceHeader + timestamp
signatureHeader = hashlib.sha256(signatureHeaderStr.encode(encoding="UTF-8")).hexdigest()
payload = {
"appId": "NcApplication",
"paasHeader": "zdww",
"timestampHeader": timestamp,
"nonceHeader": nonceHeader,
"signatureHeader": signatureHeader.upper(),
"key": "3C502C97ABDA40D0A60FBEE50FAAD1DA"
}
signatureStr = timestamp + "fTN2pfuisxTavbTuYVSsNJHetwq5bJvCQkjjtiLM2dCratiA" + timestamp
signature = hashlib.sha256(signatureStr.encode(encoding="UTF-8")).hexdigest()
headers = {
'x-wif-nonce': 'QkjjtiLM2dCratiA',
'x-wif-paasid': 'smt-application',
'x-wif-signature': signature.upper(),
'x-wif-timestamp': timestamp,
}
response = requests.post(url, headers=headers, json=payload)
logging.info(' ====> %s', response.text)
response_json = response.json()
#print(response_json ['data']['highlist'])
if response_json['code'] == 0:
#print(json.dumps(response_json, sort_keys=True, indent=2, ensure_ascii=False))
#print(f"最后更新时间: {response_json['data']['end_update_time']}")
#print(f"\t高风险个数: {response_json['data']['hcount']}")
cursor = database.cursor()
sql_delete = "truncate table T_YQ_ZGFXCX"
cursor.execute(sql_delete)
#Z_DATE = f"{response_json['data']['end_update_time']}"
sql = "INSERT INTO T_YQ_ZGFXCX (SSQ, SF, S, Q, FXLB, XXDZ) VALUES (:1,:2,:3,:4,:5,:6)"
#高风险区域数据写入数据库
for high in response_json['data']['highlist']:
z_area_name = high['area_name']
z_city = high['city']
z_county = high['county']
z_province = high['province']
z_dj = '高风险区域'
a = ', '.join(high['communitys'])
z_communitys = a.split(', ')
for i in z_communitys:
args = (z_area_name,z_province, z_city,z_county,z_dj,i)
#print(args)
cursor.execute(sql, args)
#print("\t中风险个数: {response_json['data']['mcount']}")
database.commit()
#中风险区域数据写入数据库
for middle in response_json['data']['middlelist']:
z_area_name = middle['area_name']
z_city = middle['city']
z_county = middle['county']
z_province = middle['province']
z_dj = '中风险区域'
a = ', '.join(middle['communitys'])
z_communitys = a.split(', ')
for i in z_communitys:
args = (z_area_name,z_province, z_city,z_county,z_dj,i)
#print(args)
cursor.execute(sql, args)
#print("\t中风险个数: {response_json['data']['mcount']}")
database.commit()
#低风险区域数据写入数据库
for low in response_json['data']['lowlist']:
z_area_name = low['area_name']
z_city = low['city']
z_county = low['county']
z_province = low['province']
z_dj = '低风险区域'
a = ', '.join(low['communitys'])
z_communitys = a.split(', ')
for i in z_communitys:
args = (z_area_name,z_province, z_city,z_county,z_dj,i)
#print(args)
cursor.execute(sql, args)
#print("\t中风险个数: {response_json['data']['mcount']}")
database.commit()
if __name__ == '__main__':
err = 0
while True:
try:
#每隔两个小时执行一次
get_data()
time.sleep(60*60)
except Exception as e:
traceback.print_exc()
err += 1
logging.error('------- error -----> %s', e)
"send_msg('监控风险地区有异常', str(traceback.format_exc()))" if err <= 3 else sys.exit(0)
time.sleep(60)