jack 10 月之前
父節點
當前提交
65bde90050
共有 3 個文件被更改,包括 57 次插入30 次删除
  1. 40 2
      base/base_daily_logs_send.py
  2. 16 26
      demo/demo_send_gotify.py
  3. 1 2
      manual/read_news/main.py

+ 40 - 2
base/base_daily_logs_send.py

@@ -5,6 +5,7 @@
 import time
 import os
 import sys
+import httpx
 
 sys.path.append(os.path.join(os.path.abspath(__file__).split('auto')[0] + 'auto'))
 
@@ -61,6 +62,9 @@ class LogsHandle(object):
         if text:
             S = SendEmail(subject=subject, title=title, text=text)
             S.send()
+
+            G = GotifyNotifier(title=title, message=text, token='A9KF--mx_12PjSu')
+            G.send_message()
         else:
             print("No error logs found for today.")
 
@@ -125,7 +129,41 @@ class SendEmail(object):
             print("Error: 无法发送邮件", e)
 
 
+class GotifyNotifier:
+    def __init__(self, title, message, token='A8EVb0Cmxnb2vfk'):
+        self.gotify_url = 'https://gotify.erhe.top'
+        self.app_token = token
+        self.title = title
+        self.message = message
+
+    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
+            )
+
+        # 检查响应状态码
+        if response.status_code == 200:
+            print('Gotify Message sent successfully!')
+        else:
+            print('Failed to send message:', response.text)
+
+
 if __name__ == '__main__':
-    print("发送当天日志:start")
+    print("开始执行日志处理")
     LogsHandle().logs_send()
-    print("发送当天日志:done")
+    print("处理日志程序执行完毕")

+ 16 - 26
demo/demo_send_gotify.py

@@ -1,30 +1,20 @@
 # -*- coding: utf-8 -*-
-import httpx
+import subprocess
 
-# Gotify服务器地址
-gotify_url = "https://gotify.erhe.top/message?token=A9KF--mx_12PjSu"
+token = 'AdVsmticWy_cnF6'
+title = '测试标题'
+message = '这是一条通过 Webhook 推送的消息'
+priority = 5
 
-# 要发送的消息内容
-message = {
-    "message": "测试消息",
-    "title": "测试标题",
-    "priority": 5
-}
+# 推送消息
+result = subprocess.run(['curl',
+                         f'https://gotify.erhe.top/message?token={token}',
+                         '-F',
+                         f'title={title}',
+                         '-F',
+                         f'message={message}',
+                         '-F',
+                         f'priority={priority}'
+                         ])
 
-
-async def send_message():
-    async with httpx.AsyncClient() as client:
-        try:
-            response = await client.post(gotify_url, json=message)
-            if response.status_code == 200:
-                print("消息发送成功")
-            else:
-                print(f"消息发送失败,状态码: {response.status_code}")
-        except httpx.RequestException as e:
-            print(f"请求出现异常: {e}")
-
-
-if __name__ == "__main__":
-    import asyncio
-
-    asyncio.run(send_message())
+print(result)

+ 1 - 2
manual/read_news/main.py

@@ -154,7 +154,7 @@ class SearchByKeyword:
     def search(self, keyword):
         result = {}
         for item in self.data:
-            if keyword.lower() in item['title'].lower() or keyword.lower() in item['description'].lower():
+            if keyword.lower() in item['title'].lower():
                 if keyword.lower() not in result:
                     result[keyword] = []
                 result[keyword].append(item)
@@ -195,5 +195,4 @@ if __name__ == "__main__":
         for news in item:
             print(f'标题:{news["title"]}')
             print(f'链接:{news["link"]}')
-            print(f'描述:{news["description"]}')
             print('-' * 200)