Selaa lähdekoodia

记录OpenWeatherMap调用次数

locky 1 vuosi sitten
vanhempi
commit
c79acb8b42
2 muutettua tiedostoa jossa 25 lisäystä ja 4 poistoa
  1. 10 0
      Model/models.py
  2. 15 4
      Object/IPWeatherObject.py

+ 10 - 0
Model/models.py

@@ -4342,6 +4342,16 @@ class IPAddr(models.Model):
         verbose_name = 'ip地址信息'
 
 
+class OpenWeatherMapCallCount(models.Model):
+    id = models.AutoField(primary_key=True, verbose_name='主键')
+    month = models.CharField(default='', max_length=20, verbose_name='月份')
+    count = models.IntegerField(default=1, verbose_name='api调用次数')
+
+    class Meta:
+        db_table = 'open_weather_map_call_count'
+        verbose_name = 'OpenWeatherMap调用次数'
+
+
 class DeviceLiveRestrict(models.Model):
     id = models.AutoField(primary_key=True, verbose_name='主键')
     device_type = models.IntegerField(default=0, verbose_name='设备类型id')

+ 15 - 4
Object/IPWeatherObject.py

@@ -6,7 +6,7 @@ import datetime
 import geoip2.webservice
 import requests
 
-from Model.models import IPAddr
+from Model.models import IPAddr, OpenWeatherMapCallCount
 from Object.RedisObject import RedisObject
 from Service.CommonService import CommonService
 from Ansjer.config import LOGGER
@@ -174,7 +174,8 @@ class OpenWeatherMap:
         # 查询缓存数据
         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'))
+        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)
 
         redis_obj = RedisObject()
@@ -182,15 +183,16 @@ class OpenWeatherMap:
         if weather:
             temp, humidity = weather.split('/')
         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:
                 key = 'weather:lat:{}_lon:{}_time_stamp:{}'.format(self.lat, self.lon, time_stamp)
                 redis_obj.set_ex_data(key, '{}/{}'.format(temp, humidity), 3600)
         return temp, humidity
 
-    def get_current_weather(self):
+    def get_current_weather(self, month):
         """
         根据经纬度获取当前天气
+        @param month: 年月份
         @return: temp, humidity
         """
         url = 'https://api.openweathermap.org/data/2.5/weather'
@@ -201,6 +203,15 @@ class OpenWeatherMap:
             'units': 'metric'   # 公制单位,温度单位:摄氏度
         }
         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:
             return None, None
         res_data = eval(res.text)