| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- from rest_framework.views import APIView
- from rest_framework.response import Response
- import sqlite3
- province_dict = {
- "11": '北京市',
- "12": '天津市',
- "13": '河北省',
- "14": '山西省',
- "15": '内蒙古自治区',
- "21": '辽宁省',
- "22": '吉林省',
- "23": '黑龙江省',
- "31": '上海市',
- "32": '江苏省',
- "33": '浙江省',
- "34": '安徽省',
- "35": '福建省',
- "36": '江西省',
- "37": '山东省',
- "41": '河南省',
- "42": '湖北省',
- "43": '湖南省',
- "44": '广东省',
- "45": '广西壮族自治区',
- "46": '海南省',
- "50": '重庆市',
- "51": '四川省',
- "52": '贵州省',
- "53": '云南省',
- "54": '西藏自治区',
- "61": '陕西省',
- "62": '甘肃省',
- "63": '青海省',
- "64": '宁夏回族自治区',
- "65": '新疆维吾尔自治区'
- }
- class GetWeather(APIView):
- def post(self, request):
- # 对外提供天气接口,需要提供省,市,区(县)
- try:
- db = "/data/weather/weather.db"
- data = request.data
- province = data.get("province")
- city = data.get("city")
- district = data.get("district")
- day_type = data.get("day_type")
- conn = sqlite3.connect(db)
- cursor = conn.cursor()
- table = "day_data" if day_type == "1" else "serven_day_data"
- sql = """
- select content from ? where province= ? and city = ? and district = ?
- """
- cursor.execute(sql, (table, province, city, district, ))
- result = cursor.fetchone()
- if not result:
- # 省市正确
- sql = """
- select content from ? where province= ? and city = ?
- """
- cursor.execute(sql, (table, province, city, ))
- result = cursor.fetchone()
- if not result:
- # 省正确
- sql = """
- select id from district where city = ?
- """
- cursor.execute(sql, province)
- provincd_id = cursor.fetchone()
- day_data_id = f"{provincd_id}0100000000"
- sql = """
- select content from ? where id = ?
- """
- cursor.execute(sql, (table, day_data_id, ))
- result = cursor.fetchone()
- return Response({"content": result})
- except Exception as e:
- print(e)
- return Response({"msg": "请联系管理员", "code": "50001"})
-
|