Browse Source

获取实时天气

peng 2 years ago
parent
commit
021542c0aa
1 changed files with 22 additions and 0 deletions
  1. 22 0
      Object/IPQueryObject.py

+ 22 - 0
Object/IPQueryObject.py

@@ -1,6 +1,8 @@
 # @Author    : Rocky
 # @File      : IPQueryObject.py
 # @Time      : 2023/8/16 8:56
+import ssl
+import urllib
 import requests
 
 
@@ -9,6 +11,7 @@ class IPQuery:
     阿里云IP地址查询
     https://market.aliyun.com/products/57002003/cmapi021970.html?spm=5176.2020520132.recommend-card.dbottom-suggestion.33e17218GYjWDt#sku=yuncode15970000020
     """
+
     def __init__(self, ip):
         self.appcode = 'd7d63b34b1d54214be446608a57ff0a2'
         self.host = 'https://c2ba.api.huachen.cn'
@@ -28,3 +31,22 @@ class IPQuery:
             res_data = eval(res.content.decode('utf-8'))
             if res_data['ret'] == 200:
                 self.district, self.city = res_data['data']['district'], res_data['data']['city']
+
+
+class WeatherInfo:
+    def __init__(self, city_id):
+        self.appcode = 'd7d63b34b1d54214be446608a57ff0a2'
+        self.headers = {'Authorization': 'APPCODE ' + self.appcode,
+                        'Content-Type': 'application/x-www-form-urlencoded'}
+        self.city_id = city_id
+
+    def get_city_weather(self):
+        url = "https://aliv18.data.moji.com/whapi/json/alicityweather/condition"  # 获取实时天气
+        data = {'cityId': self.city_id}
+        response = requests.post(url, headers=self.headers, data=data, verify=False)
+        if response.status_code == 200:
+            result = response.json()
+            if result['code'] == 0:
+                # 返回温湿度
+                return result['data']['condition']['temp'], result['data']['condition']['humidity']
+        return False