peng пре 1 година
родитељ
комит
a7ff644dfd
2 измењених фајлова са 13 додато и 7 уклоњено
  1. 3 4
      Controller/WeatherControl.py
  2. 10 3
      Object/IPWeatherObject.py

+ 3 - 4
Controller/WeatherControl.py

@@ -62,6 +62,7 @@ class WeatherView(View):
             if weather:
                 temp, humidity = weather.split('/')
             else:
+                LOGGER.info('redis不存在天气:{}'.format('city_id_{}_{}_weather'.format(city_id, time_stamp)))
                 city_obj = WeatherInfo(city_id)
                 weather_list = city_obj.get_city_24_weather()
                 if weather_list:
@@ -69,10 +70,8 @@ class WeatherView(View):
                     humidity = weather_list[0]['humidity']
                     for item in weather_list:
                         temp_time = CommonService.str_to_timestamp(item['date'] + ' ' + item['hour'] + ':00:00')
-                        result = redis_obj.set_ex_data('city_id_{}_{}_weather'.format(city_id, temp_time),
-                                                       '{}/{}'.format(item['temp'], item['humidity']), 86400)
-                        if not result:
-                            LOGGER.info('写入天气失败:{}'.format(item))
+                        redis_obj.set_ex_data('city_id_{}_{}_weather'.format(city_id, temp_time),
+                                              '{}/{}'.format(item['temp'], item['humidity']), 86400)
             if not all([temp, humidity]):
                 return response.json(10, '获取天气失败')
             return response.json(0, {'temp': temp, 'humidity': humidity})

+ 10 - 3
Object/IPWeatherObject.py

@@ -4,6 +4,7 @@
 import requests
 
 from Model.models import IPAddr
+from Object.RedisObject import RedisObject
 
 
 class IPQuery:
@@ -18,8 +19,8 @@ class IPQuery:
         self.path = '/ip'
 
         self.district = ''  # 区
-        self.city = ''      # 市
-        self.region = ''    # 省/州
+        self.city = ''  # 市
+        self.region = ''  # 省/州
         self.country_id = ''
 
         param = 'ip=' + ip
@@ -75,9 +76,15 @@ class WeatherInfo:
         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)
+        redis_obj = RedisObject()
+        count = redis_obj.get_data('weather_count')
+        if count:
+            redis_obj.set_data('weather_count', int(count) + 1)
+        else:
+            redis_obj.set_data('weather_count', 1)
         if response.status_code == 200:
             result = response.json()
             if result['code'] == 0:
                 # 返回天气列表
                 return result['data']['hourly']
-        return None
+        return None