|
@@ -13,7 +13,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
sys.path.append(BASE_DIR)
|
|
sys.path.append(BASE_DIR)
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bigdataAPI.settings')
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bigdataAPI.settings')
|
|
|
django.setup()
|
|
django.setup()
|
|
|
-from apps.Weather.models import District, DayData, ServerDayData
|
|
|
|
|
|
|
+from apps.Weather.models import District, DayData, HistoryData
|
|
|
# # 配置日志级别和格式
|
|
# # 配置日志级别和格式
|
|
|
# logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
# logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
@@ -89,7 +89,10 @@ def main():
|
|
|
today_error = 0
|
|
today_error = 0
|
|
|
# 调用次数统计
|
|
# 调用次数统计
|
|
|
day_ct = 0
|
|
day_ct = 0
|
|
|
- start_ct_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
|
|
+ now = datetime.now()
|
|
|
|
|
+ start_ct_time = now.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
+ now_hour = now.hour
|
|
|
|
|
+ today_str = datetime.strptime(f'{now.year-now.month-now.day} 19:00:00', "%Y:%m:%d %H:%M:%S")
|
|
|
for day_obj in day_data:
|
|
for day_obj in day_data:
|
|
|
time_out = False
|
|
time_out = False
|
|
|
print(day_obj.cityid)
|
|
print(day_obj.cityid)
|
|
@@ -102,6 +105,15 @@ def main():
|
|
|
if "errcode" not in today_data.keys():
|
|
if "errcode" not in today_data.keys():
|
|
|
day_obj.content = str(today_data)
|
|
day_obj.content = str(today_data)
|
|
|
day_obj.save()
|
|
day_obj.save()
|
|
|
|
|
+ try:
|
|
|
|
|
+ if now_hour == 19:
|
|
|
|
|
+ HistoryData.objects.create(
|
|
|
|
|
+ cityid = day_obj.cityid,
|
|
|
|
|
+ content = today_data,
|
|
|
|
|
+ addtime = today_str.timestamp()
|
|
|
|
|
+ )
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(e, "历史数据入库失败")
|
|
|
else:
|
|
else:
|
|
|
print(f"cityid {day_obj.cityid} error_code {today_response.status_code}")
|
|
print(f"cityid {day_obj.cityid} error_code {today_response.status_code}")
|
|
|
print(f"request fail again : {day_obj.cityid}")
|
|
print(f"request fail again : {day_obj.cityid}")
|
|
@@ -114,6 +126,15 @@ def main():
|
|
|
day_obj.content = str(today_data)
|
|
day_obj.content = str(today_data)
|
|
|
day_obj.save()
|
|
day_obj.save()
|
|
|
print(f"request again success: {day_obj.cityid}")
|
|
print(f"request again success: {day_obj.cityid}")
|
|
|
|
|
+ try:
|
|
|
|
|
+ if now_hour == 19:
|
|
|
|
|
+ HistoryData.objects.create(
|
|
|
|
|
+ cityid = day_obj.cityid,
|
|
|
|
|
+ content = today_data,
|
|
|
|
|
+ addtime = today_str.timestamp()
|
|
|
|
|
+ )
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(e, "历史数据入库失败")
|
|
|
else:
|
|
else:
|
|
|
print(f"cityid {day_obj.cityid} error_code {today_response.status_code} again request fail")
|
|
print(f"cityid {day_obj.cityid} error_code {today_response.status_code} again request fail")
|
|
|
print(f"error {today_error}")
|
|
print(f"error {today_error}")
|
|
@@ -125,9 +146,12 @@ def main():
|
|
|
time.sleep(3)
|
|
time.sleep(3)
|
|
|
else:
|
|
else:
|
|
|
time.sleep(2)
|
|
time.sleep(2)
|
|
|
|
|
+
|
|
|
with open("/data/weather/weather_count.txt", 'a+', encoding='utf-8') as f:
|
|
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} \n")
|
|
f.write(f"开始时间:{start_ct_time} 结束时间:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} 调用次数:{day_ct} \n")
|
|
|
|
|
|
|
|
|
|
+ # 把19点的数据写入历史天气数据中
|
|
|
|
|
+
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|
|
|
main()
|
|
main()
|