yf_yzl 2 лет назад
Родитель
Сommit
e21521cfd6
1 измененных файлов с 52 добавлено и 52 удалено
  1. 52 52
      crond_script/crond_weather.py

+ 52 - 52
crond_script/crond_weather.py

@@ -115,59 +115,59 @@ def init_data():
 
 def main():
     init_data()
-    special_time = [0, 8, 16, 17, 15]
-    while True:
-        now = datetime.datetime.now()
-        hour = int(now.hour)
-        print(hour, "--------------")
-        if hour in special_time:
-            conn1 = sqlite3.connect(db)
-            cursor1 = conn1.cursor()
-            sql = """
-                select * from district where id > 65
+    # special_time = [0, 8, 16, 17, 15]
+    # while True:
+    #     now = datetime.datetime.now()
+    #     hour = int(now.hour)
+    #     print(hour, "--------------")
+    #     if hour in special_time:
+    conn1 = sqlite3.connect(db)
+    cursor1 = conn1.cursor()
+    sql = """
+        select * from district where id > 65
+    """
+    cursor1.execute(sql)
+    results = cursor1.fetchall()
+    # 更新数据
+    for i, row in enumerate(results):
+        temp = row[1]
+        id = row[0]
+        pid = row[2]
+        if len(str(pid)) == 2:
+            city = temp 
+            district = ""
+        else:
+            print(pid)
+            cursor1.execute('select city from district where id = ?', (pid,))
+            te = cursor1.fetchone()
+            city = te[0] 
+            district = temp
+            print(district, city, "--------------")
+        province_id = (str(row[0]))[:2]
+        province = province_dict.get(province_id)
+        
+        # if district == '唐河县' or city == "郑州市" or city == "市辖区":
+        today_url = f"https://v0.yiketianqi.com/api?unescape=1&version=v62&appid={app_id}&appsecret={app_secret}&adcode={id}"
+        today_response = requests.get(today_url)
+        today_data = json.loads(today_response.text)
+        if "errcode" not in today_data.keys():
+            today_sql = """
+                INSERT OR REPLACE INTO `day_data` (`id`,`province`, `city`, `district`, content) VALUES (?, ?, ?, ?, ?)
+            """
+            cursor1.execute(today_sql, (i, province, city, district,str(today_data)))
+            print(city, district, province, "---------------------")
+        server_day_url = f"https://v0.yiketianqi.com/api?unescape=1&version=v91&appid={app_id}&appsecret={app_secret}&adcode={id}"
+        server_day_response = requests.get(server_day_url)
+        server_day_data = json.loads(server_day_response.text)
+        if "errcode" not in server_day_data.keys():
+            logging.warning(f"{province, city, district} server day weather success")
+            server_sql = f"""
+                INSERT OR REPLACE INTO `server_day_data` (`id`,`province`, `city`, `district`, `content`) VALUES (?, ?, ?, ?, ?)
             """
-            cursor1.execute(sql)
-            results = cursor1.fetchall()
-            # 更新数据
-            for i, row in enumerate(results):
-                temp = row[1]
-                id = row[0]
-                pid = row[2]
-                if len(str(pid)) == 2:
-                    city = temp 
-                    district = ""
-                else:
-                    print(pid)
-                    cursor1.execute('select city from district where id = ?', (pid,))
-                    te = cursor1.fetchone()
-                    city = te[0] 
-                    district = temp
-                    print(district, city, "--------------")
-                province_id = (str(row[0]))[:2]
-                province = province_dict.get(province_id)
-                
-                # if district == '唐河县' or city == "郑州市" or city == "市辖区":
-                today_url = f"https://v0.yiketianqi.com/api?unescape=1&version=v62&appid={app_id}&appsecret={app_secret}&adcode={id}"
-                today_response = requests.get(today_url)
-                today_data = json.loads(today_response.text)
-                if "errcode" not in today_data.keys():
-                    today_sql = """
-                        INSERT OR REPLACE INTO `day_data` (`id`,`province`, `city`, `district`, content) VALUES (?, ?, ?, ?, ?)
-                    """
-                    cursor1.execute(today_sql, (i, province, city, district,str(today_data)))
-                    print(city, district, province, "---------------------")
-                server_day_url = f"https://v0.yiketianqi.com/api?unescape=1&version=v91&appid={app_id}&appsecret={app_secret}&adcode={id}"
-                server_day_response = requests.get(server_day_url)
-                server_day_data = json.loads(server_day_response.text)
-                if "errcode" not in server_day_data.keys():
-                    logging.warning(f"{province, city, district} server day weather success")
-                    server_sql = f"""
-                        INSERT OR REPLACE INTO `server_day_data` (`id`,`province`, `city`, `district`, `content`) VALUES (?, ?, ?, ?, ?)
-                    """
-                    cursor1.execute(server_sql, (i, province, city, district,str(server_day_data),))
-                    print(city, district, province, "+++++++++++++++++++++")
-                conn1.commit()
-            conn1.close()
+            cursor1.execute(server_sql, (i, province, city, district,str(server_day_data),))
+            print(city, district, province, "+++++++++++++++++++++")
+        conn1.commit()
+    conn1.close()
 
 
 if __name__ == "__main__":