apply_weather.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from rest_framework.views import APIView
  2. from rest_framework.response import Response
  3. import sqlite3
  4. import logging
  5. import json
  6. province_dict = {
  7. "11": '北京市',
  8. "12": '天津市',
  9. "13": '河北省',
  10. "14": '山西省',
  11. "15": '内蒙古自治区',
  12. "21": '辽宁省',
  13. "22": '吉林省',
  14. "23": '黑龙江省',
  15. "31": '上海市',
  16. "32": '江苏省',
  17. "33": '浙江省',
  18. "34": '安徽省',
  19. "35": '福建省',
  20. "36": '江西省',
  21. "37": '山东省',
  22. "41": '河南省',
  23. "42": '湖北省',
  24. "43": '湖南省',
  25. "44": '广东省',
  26. "45": '广西壮族自治区',
  27. "46": '海南省',
  28. "50": '重庆市',
  29. "51": '四川省',
  30. "52": '贵州省',
  31. "53": '云南省',
  32. "54": '西藏自治区',
  33. "61": '陕西省',
  34. "62": '甘肃省',
  35. "63": '青海省',
  36. "64": '宁夏回族自治区',
  37. "65": '新疆维吾尔自治区'
  38. }
  39. logging.basicConfig(level=logging.DEBUG, filename='/data/logs/app.log', filemode='w',
  40. format='%(asctime)s - %(levelname)s - %(message)s')
  41. class GetWeather(APIView):
  42. def post(self, request):
  43. # 对外提供天气接口,需要提供省,市,区(县)
  44. try:
  45. db = "/data/weather/weather.db"
  46. data = request.data
  47. cityid = data.get("cityid", "")
  48. province = data.get("province")
  49. city = data.get("city")
  50. district = data.get("district")
  51. day_type = data.get("day_type")
  52. conn = sqlite3.connect(db)
  53. cursor = conn.cursor()
  54. username = data.get("username")
  55. password = data.get("password")
  56. sql = """
  57. select * from user where username = ? and password = ?
  58. """
  59. cursor.execute(sql, (username, password,))
  60. user = cursor.fetchone()
  61. if not user:
  62. return Response({"msg":"认证失败", "code": 400})
  63. table = "day_data" if day_type == "1" else "serven_day_data"
  64. table = "day_data" if day_type == "1" else "server_day_data"
  65. if cityid:
  66. sql = f"select content from {table} where cityid = '{cityid}'"
  67. cursor.execute(sql)
  68. result = cursor.fetchone()
  69. if result:
  70. temp = result[0].replace("'", '"')
  71. result = json.loads(temp)
  72. return Response({"content": result, "msg": "success", "code": 200})
  73. else:
  74. return Response({"msg": "请联系管理员排查", "code": 500})
  75. else:
  76. sql = f"select content from {table} where province = '{province}' and city ='{city}' and district ='{district}'"
  77. cursor.execute(sql)
  78. result = cursor.fetchone()
  79. if not result:
  80. # 省市正确
  81. sql = f"select content from {table} where province= '{province}' and city = '{city}'"
  82. cursor.execute(sql)
  83. result = cursor.fetchone()
  84. if not result:
  85. # 省正确
  86. sql = f"select content from {table} where province = '{province}'"
  87. cursor.execute(sql)
  88. result = cursor.fetchone()
  89. if result:
  90. try:
  91. temp = result[0].replace("'", '"')
  92. result = json.loads(temp)
  93. return Response({"content": result, "msg": "success", "code": 200})
  94. except Exception as e:
  95. logging.info(e)
  96. return Response({"msg": "请联系管理员", "code": "50001"})
  97. else:
  98. return Response({"content": "", "msg": "success", "code": 500})
  99. except Exception as e:
  100. logging.info(e)
  101. return Response({"msg": "请联系管理员", "code": "50001"})
  102. class TestAPI(APIView):
  103. def post(self, request):
  104. return Response({"test": 111})