فهرست منبع

增加历史数据入库

林轩 1 سال پیش
والد
کامیت
51a902fb13
2فایلهای تغییر یافته به همراه27 افزوده شده و 5 حذف شده
  1. 1 3
      apps/Weather/models.py
  2. 26 2
      crond_script/today_weather.py

+ 1 - 3
apps/Weather/models.py

@@ -62,9 +62,7 @@ class HistoryDayData(models.Model):
 
     id = models.AutoField("ID", primary_key=True)
     cityid = models.CharField("城市ID", max_length=64, blank=True, null=True)
-    province = models.CharField("省", max_length=64, blank=True, null=True)
-    city = models.CharField("市", max_length=64, blank=True, null=True)
-    district = models.CharField("区", max_length=64, blank=True, null=True)
     content = models.TextField("天气")
+    addtime = models.PositiveIntegerField("时间", default=0)
     class Meta:
         db_table = "history_today"

+ 26 - 2
crond_script/today_weather.py

@@ -13,7 +13,7 @@ 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 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')
 
@@ -89,7 +89,10 @@ def main():
     today_error = 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:
         time_out = False 
         print(day_obj.cityid)
@@ -102,6 +105,15 @@ def main():
                 if "errcode" not in today_data.keys():
                     day_obj.content = str(today_data)
                     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:
                 print(f"cityid {day_obj.cityid}  error_code {today_response.status_code}")
                 print(f"request fail again : {day_obj.cityid}")
@@ -114,6 +126,15 @@ def main():
                         day_obj.content = str(today_data)
                         day_obj.save()
                         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:
                     print(f"cityid {day_obj.cityid}  error_code {today_response.status_code}  again request fail")
                     print(f"error {today_error}")
@@ -125,9 +146,12 @@ def main():
             time.sleep(3)
         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} \n")
 
+    # 把19点的数据写入历史天气数据中
+    
 
 if __name__ == "__main__":
     main()