Report_Generate_Server/tools/Get_Json.py

42 lines
1.3 KiB
Python
Raw Normal View History

import requests
import json
from tools.API import *
def get_data(url, data_type="data", params=None):
headers = {
"content_type": "application/x-www-form-urlencoded"
}
try:
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = json.loads(response.text)
print(f"获取到数据:{data}")
return data.get(data_type)
else:
print(f"请求数据失败,状态码:{response.status_code}")
return None
except Exception as e:
print(f"请求数据失败,异常:{e}")
return None
def get_project_info(projectId):
projecturl = DTURL + GETPROJECTINFO.format(projectId=projectId)
return get_data(projecturl, "data")
def get_jizu_info(turbineId):
jizuurl = DTURL + GETJIZUINFO.format(turbineId=turbineId)
return get_data(jizuurl, "data")
def get_jizu_shigong_info(turbineId):
jizuurl = DTURL + GETSHIGONGINFO
params = {
"rows": {
"turbineId": turbineId
}
}
result = get_data(jizuurl, "rows", params)
return result[0] if result else None
def get_weather(weatherid):
weatherurl = DTURL + GETWEATHERINFO.format(weatherCode=weatherid)
return get_data(weatherurl, "data")