peng 1 жил өмнө
parent
commit
8b477eac6c

+ 5 - 11
Controller/WeatherControl.py

@@ -58,22 +58,16 @@ class WeatherView(View):
         today = datetime.datetime.today()
         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'))
-        temp, humidity = None, None
         try:
-            weather = redis_obj.get_hash_data('city_id_{}_weather'.format(city_id), time_stamp)
+            weather = redis_obj.get_data('city_id_{}_{}_weather'.format(city_id, time_stamp))
             if weather:
                 temp, humidity = weather.split('/')
             else:
                 city_obj = WeatherInfo(city_id)
-                weather_list = city_obj.get_city_24_weather()
-                if weather_list:
-                    temp = weather_list[0]['temp']
-                    humidity = weather_list[0]['humidity']
-                    for item in weather_list:
-                        temp_time = CommonService.str_to_timestamp(item['date'] + ' ' + item['hour'] + ':00:00')
-                        redis_obj.set_hash_data('city_id_{}_weather'.format(city_id),
-                                                {temp_time: '{}/{}'.format(item['temp'], item['humidity'])})
-                    redis_obj.set_expire('city_id_{}_weather'.format(city_id), 86400)
+                temp, humidity = city_obj.get_city_weather()
+                if temp and humidity:
+                    redis_obj.set_ex_data('city_id_{}_{}_weather'.format(city_id, time_stamp),
+                                          '{}/{}'.format(temp, humidity), 3600)
             if not all([temp, humidity]):
                 return response.json(10, '获取天气失败')
             return response.json(0, {'temp': temp, 'humidity': humidity})