IPWeatherObject.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # @Author : Rocky
  2. # @File : IPWeatherObject.py
  3. # @Time : 2023/8/16 8:56
  4. import geoip2.webservice
  5. import requests
  6. from Model.models import IPAddr
  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. ip_addr_qs = IPAddr.objects.filter(ip=ip)
  38. if ip_addr_qs.exists():
  39. ip_addr_qs.update(district=district, city=city, region=region, country_code=country_id)
  40. else:
  41. IPAddr.objects.create(ip=ip, district=district, city=city, region=region, country_code=country_id)
  42. # 确定天气城市
  43. if district != '':
  44. self.district = district
  45. elif city != '':
  46. self.district = city
  47. self.country_id = country_id
  48. class WeatherInfo:
  49. """
  50. 阿里云墨迹天气服务
  51. https://market.aliyun.com/products/57096001/cmapi013828.html?spm=5176.2020520132.101.19.2b8f7218NuiGPd#sku=yuncode782800000
  52. """
  53. def __init__(self, city_id):
  54. self.appcode = 'd7d63b34b1d54214be446608a57ff0a2'
  55. self.headers = {'Authorization': 'APPCODE ' + self.appcode,
  56. 'Content-Type': 'application/x-www-form-urlencoded'}
  57. self.city_id = city_id
  58. def get_city_weather(self):
  59. url = "https://aliv18.data.moji.com/whapi/json/alicityweather/condition" # 获取实时天气
  60. data = {'cityId': self.city_id}
  61. response = requests.post(url, headers=self.headers, data=data, verify=False)
  62. if response.status_code == 200:
  63. result = response.json()
  64. if result['code'] == 0:
  65. # 返回温湿度
  66. return result['data']['condition']['temp'], result['data']['condition']['humidity']
  67. return None, None
  68. def get_city_24_weather(self):
  69. url = 'https://aliv18.data.moji.com/whapi/json/alicityweather/forecast24hours' # 获取城市24小时天气
  70. data = {'cityId': self.city_id}
  71. response = requests.post(url, headers=self.headers, data=data, verify=False)
  72. if response.status_code == 200:
  73. result = response.json()
  74. if result['code'] == 0:
  75. # 返回天气列表
  76. return result['data']['hourly']
  77. return None
  78. class GeoIP2:
  79. """
  80. MaxMind GeoIP2查询国外ip
  81. https://www.maxmind.com/
  82. """
  83. def __init__(self, ip):
  84. self.account_id = 0
  85. self.license_key = ''
  86. with geoip2.webservice.Client(self.account_id, self.license_key) as client:
  87. # You can also use `client.city` or `client.insights`
  88. # `client.insights` is not available to GeoLite2 users
  89. response = client.country(ip)
  90. print(response.country.iso_code)
  91. class OpenWeatherMap:
  92. """
  93. OpenWeatherMap查询国外天气服务
  94. https://openweathermap.org/
  95. """
  96. def __init__(self, lat, lon):
  97. self.appid = '7a6cd7dfeb034ededa451ed575788857'
  98. self.lat = lat # 纬度
  99. self.lon = lon # 经度
  100. def get_current_weather(self):
  101. """
  102. 根据经纬度获取当前天气
  103. @return: temp, humidity
  104. """
  105. url = 'https://api.openweathermap.org/data/2.5/weather'
  106. params = {
  107. 'lat': self.lat,
  108. 'lon': self.lon,
  109. 'appid': self.appid,
  110. 'units': 'metric' # 公制单位,温度单位:摄氏度
  111. }
  112. res = requests.get(url=url, params=params, timeout=10)
  113. if res.status_code != 200:
  114. return None, None
  115. res_data = eval(res.text)
  116. if res_data['cod'] != 200:
  117. return None, None
  118. temp = res_data['main']['temp']
  119. humidity = res_data['main']['humidity']
  120. return temp, humidity