|
@@ -14,20 +14,20 @@ 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, ServerDayData
|
|
|
-# 配置日志级别和格式
|
|
|
|
|
-logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
+# # 配置日志级别和格式
|
|
|
|
|
+# logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
|
-# 创建一个文件处理器,指定日志文件名和写入模式
|
|
|
|
|
-os.mkdir("/data/weather/") if not os.path.exists("/data/weather/") else None
|
|
|
|
|
-file_handler = logging.FileHandler('/data/weather/app.log')
|
|
|
|
|
-file_handler.setLevel(logging.INFO)
|
|
|
|
|
|
|
+# # 创建一个文件处理器,指定日志文件名和写入模式
|
|
|
|
|
+# os.mkdir("/data/weather/") if not os.path.exists("/data/weather/") else None
|
|
|
|
|
+# file_handler = logging.FileHandler('/data/weather/app.log')
|
|
|
|
|
+# file_handler.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
-# 创建一个格式化程序,用于定义日志消息的显示方式
|
|
|
|
|
-formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
-file_handler.setFormatter(formatter)
|
|
|
|
|
|
|
+# # 创建一个格式化程序,用于定义日志消息的显示方式
|
|
|
|
|
+# formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
+# file_handler.setFormatter(formatter)
|
|
|
|
|
|
|
|
-# 将文件处理器添加到根日志记录器中
|
|
|
|
|
-logging.getLogger('').addHandler(file_handler)
|
|
|
|
|
|
|
+# # 将文件处理器添加到根日志记录器中
|
|
|
|
|
+# logging.getLogger('').addHandler(file_handler)
|
|
|
|
|
|
|
|
app_id = "69334222"
|
|
app_id = "69334222"
|
|
|
app_secret = "2u4bHXHD"
|
|
app_secret = "2u4bHXHD"
|
|
@@ -84,6 +84,7 @@ def main():
|
|
|
'Accept-Encoding': 'gzip, deflate, br',
|
|
'Accept-Encoding': 'gzip, deflate, br',
|
|
|
'Connection': 'keep-alive',
|
|
'Connection': 'keep-alive',
|
|
|
}
|
|
}
|
|
|
|
|
+ print("up today ....")
|
|
|
day_data = DayData.objects.all()
|
|
day_data = DayData.objects.all()
|
|
|
today_error = 0
|
|
today_error = 0
|
|
|
# 调用次数统计
|
|
# 调用次数统计
|
|
@@ -91,7 +92,7 @@ def main():
|
|
|
start_ct_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
start_ct_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
for day_obj in day_data:
|
|
for day_obj in day_data:
|
|
|
time_out = False
|
|
time_out = False
|
|
|
- logging.warning(day_obj.cityid)
|
|
|
|
|
|
|
+ print(day_obj.cityid)
|
|
|
today_url = f"http://v1.yiketianqi.com/api?unescape=1&version=v62&appid=69334222&appsecret=2ME6U58N&cityid={day_obj.cityid}"
|
|
today_url = f"http://v1.yiketianqi.com/api?unescape=1&version=v62&appid=69334222&appsecret=2ME6U58N&cityid={day_obj.cityid}"
|
|
|
today_response = requests.get(today_url, headers=headers, timeout=10)
|
|
today_response = requests.get(today_url, headers=headers, timeout=10)
|
|
|
day_ct += 1
|
|
day_ct += 1
|
|
@@ -102,8 +103,8 @@ def main():
|
|
|
day_obj.content = str(today_data)
|
|
day_obj.content = str(today_data)
|
|
|
day_obj.save()
|
|
day_obj.save()
|
|
|
else:
|
|
else:
|
|
|
- logging.warning(f"cityid {day_obj.cityid} error_code {today_response.status_code}")
|
|
|
|
|
- logging.warning(f"request fail again : {day_obj.cityid}")
|
|
|
|
|
|
|
+ print(f"cityid {day_obj.cityid} error_code {today_response.status_code}")
|
|
|
|
|
+ print(f"request fail again : {day_obj.cityid}")
|
|
|
time.sleep(2)
|
|
time.sleep(2)
|
|
|
today_response = requests.get(today_url, headers=headers, timeout=5)
|
|
today_response = requests.get(today_url, headers=headers, timeout=5)
|
|
|
day_ct += 1
|
|
day_ct += 1
|
|
@@ -112,16 +113,16 @@ 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()
|
|
|
- logging.warning(f"request again success: {day_obj.cityid}")
|
|
|
|
|
|
|
+ print(f"request again success: {day_obj.cityid}")
|
|
|
else:
|
|
else:
|
|
|
- logging.warning(f"cityid {day_obj.cityid} error_code {today_response.status_code} again request fail")
|
|
|
|
|
- logging.error(f"error {today_error}")
|
|
|
|
|
|
|
+ print(f"cityid {day_obj.cityid} error_code {today_response.status_code} again request fail")
|
|
|
|
|
+ print(f"error {today_error}")
|
|
|
today_error += 1
|
|
today_error += 1
|
|
|
time_out = True
|
|
time_out = True
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
- logging.warning(f"request fail again : {day_obj.cityid} {e.args}")
|
|
|
|
|
|
|
+ print(f"request fail again : {day_obj.cityid} {e.args}")
|
|
|
if time_out:
|
|
if time_out:
|
|
|
- time.sleep(5)
|
|
|
|
|
|
|
+ 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:
|