|
@@ -6,7 +6,7 @@ import datetime
|
|
import geoip2.webservice
|
|
import geoip2.webservice
|
|
import requests
|
|
import requests
|
|
|
|
|
|
-from Model.models import IPAddr
|
|
|
|
|
|
+from Model.models import IPAddr, OpenWeatherMapCallCount
|
|
from Object.RedisObject import RedisObject
|
|
from Object.RedisObject import RedisObject
|
|
from Service.CommonService import CommonService
|
|
from Service.CommonService import CommonService
|
|
from Ansjer.config import LOGGER
|
|
from Ansjer.config import LOGGER
|
|
@@ -174,7 +174,8 @@ class OpenWeatherMap:
|
|
# 查询缓存数据
|
|
# 查询缓存数据
|
|
today = datetime.datetime.today()
|
|
today = datetime.datetime.today()
|
|
now_time = datetime.datetime(today.year, today.month, today.day, today.hour)
|
|
now_time = datetime.datetime(today.year, today.month, today.day, today.hour)
|
|
- time_stamp = CommonService.str_to_timestamp(now_time.strftime('%Y-%m-%d %H:%M:%S'))
|
|
|
|
|
|
+ str_time = now_time.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
+ time_stamp = CommonService.str_to_timestamp(str_time)
|
|
key = 'weather:lat:{}_lon:{}_time_stamp:{}'.format(self.lat, self.lon, time_stamp)
|
|
key = 'weather:lat:{}_lon:{}_time_stamp:{}'.format(self.lat, self.lon, time_stamp)
|
|
|
|
|
|
redis_obj = RedisObject()
|
|
redis_obj = RedisObject()
|
|
@@ -182,15 +183,16 @@ class OpenWeatherMap:
|
|
if weather:
|
|
if weather:
|
|
temp, humidity = weather.split('/')
|
|
temp, humidity = weather.split('/')
|
|
else:
|
|
else:
|
|
- temp, humidity = self.get_current_weather()
|
|
|
|
|
|
+ temp, humidity = self.get_current_weather(str_time[:7])
|
|
if temp is not None and humidity is not None:
|
|
if temp is not None and humidity is not None:
|
|
key = 'weather:lat:{}_lon:{}_time_stamp:{}'.format(self.lat, self.lon, time_stamp)
|
|
key = 'weather:lat:{}_lon:{}_time_stamp:{}'.format(self.lat, self.lon, time_stamp)
|
|
redis_obj.set_ex_data(key, '{}/{}'.format(temp, humidity), 3600)
|
|
redis_obj.set_ex_data(key, '{}/{}'.format(temp, humidity), 3600)
|
|
return temp, humidity
|
|
return temp, humidity
|
|
|
|
|
|
- def get_current_weather(self):
|
|
|
|
|
|
+ def get_current_weather(self, month):
|
|
"""
|
|
"""
|
|
根据经纬度获取当前天气
|
|
根据经纬度获取当前天气
|
|
|
|
+ @param month: 年月份
|
|
@return: temp, humidity
|
|
@return: temp, humidity
|
|
"""
|
|
"""
|
|
url = 'https://api.openweathermap.org/data/2.5/weather'
|
|
url = 'https://api.openweathermap.org/data/2.5/weather'
|
|
@@ -201,6 +203,15 @@ class OpenWeatherMap:
|
|
'units': 'metric' # 公制单位,温度单位:摄氏度
|
|
'units': 'metric' # 公制单位,温度单位:摄氏度
|
|
}
|
|
}
|
|
res = requests.get(url=url, params=params, timeout=10)
|
|
res = requests.get(url=url, params=params, timeout=10)
|
|
|
|
+
|
|
|
|
+ # 记录调用次数
|
|
|
|
+ open_weather_map_call_count_qs = OpenWeatherMapCallCount.objects.filter(month=month).values('count')
|
|
|
|
+ if not open_weather_map_call_count_qs.exists():
|
|
|
|
+ OpenWeatherMapCallCount.objects.create(month=month)
|
|
|
|
+ else:
|
|
|
|
+ count = open_weather_map_call_count_qs[0]['count'] + 1
|
|
|
|
+ open_weather_map_call_count_qs.update(count=count)
|
|
|
|
+
|
|
if res.status_code != 200:
|
|
if res.status_code != 200:
|
|
return None, None
|
|
return None, None
|
|
res_data = eval(res.text)
|
|
res_data = eval(res.text)
|