yf_yzl 2 năm trước cách đây
mục cha
commit
d90a5ddd37
1 tập tin đã thay đổi với 16 bổ sung7 xóa
  1. 16 7
      crond_script/crond_weather.py

+ 16 - 7
crond_script/crond_weather.py

@@ -78,6 +78,7 @@ def init_data():
         """
         CREATE TABLE IF NOT EXISTS `day_data` (
         `id` INTEGER PRIMARY KEY,
+        `cityid` varchar(50) NOT NULL,
         `province` varchar(50) NOT NULL,
         `city` varchar(50) NOT NULL DEFAULT '',
         `district` varchar(50) NOT NULL DEFAULT '',
@@ -90,6 +91,7 @@ def init_data():
         """
         CREATE TABLE IF NOT EXISTS `server_day_data` (
         `id` INTEGER PRIMARY KEY,
+        `cityid` varchar(50) NOT NULL,
         `province` varchar(50) NOT NULL,
         `city` varchar(50) NOT NULL DEFAULT '',
         `district` varchar(50) NOT NULL DEFAULT '',
@@ -125,17 +127,22 @@ def main():
     results = cursor1.fetchall()
     # 更新数据
     for i, row in enumerate(results):
-        temp = row[1]
-        id = row[0]
-        pid = row[2]
+        temp = row[1]   # 东城区
+        id = row[0]     # 110101000000
+        pid = row[2]    # 110100000000
         if len(str(pid)) == 2:
+            # 处理 省 市 样例
             city = temp 
             district = ""
         else:
+            # 其它
             cursor1.execute('select city from district where id = ?', (pid,))
             te = cursor1.fetchone()
             city = te[0] 
             district = temp
+            if city == "市辖区":
+                city = district
+                district = ""
         province_id = (str(row[0]))[:2]
         province = province_dict.get(province_id)
         try:
@@ -143,18 +150,20 @@ def main():
             today_response = requests.get(today_url)
             today_data = json.loads(today_response.text)
             if "errcode" not in today_data.keys():
+                cityid = today_data.get("cityid", "")
                 today_sql = """
-                    INSERT OR REPLACE INTO `day_data` (`id`,`province`, `city`, `district`, content) VALUES (?, ?, ?, ?, ?)
+                    INSERT OR REPLACE INTO `day_data` (`id`, `cityid`, `province`, `city`, `district`, content) VALUES (?,?, ?, ?, ?, ?)
                 """
-                cursor1.execute(today_sql, (i, province, city, district,str(today_data)))
+                cursor1.execute(today_sql, (i, cityid, province, city, district,str(today_data)))
             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():
+                cityid = today_data.get("cityid", "")
                 server_sql = f"""
-                    INSERT OR REPLACE INTO `server_day_data` (`id`,`province`, `city`, `district`, `content`) VALUES (?, ?, ?, ?, ?)
+                    INSERT OR REPLACE INTO `server_day_data` (`id`,`cityid`,`province`, `city`, `district`, `content`) VALUES (?,?, ?, ?, ?, ?)
                 """
-                cursor1.execute(server_sql, (i, province, city, district,str(server_day_data),))
+                cursor1.execute(server_sql, (i, cityid, province, city, district,str(server_day_data),))
             conn1.commit()
         except Exception as e:
             print(e)