jack 2 luni în urmă
părinte
comite
95e07bc617
3 a modificat fișierele cu 67 adăugiri și 55 ștergeri
  1. 10 0
      gotify_config.json
  2. 4 55
      message/weather/get_one_week_weather.py
  3. 53 0
      utils/gotify.py

+ 10 - 0
gotify_config.json

@@ -0,0 +1,10 @@
+{
+  "base": "A8EVb0Cmxnb2vfk",
+  "coin": "AgfOJESqDKftBTQ",
+  "dlt": "A3bqt9Dlbs.fPUb",
+  "AirdropTasksNews": "Aoe0VKt-kkZnm8d",
+  "weather": "A9KF--mx_12PjSu",
+  "news": "AT2QGp_vyCX4akW",
+  "CheckAndRemind": "Aw7XKE2Ppk7Dgwk",
+  "test": "A0Xg6ZE5946iBYg"
+}

+ 4 - 55
message/weather/get_one_week_weather.py

@@ -5,10 +5,12 @@
 import os
 import sys
 import time
-from datetime import datetime
+import json
 from bs4 import BeautifulSoup
 import httpx
 
+from utils.gotify import GotifyNotifier
+
 sys.path.append(os.path.join(os.path.abspath(__file__).split('AutoInfo')[0] + 'AutoInfo'))
 SUB_PROJECT_NAME = "获取天气预报"
 PROJECT_PATH = os.path.join(os.path.abspath(__file__).split('AutoInfo')[0] + 'AutoInfo')
@@ -52,61 +54,8 @@ class Weather():
         print('天气预报数据已获取')
 
 
-class GotifyNotifier:
-    def __init__(self, title, message, token_name=''):
-        self.gotify_url = 'https://gotify.erhe.top'
-        self.app_token = self.match_token_name(token_name)
-        self.title = title
-        self.message = message
-
-    def match_token_name(self, name):
-        token_name_dict = {
-            'base': 'A8EVb0Cmxnb2vfk',
-            'coin': 'AgfOJESqDKftBTQ',
-            'dlt': 'A3bqt9Dlbs.fPUb',
-            'AirdropTasksNews': 'Aoe0VKt-kkZnm8d',
-            'weather': 'A9KF--mx_12PjSu',
-            'news': 'AT2QGp_vyCX4akW',
-            'CheckAndRemind': 'Aw7XKE2Ppk7Dgwk',
-            'test': 'A0Xg6ZE5946iBYg',
-        }
-
-        token = token_name_dict.get(name)
-        if token:
-            return token
-        else:
-            return token_name_dict['base']
-
-    def send_message(self):
-        # 构建POST请求的headers
-        headers = {
-            'Content-Type': 'application/json'
-        }
-
-        # 构建POST请求的body
-        body = {
-            'title': self.title,
-            'message': self.message
-        }
-
-        # 发送POST请求
-        with httpx.Client() as client:
-            response = client.post(
-                url=f"{self.gotify_url}/message?token={self.app_token}",
-                headers=headers,
-                json=body
-            )
-
-        # 或者可以使用 curl
-        # curl -k "https://gotify.erhe.top/message?token=A0Xg6ZE5946iBYg" -F "title=测试发送信息" -F "message=假装有信息,测试发送" -F "priority=5"
 
-        # 检查响应状态码
-        if response.status_code == 200:
-            print('Gotify Message sent successfully!')
-        else:
-            print('Failed to send message:', response.text)
 
 if __name__ == "__main__":
-    # W = Weather().main()
-    print(PROJECT_PATH)
+    W = Weather().main()
 

+ 53 - 0
utils/gotify.py

@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+import httpx
+import json
+import os
+
+
+class GotifyNotifier:
+    def __init__(self, title, message, token_name=''):
+        self.gotify_url = 'https://gotify.erhe.top'
+        self.app_token = self.match_token_name(token_name)
+        self.title = title
+        self.message = message
+
+    def match_token_name(self, name):
+        token_name_dict = {}
+        # 读取项目根目录下的 gotify_config.json 文件
+        gotify_config_path = os.path.join(PROJECT_PATH, 'gotify_config.json')
+        with open(gotify_config_path, 'r') as f:
+            token_name_dict = json.load(f)
+        token = token_name_dict.get(name)
+        if token:
+            return token
+        else:
+            return token_name_dict['base']
+
+    def send_message(self):
+        # 构建POST请求的headers
+        headers = {
+            'Content-Type': 'application/json'
+        }
+
+        # 构建POST请求的body
+        body = {
+            'title': self.title,
+            'message': self.message
+        }
+
+        # 发送POST请求
+        with httpx.Client() as client:
+            response = client.post(
+                url=f"{self.gotify_url}/message?token={self.app_token}",
+                headers=headers,
+                json=body
+            )
+
+        # 或者可以使用 curl
+        # curl -k "https://gotify.erhe.top/message?token=A0Xg6ZE5946iBYg" -F "title=测试发送信息" -F "message=假装有信息,测试发送" -F "priority=5"
+
+        # 检查响应状态码
+        if response.status_code == 200:
+            print('Gotify Message sent successfully!')
+        else:
+            print('Failed to send message:', response.text)