|
@@ -1,6 +1,7 @@
|
|
|
# @Author : Rocky
|
|
|
# @File : IPWeatherObject.py
|
|
|
# @Time : 2023/8/16 8:56
|
|
|
+import geoip2.webservice
|
|
|
import requests
|
|
|
|
|
|
from Model.models import IPAddr
|
|
@@ -58,6 +59,10 @@ class IPQuery:
|
|
|
|
|
|
|
|
|
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,
|
|
@@ -85,3 +90,53 @@ class WeatherInfo:
|
|
|
# 返回天气列表
|
|
|
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
|