IPWeatherObject.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # @Author : Rocky
  2. # @File : IPWeatherObject.py
  3. # @Time : 2023/8/16 8:56
  4. import requests
  5. class IPQuery:
  6. """
  7. 阿里云IP地址查询
  8. https://market.aliyun.com/products/57002003/cmapi021970.html?spm=5176.2020520132.recommend-card.dbottom-suggestion.33e17218GYjWDt#sku=yuncode15970000020
  9. """
  10. def __init__(self, ip):
  11. self.appcode = 'd7d63b34b1d54214be446608a57ff0a2'
  12. self.host = 'https://c2ba.api.huachen.cn'
  13. self.path = '/ip'
  14. self.district = None
  15. param = 'ip=' + ip
  16. url = self.host + self.path + '?' + param
  17. # 获取ip的区级和城市信息
  18. headers = {'Authorization': 'APPCODE ' + self.appcode}
  19. res = requests.get(url=url, headers=headers)
  20. if res.status_code == 200:
  21. # 反序列化响应数据
  22. res_data = eval(res.content.decode('utf-8'))
  23. if res_data['ret'] == 200:
  24. district = res_data['data']['district']
  25. city = res_data['data']['city'] + '市'
  26. self.district = district if district else city
  27. class WeatherInfo:
  28. def __init__(self, city_id):
  29. self.appcode = 'd7d63b34b1d54214be446608a57ff0a2'
  30. self.headers = {'Authorization': 'APPCODE ' + self.appcode,
  31. 'Content-Type': 'application/x-www-form-urlencoded'}
  32. self.city_id = city_id
  33. def get_city_weather(self):
  34. url = "https://aliv18.data.moji.com/whapi/json/alicityweather/condition" # 获取实时天气
  35. data = {'cityId': self.city_id}
  36. response = requests.post(url, headers=self.headers, data=data, verify=False)
  37. if response.status_code == 200:
  38. result = response.json()
  39. if result['code'] == 0:
  40. # 返回温湿度
  41. return result['data']['condition']['temp'], result['data']['condition']['humidity']
  42. return None, None