IPWeatherObject.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # @Author : Rocky
  2. # @File : IPWeatherObject.py
  3. # @Time : 2023/8/16 8:56
  4. import requests
  5. from Model.models import IPAddr
  6. from Object.RedisObject import RedisObject
  7. class IPQuery:
  8. """
  9. 阿里云IP地址查询
  10. https://market.aliyun.com/products/57002003/cmapi021970.html?spm=5176.2020520132.recommend-card.dbottom-suggestion.33e17218GYjWDt#sku=yuncode15970000020
  11. """
  12. def __init__(self, ip):
  13. self.appcode = 'd7d63b34b1d54214be446608a57ff0a2'
  14. self.host = 'https://c2ba.api.huachen.cn'
  15. self.path = '/ip'
  16. self.district = '' # 区
  17. self.city = '' # 市
  18. self.region = '' # 省/州
  19. self.country_id = ''
  20. param = 'ip=' + ip
  21. url = self.host + self.path + '?' + param
  22. # 获取ip的区级和城市信息
  23. headers = {'Authorization': 'APPCODE ' + self.appcode}
  24. res = requests.get(url=url, headers=headers)
  25. if res.status_code == 200:
  26. # 反序列化响应数据
  27. res_data = eval(res.content.decode('utf-8'))
  28. if res_data['ret'] == 200:
  29. district = res_data['data']['district']
  30. city = res_data['data']['city']
  31. region = res_data['data']['region']
  32. country_id = res_data['data']['country_id']
  33. # 国内城市数据不为空字符,拼上'市'字
  34. if country_id == 'CN' and city != '':
  35. city += '市'
  36. # ip地址信息存表
  37. IPAddr.objects.create(ip=ip, district=district, city=city, region=region, country_code=country_id)
  38. # 确定天气城市
  39. if district != '':
  40. self.district = district
  41. elif city != '':
  42. self.district = city
  43. self.country_id = country_id
  44. class WeatherInfo:
  45. def __init__(self, city_id):
  46. self.appcode = 'd7d63b34b1d54214be446608a57ff0a2'
  47. self.headers = {'Authorization': 'APPCODE ' + self.appcode,
  48. 'Content-Type': 'application/x-www-form-urlencoded'}
  49. self.city_id = city_id
  50. def get_city_weather(self):
  51. url = "https://aliv18.data.moji.com/whapi/json/alicityweather/condition" # 获取实时天气
  52. data = {'cityId': self.city_id}
  53. response = requests.post(url, headers=self.headers, data=data, verify=False)
  54. if response.status_code == 200:
  55. result = response.json()
  56. if result['code'] == 0:
  57. # 返回温湿度
  58. return result['data']['condition']['temp'], result['data']['condition']['humidity']
  59. return None, None
  60. def get_city_24_weather(self):
  61. url = 'https://aliv18.data.moji.com/whapi/json/alicityweather/forecast24hours' # 获取城市24小时天气
  62. data = {'cityId': self.city_id}
  63. response = requests.post(url, headers=self.headers, data=data, verify=False)
  64. redis_obj = RedisObject()
  65. count = redis_obj.get_data('weather_count')
  66. if count:
  67. redis_obj.set_data('weather_count', int(count) + 1)
  68. else:
  69. redis_obj.set_data('weather_count', 1)
  70. if response.status_code == 200:
  71. result = response.json()
  72. if result['code'] == 0:
  73. # 返回天气列表
  74. return result['data']['hourly']
  75. return None