IPWeatherObject.py 3.3 KB

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