|
|
@@ -2,10 +2,15 @@ import requests
|
|
|
import sqlite3
|
|
|
import json
|
|
|
import os
|
|
|
-import datetime
|
|
|
+import sys
|
|
|
from weather import all_city
|
|
|
import logging
|
|
|
-
|
|
|
+import django
|
|
|
+BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
+sys.path.append(BASE_DIR)
|
|
|
+os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bigdataAPI.settings')
|
|
|
+django.setup()
|
|
|
+from apps.Weather.models import District, DayData, ServerDayData
|
|
|
# 配置日志级别和格式
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
@@ -61,62 +66,21 @@ province_dict = {
|
|
|
db = "/data/weather/weather.db"
|
|
|
def init_data():
|
|
|
# 导入全部的城市
|
|
|
- conn = sqlite3.connect(db)
|
|
|
- cursor = conn.cursor()
|
|
|
-
|
|
|
-
|
|
|
- cursor.execute(
|
|
|
- """
|
|
|
- CREATE TABLE IF NOT EXISTS `district` (
|
|
|
- `id` bigint(20) NOT NULL,
|
|
|
- `city` varchar(50) NOT NULL DEFAULT '',
|
|
|
- `pid` bigint(20) NOT NULL
|
|
|
- );
|
|
|
- """
|
|
|
- )
|
|
|
- cursor.execute(
|
|
|
- """
|
|
|
- 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 '',
|
|
|
- `content` TEXT NULL DEFAULT ''
|
|
|
- );
|
|
|
- """
|
|
|
- )
|
|
|
-
|
|
|
- cursor.execute(
|
|
|
- """
|
|
|
- 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 '',
|
|
|
- `content` TEXT NULL DEFAULT ''
|
|
|
- );
|
|
|
- """
|
|
|
- )
|
|
|
-
|
|
|
- sql = """
|
|
|
- select * from district where id > 65
|
|
|
- """
|
|
|
- cursor.execute(sql)
|
|
|
- results = cursor.fetchall()
|
|
|
- if not results:
|
|
|
- print("填充原始数据")
|
|
|
- for k in all_city:
|
|
|
- sql = """
|
|
|
- INSERT INTO `district` (`id`, `city`, `pid`) VALUES (?, ?, ?)
|
|
|
- """
|
|
|
- cursor.execute(sql, (k[0], k[1], k[2], ))
|
|
|
- conn.commit()
|
|
|
- conn.close()
|
|
|
+ for k in all_city:
|
|
|
+ District.objects.get_or_create(
|
|
|
+ id=k[0],
|
|
|
+ defaults={
|
|
|
+ "id": k[0],
|
|
|
+ "city": k[1],
|
|
|
+ "pid": k[2]
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
|
|
|
def main():
|
|
|
- init_data()
|
|
|
+ district = District.objects.all()
|
|
|
+ if not district:
|
|
|
+ init_data()
|
|
|
logging.warning("up...")
|
|
|
conn1 = sqlite3.connect(db)
|
|
|
cursor1 = conn1.cursor()
|
|
|
@@ -125,49 +89,59 @@ def main():
|
|
|
"""
|
|
|
cursor1.execute(sql)
|
|
|
results = cursor1.fetchall()
|
|
|
+
|
|
|
+ results = District.objects.filter(id__gt = 65)
|
|
|
# 更新数据
|
|
|
- for i, row in enumerate(results):
|
|
|
- temp = row[1] # 东城区
|
|
|
- id = row[0] # 110101000000
|
|
|
- pid = row[2] # 110100000000
|
|
|
+ for row in results:
|
|
|
+ temp = row.city # 东城区
|
|
|
+ id = row.id # 110101000000
|
|
|
+ pid = row.pid # 110100000000
|
|
|
if len(str(pid)) == 2:
|
|
|
# 处理 省 市 样例
|
|
|
city = temp
|
|
|
district = ""
|
|
|
else:
|
|
|
- # 其它
|
|
|
- cursor1.execute('select city from district where id = ?', (pid,))
|
|
|
- te = cursor1.fetchone()
|
|
|
+ te = District.objects.filter(pid=pid).first()
|
|
|
city = te[0]
|
|
|
district = temp
|
|
|
if city == "市辖区":
|
|
|
city = district
|
|
|
district = ""
|
|
|
- province_id = (str(row[0]))[:2]
|
|
|
+ province_id = (str(row.id))[:2]
|
|
|
province = province_dict.get(province_id)
|
|
|
try:
|
|
|
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_response = requests.get(today_url, timeout=5)
|
|
|
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`, `cityid`, `province`, `city`, `district`, content) VALUES (?,?, ?, ?, ?, ?)
|
|
|
- """
|
|
|
- cursor1.execute(today_sql, (i, cityid, province, city, district,str(today_data)))
|
|
|
+ DayData.objects.update_or_create(
|
|
|
+ cityid=cityid,
|
|
|
+ defaults={
|
|
|
+ "cityid": cityid,
|
|
|
+ "province": province,
|
|
|
+ "city": city,
|
|
|
+ "district": district,
|
|
|
+ "content": str(today_data)
|
|
|
+ }
|
|
|
+ )
|
|
|
server_day_url = f"https://v0.yiketianqi.com/api?unescape=1&version=v91&appid={app_id}&appsecret={app_secret}&adcode={id}&ext=hours,aqi,life"
|
|
|
- server_day_response = requests.get(server_day_url)
|
|
|
+ server_day_response = requests.get(server_day_url, timeout=5)
|
|
|
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`,`cityid`,`province`, `city`, `district`, `content`) VALUES (?,?, ?, ?, ?, ?)
|
|
|
- """
|
|
|
- cursor1.execute(server_sql, (i, cityid, province, city, district,str(server_day_data),))
|
|
|
- conn1.commit()
|
|
|
+ ServerDayData.objects.update_or_create(
|
|
|
+ cityid=cityid,
|
|
|
+ defaults={
|
|
|
+ "cityid": cityid,
|
|
|
+ "province": province,
|
|
|
+ "city": city,
|
|
|
+ "district": district,
|
|
|
+ "content": str(today_data)
|
|
|
+ }
|
|
|
+ )
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
- conn1.close()
|
|
|
logging.warning("over...")
|
|
|
|
|
|
|