|
|
@@ -0,0 +1,72 @@
|
|
|
+import requests
|
|
|
+import datetime
|
|
|
+import json
|
|
|
+import os
|
|
|
+import sys
|
|
|
+from weather import all_city
|
|
|
+import logging
|
|
|
+import django
|
|
|
+BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
+sys.path.append(BASE_DIR)
|
|
|
+os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bigdataAPI.settings')
|
|
|
+django.setup()
|
|
|
+from django.conf import settings
|
|
|
+from apps.Weather.models import HistoryData, AddressInfo, DayData
|
|
|
+
|
|
|
+
|
|
|
+# 配置日志级别和格式
|
|
|
+logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
+file_handler = logging.FileHandler('/data/weather/history.log')
|
|
|
+file_handler.setLevel(logging.INFO)
|
|
|
+# 创建一个格式化程序,用于定义日志消息的显示方式
|
|
|
+formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
|
|
+file_handler.setFormatter(formatter)
|
|
|
+# 将文件处理器添加到根日志记录器中
|
|
|
+logging.getLogger('').addHandler(file_handler)
|
|
|
+
|
|
|
+
|
|
|
+def add_address_info_data():
|
|
|
+ address = AddressInfo.objects.all().count()
|
|
|
+ if not address:
|
|
|
+ day_data = DayData.objects.all()
|
|
|
+ for day in day_data:
|
|
|
+ AddressInfo.objects.get_or_create(
|
|
|
+ cityid = day.cityid,
|
|
|
+ defaults={
|
|
|
+ "cityid": day.cityid,
|
|
|
+ "province": day.province,
|
|
|
+ "city": day.city,
|
|
|
+ "district": day.district
|
|
|
+ }
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ logging.warning("address_info 已存在数据")
|
|
|
+
|
|
|
+
|
|
|
+def history_data():
|
|
|
+ url = settings.HISTORY_WEATHER_URL
|
|
|
+ appid = settings.HISTORY_WEATHER_APPID
|
|
|
+ appsecret = settings.HISTORY_WEATHER_APPSECRET
|
|
|
+ address = AddressInfo.objects.all().values("cityid")
|
|
|
+ for i in address[:1]:
|
|
|
+ cityid = i.get("cityid")
|
|
|
+ for y in [2022, 2023]:
|
|
|
+ for m in range(1,13):
|
|
|
+ response = requests.get(url=f"{url}appid={appid}&appsecret={appsecret}&cityid={cityid}&year={y}&month={m}")
|
|
|
+ result = json.loads(response.text)
|
|
|
+ for k in result["data"]:
|
|
|
+ ymd = k["ymd"]
|
|
|
+ timestamp = datetime.datetime.strptime(ymd, "%Y-%m-%d").timestamp()
|
|
|
+ HistoryData.objects.get_or_create(
|
|
|
+ timestamp = timestamp,
|
|
|
+ cityid = cityid,
|
|
|
+ defaults={
|
|
|
+ "timestamp":timestamp,
|
|
|
+ "cityid": cityid,
|
|
|
+ "content": str(k)
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ history_data()
|