crond_weather.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import requests
  2. import sqlite3
  3. import json
  4. import time
  5. import datetime
  6. from weather import all_city
  7. app_id = "69334222"
  8. app_secret = "2u4bHXHD"
  9. province_dict = {
  10. "11": '北京市',
  11. "12": '天津市',
  12. "13": '河北省',
  13. "14": '山西省',
  14. "15": '内蒙古自治区',
  15. "21": '辽宁省',
  16. "22": '吉林省',
  17. "23": '黑龙江省',
  18. "31": '上海市',
  19. "32": '江苏省',
  20. "33": '浙江省',
  21. "34": '安徽省',
  22. "35": '福建省',
  23. "36": '江西省',
  24. "37": '山东省',
  25. "41": '河南省',
  26. "42": '湖北省',
  27. "43": '湖南省',
  28. "44": '广东省',
  29. "45": '广西壮族自治区',
  30. "46": '海南省',
  31. "50": '重庆市',
  32. "51": '四川省',
  33. "52": '贵州省',
  34. "53": '云南省',
  35. "54": '西藏自治区',
  36. "61": '陕西省',
  37. "62": '甘肃省',
  38. "63": '青海省',
  39. "64": '宁夏回族自治区',
  40. "65": '新疆维吾尔自治区'
  41. }
  42. db = "/data/weather/weather.db"
  43. def init_data():
  44. # 导入全部的城市
  45. conn = sqlite3.connect(db)
  46. cursor = conn.cursor()
  47. cursor.execute(
  48. """
  49. CREATE TABLE IF NOT EXISTS `district` (
  50. `id` bigint(20) NOT NULL,
  51. `city` varchar(50) NOT NULL DEFAULT '',
  52. `pid` bigint(20) NOT NULL
  53. );
  54. """
  55. )
  56. cursor.execute(
  57. """
  58. CREATE TABLE IF NOT EXISTS `day_data` (
  59. `id` INTEGER PRIMARY KEY,
  60. `province` varchar(50) NOT NULL,
  61. `city` varchar(50) NOT NULL DEFAULT '',
  62. `district` varchar(50) NOT NULL DEFAULT '',
  63. `content` TEXT NULL DEFAULT ''
  64. );
  65. """
  66. )
  67. cursor.execute(
  68. """
  69. CREATE TABLE IF NOT EXISTS `server_day_data` (
  70. `id` INTEGER PRIMARY KEY,
  71. `province` varchar(50) NOT NULL,
  72. `city` varchar(50) NOT NULL DEFAULT '',
  73. `district` varchar(50) NOT NULL DEFAULT '',
  74. `content` TEXT NULL DEFAULT ''
  75. );
  76. """
  77. )
  78. sql = """
  79. select * from district where id > 65
  80. """
  81. cursor.execute(sql)
  82. results = cursor.fetchall()
  83. if not results:
  84. print("填充原始数据")
  85. for k in all_city:
  86. sql = """
  87. INSERT INTO `district` (`id`, `city`, `pid`) VALUES (?, ?, ?)
  88. """
  89. cursor.execute(sql, (k[0], k[1], k[2], ))
  90. conn.commit()
  91. conn.close()
  92. def main():
  93. init_data()
  94. special_time = [0, 8, 16, 17, 15]
  95. while True:
  96. now = datetime.datetime.now()
  97. hour = int(now.hour)
  98. print(hour, "--------------")
  99. if hour in special_time:
  100. conn1 = sqlite3.connect(db)
  101. cursor1 = conn1.cursor()
  102. sql = """
  103. select * from district where id > 65
  104. """
  105. cursor1.execute(sql)
  106. results = cursor1.fetchall()
  107. # 更新数据
  108. for i, row in enumerate(results):
  109. temp = row[1]
  110. id = row[0]
  111. pid = row[2]
  112. if len(str(pid)) == 2:
  113. city = temp
  114. district = ""
  115. else:
  116. if pid == 620500000000:
  117. print(pid, '=================================')
  118. cursor1.execute('select city from district where id = ?', (pid,))
  119. te = cursor1.fetchone()
  120. city = te[0]
  121. district = temp
  122. print(district, city, "-------%%%%%%%%%%%-------")
  123. else:
  124. print(pid)
  125. cursor1.execute('select city from district where id = ?', (pid,))
  126. te = cursor1.fetchone()
  127. city = te[0]
  128. district = temp
  129. print(district, city, "--------------")
  130. province_id = (str(row[0]))[:2]
  131. province = province_dict.get(province_id)
  132. if district == '唐河县' or city == "郑州市" or city == "市辖区":
  133. today_url = f"https://v0.yiketianqi.com/api?unescape=1&version=v62&appid={app_id}&appsecret={app_secret}&adcode={id}"
  134. today_response = requests.get(today_url)
  135. today_data = json.loads(today_response.text)
  136. if "errcode" not in today_data.keys():
  137. today_sql = """
  138. INSERT OR REPLACE INTO `day_data` (`id`,`province`, `city`, `district`, content) VALUES (?, ?, ?, ?, ?)
  139. """
  140. cursor1.execute(today_sql, (i, province, city, district,str(today_data)))
  141. print(city, district, province, "---------------------")
  142. if hour in special_time:
  143. server_day_url = f"https://v0.yiketianqi.com/api?unescape=1&version=v91&appid={app_id}&appsecret={app_secret}&adcode={id}"
  144. server_day_response = requests.get(server_day_url)
  145. server_day_data = json.loads(server_day_response.text)
  146. if "errcode" not in server_day_data.keys():
  147. server_sql = f"""
  148. INSERT OR REPLACE INTO `server_day_data` (`id`,`province`, `city`, `district`, `content`) VALUES (?, ?, ?, ?, ?)
  149. """
  150. cursor1.execute(server_sql, (i, province, city, district,str(server_day_data),))
  151. print(city, district, province, "+++++++++++++++++++++")
  152. conn1.commit()
  153. conn1.close()
  154. print("start sleep...")
  155. time.sleep(3600)
  156. if __name__ == "__main__":
  157. main()