yf_yzl vor 2 Jahren
Ursprung
Commit
112540d42c
1 geänderte Dateien mit 22 neuen und 7 gelöschten Zeilen
  1. 22 7
      crond_script/crond_weather.py

+ 22 - 7
crond_script/crond_weather.py

@@ -78,18 +78,33 @@ def init_data():
 
 def main():
 
-    day_data = DayData.objects.all()
+    day_data = DayData.objects.filter(id__gte=873)
     for day_obj in day_data:
         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, timeout=5)
+        headers = {
+            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299',
+            'Accept-Language': 'en-US,en;q=0.8',
+            'Accept-Encoding': 'gzip, deflate, br',
+            'Connection': 'keep-alive',
+        }
+        today_response = requests.get(today_url, headers=headers, timeout=5)
         try:
-            today_data = json.loads(today_response.text)
-            if "errcode" not in today_data.keys():
-                day_obj.content = str(today_data)
-                day_obj.save()
+            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}")
         except Exception as e:
-            logging.warning(f"cityid {day_obj.cityid}  error {e} {today_response.text}")
+            logging.warning(f"request fail again : {day_obj.cityid}")
+            today_response = requests.get(today_url, headers=headers, timeout=5)
+            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()
         time.sleep(random.randint(1, 5))
 
     logging.warning("up server day ...")