|
|
@@ -84,51 +84,6 @@ def main():
|
|
|
'Accept-Encoding': 'gzip, deflate, br',
|
|
|
'Connection': 'keep-alive',
|
|
|
}
|
|
|
- day_data = DayData.objects.all()
|
|
|
- today_error = 0
|
|
|
- # 调用次数统计
|
|
|
- day_ct = 0
|
|
|
- start_ct_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
- for day_obj in day_data:
|
|
|
- time_out = False
|
|
|
- logging.warning(day_obj.cityid)
|
|
|
- today_url = f"http://v0.yiketianqi.com/api?unescape=1&version=v62&appid=69334222&appsecret=2ME6U58N&cityid={day_obj.cityid}"
|
|
|
- today_response = requests.get(today_url, headers=headers, timeout=5)
|
|
|
- day_ct += 1
|
|
|
- try:
|
|
|
- if today_response.status_code == 200:
|
|
|
- today_data = json.loads(today_response.text)
|
|
|
- if "errcode" not in today_data.keys():
|
|
|
- day_obj.content = str(today_data)
|
|
|
- day_obj.save()
|
|
|
- else:
|
|
|
- logging.warning(f"cityid {day_obj.cityid} error_code {today_response.status_code}")
|
|
|
- logging.warning(f"request fail again : {day_obj.cityid}")
|
|
|
- time.sleep(2)
|
|
|
- today_response = requests.get(today_url, headers=headers, timeout=5)
|
|
|
- day_ct += 1
|
|
|
- if today_response.status_code == 200:
|
|
|
- today_data = json.loads(today_response.text)
|
|
|
- if "errcode" not in today_data.keys():
|
|
|
- day_obj.content = str(today_data)
|
|
|
- day_obj.save()
|
|
|
- logging.warning(f"request again success: {day_obj.cityid}")
|
|
|
- else:
|
|
|
- logging.warning(f"cityid {day_obj.cityid} error_code {today_response.status_code} again request fail")
|
|
|
- logging.error(f"error {today_error}")
|
|
|
- today_error += 1
|
|
|
- time_out = True
|
|
|
- except Exception as e:
|
|
|
- logging.warning(f"request fail again : {day_obj.cityid} {e.args}")
|
|
|
- if time_out:
|
|
|
- time.sleep(5)
|
|
|
- else:
|
|
|
- time.sleep(2)
|
|
|
- with open("/data/weather/weather_count.txt", 'a+', encoding='utf-8') as f:
|
|
|
- f.write(f"开始时间:{start_ct_time} 结束时间:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} 调用次数:{day_ct}")
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
server_error = 0
|
|
|
logging.warning("up server day ...")
|
|
|
server_ct = 0
|
|
|
@@ -173,66 +128,5 @@ def main():
|
|
|
f.write(f"开始时间:{server_start_ct_time} 结束时间:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} 调用次数:{server_ct}")
|
|
|
|
|
|
|
|
|
- """
|
|
|
- district = District.objects.all()
|
|
|
- # init_data()
|
|
|
- logging.warning("up...")
|
|
|
- results = District.objects.filter(id__gt = 31)
|
|
|
- # 更新数据
|
|
|
- for row in results:
|
|
|
- temp = row.city # 东城区
|
|
|
- city_id = row.city_id # 110101000000
|
|
|
- pid = row.pid # 110100000000
|
|
|
- province_id = city_id[:2]
|
|
|
- province = province_dict.get(province_id)
|
|
|
- if len(pid) == 2 and temp == "市辖区":
|
|
|
- # 处理 市辖区 只有两级,区为空
|
|
|
- city = province
|
|
|
- district = "市辖区"
|
|
|
- elif len(pid) ==2 and temp != "市辖区":
|
|
|
- # 三级 省 市 区(县)
|
|
|
- city = temp
|
|
|
- district = ""
|
|
|
- else:
|
|
|
- dis = District.objects.get(city_id=pid)
|
|
|
- city = dis.city
|
|
|
- district = temp
|
|
|
-
|
|
|
- try:
|
|
|
- today_url = f"http://v0.yiketianqi.com/api?unescape=1&version=v62&appid=69334222&appsecret=2u4bHXHD&adcode={city_id}"
|
|
|
- today_response = requests.get(today_url, timeout=5)
|
|
|
- today_data = json.loads(today_response.text)
|
|
|
- if "errcode" not in today_data.keys():
|
|
|
- cityid = today_data.get("cityid", "")
|
|
|
- DayData.objects.update_or_create(
|
|
|
- cityid=cityid,
|
|
|
- defaults={
|
|
|
- "cityid": cityid,
|
|
|
- "province": province,
|
|
|
- "city": city,
|
|
|
- "district": district,
|
|
|
- "content": str(today_data)
|
|
|
- }
|
|
|
- )
|
|
|
- server_day_url = f"http://v0.yiketianqi.com/api?unescape=1&version=v91&appid={app_id}&appsecret={app_secret}&adcode={city_id}&ext=hours,aqi,life"
|
|
|
- server_day_response = requests.get(server_day_url, timeout=5)
|
|
|
- server_day_data = json.loads(server_day_response.text)
|
|
|
- if "errcode" not in server_day_data.keys():
|
|
|
- cityid = today_data.get("cityid", "")
|
|
|
- ServerDayData.objects.update_or_create(
|
|
|
- cityid=cityid,
|
|
|
- defaults={
|
|
|
- "cityid": cityid,
|
|
|
- "province": province,
|
|
|
- "city": city,
|
|
|
- "district": district,
|
|
|
- "content": str(server_day_data)
|
|
|
- }
|
|
|
- )
|
|
|
- except Exception as e:
|
|
|
- print(e)
|
|
|
- logging.warning("over...")
|
|
|
- """
|
|
|
-
|
|
|
if __name__ == "__main__":
|
|
|
main()
|