# @Author : Rocky # @File : IPWeatherObject.py # @Time : 2023/8/16 8:56 import geoip2.webservice import requests from Model.models import IPAddr class IPQuery: """ 阿里云IP地址查询 https://market.aliyun.com/products/57002003/cmapi021970.html?spm=5176.2020520132.recommend-card.dbottom-suggestion.33e17218GYjWDt#sku=yuncode15970000020 """ def __init__(self, ip): self.appcode = 'd7d63b34b1d54214be446608a57ff0a2' self.host = 'https://c2ba.api.huachen.cn' self.path = '/ip' self.district = '' # 区 self.city = '' # 市 self.region = '' # 省/州 self.country_id = '' param = 'ip=' + ip url = self.host + self.path + '?' + param # 获取ip的区级和城市信息 headers = {'Authorization': 'APPCODE ' + self.appcode} res = requests.get(url=url, headers=headers) if res.status_code == 200: # 反序列化响应数据 res_data = eval(res.content.decode('utf-8')) if res_data['ret'] == 200: district = res_data['data']['district'] city = res_data['data']['city'] region = res_data['data']['region'] country_id = res_data['data']['country_id'] # 国内城市数据不为空字符,拼上'市'字 if country_id == 'CN' and city != '': city += '市' # ip地址信息存表或更新 ip_addr_qs = IPAddr.objects.filter(ip=ip) if ip_addr_qs.exists(): ip_addr_qs.update(district=district, city=city, region=region, country_code=country_id) else: IPAddr.objects.create(ip=ip, district=district, city=city, region=region, country_code=country_id) # 确定天气城市 if district != '': self.district = district elif city != '': self.district = city self.country_id = country_id class WeatherInfo: """ 阿里云墨迹天气服务 https://market.aliyun.com/products/57096001/cmapi013828.html?spm=5176.2020520132.101.19.2b8f7218NuiGPd#sku=yuncode782800000 """ def __init__(self, city_id): self.appcode = 'd7d63b34b1d54214be446608a57ff0a2' self.headers = {'Authorization': 'APPCODE ' + self.appcode, 'Content-Type': 'application/x-www-form-urlencoded'} self.city_id = city_id def get_city_weather(self): url = "https://aliv18.data.moji.com/whapi/json/alicityweather/condition" # 获取实时天气 data = {'cityId': self.city_id} response = requests.post(url, headers=self.headers, data=data, verify=False) if response.status_code == 200: result = response.json() if result['code'] == 0: # 返回温湿度 return result['data']['condition']['temp'], result['data']['condition']['humidity'] return None, None def get_city_24_weather(self): url = 'https://aliv18.data.moji.com/whapi/json/alicityweather/forecast24hours' # 获取城市24小时天气 data = {'cityId': self.city_id} response = requests.post(url, headers=self.headers, data=data, verify=False) if response.status_code == 200: result = response.json() if result['code'] == 0: # 返回天气列表 return result['data']['hourly'] return None class GeoIP2: """ MaxMind GeoIP2查询国外ip https://www.maxmind.com/ """ def __init__(self, ip): self.account_id = 0 self.license_key = '' with geoip2.webservice.Client(self.account_id, self.license_key) as client: # You can also use `client.city` or `client.insights` # `client.insights` is not available to GeoLite2 users response = client.country(ip) print(response.country.iso_code) class OpenWeatherMap: """ OpenWeatherMap查询国外天气服务 https://openweathermap.org/ """ def __init__(self, lat, lon): self.appid = '7a6cd7dfeb034ededa451ed575788857' self.lat = lat # 纬度 self.lon = lon # 经度 def get_current_weather(self): """ 根据经纬度获取当前天气 @return: temp, humidity """ url = 'https://api.openweathermap.org/data/2.5/weather' params = { 'lat': self.lat, 'lon': self.lon, 'appid': self.appid, 'units': 'metric' # 公制单位,温度单位:摄氏度 } res = requests.get(url=url, params=params, timeout=10) if res.status_code != 200: return None, None res_data = eval(res.text) if res_data['cod'] != 200: return None, None temp = res_data['main']['temp'] humidity = res_data['main']['humidity'] return temp, humidity