-
Notifications
You must be signed in to change notification settings - Fork 866
Expand file tree
/
Copy pathpush.py
More file actions
134 lines (115 loc) · 4.84 KB
/
push.py
File metadata and controls
134 lines (115 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import json
import logging
import os
import random
import time
import requests
from config import (
PUSHPLUS_TOKEN,
SERVERCHAN_SPT,
TELEGRAM_BOT_TOKEN,
TELEGRAM_CHAT_ID,
WXPUSHER_SPT,
)
logger = logging.getLogger(__name__)
class PushNotification:
def __init__(self):
self.pushplus_url = "https://www.pushplus.plus/send"
self.telegram_url = "https://api.telegram.org/bot{}/sendMessage"
self.server_chan_url = "https://sctapi.ftqq.com/{}.send"
self.wxpusher_simple_url = "https://wxpusher.zjiecode.com/api/send/message/{}/{}"
self.headers = {"Content-Type": "application/json"}
self.proxies = {
"http": os.getenv("http_proxy"),
"https": os.getenv("https_proxy"),
}
def push_pushplus(self, content, token):
attempts = 5
for attempt in range(attempts):
try:
response = requests.post(
self.pushplus_url,
data=json.dumps({"token": token, "title": "微信阅读推送","content": content,}).encode("utf-8"),headers=self.headers,timeout=10,)
response.raise_for_status()
logger.info("PushPlus 响应: %s", response.text)
return True
except requests.exceptions.RequestException as exc:
logger.error("PushPlus 推送失败: %s", exc)
if attempt < attempts - 1:
sleep_time = random.randint(180, 360)
logger.info("%d 秒后重试...", sleep_time)
time.sleep(sleep_time)
return False
def push_telegram(self, content, bot_token, chat_id):
url = self.telegram_url.format(bot_token)
payload = {"chat_id": chat_id, "text": content}
try:
response = requests.post(url, json=payload, proxies=self.proxies, timeout=30)
logger.info("Telegram 响应: %s", response.text)
response.raise_for_status()
return True
except Exception as exc:
logger.error("Telegram 代理发送失败: %s", exc)
try:
response = requests.post(url, json=payload, timeout=30)
response.raise_for_status()
return True
except Exception as inner_exc:
logger.error("Telegram 发送失败: %s", inner_exc)
return False
def push_wxpusher(self, content, spt):
attempts = 5
url = self.wxpusher_simple_url.format(spt, content)
for attempt in range(attempts):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
logger.info("WxPusher 响应: %s", response.text)
return True
except requests.exceptions.RequestException as exc:
logger.error("WxPusher 推送失败: %s", exc)
if attempt < attempts - 1:
sleep_time = random.randint(180, 360)
logger.info("%d 秒后重试...", sleep_time)
time.sleep(sleep_time)
return False
def push_serverChan(self, content, spt):
attempts = 5
url = self.server_chan_url.format(spt)
title = "微信阅读推送"
if "自动阅读完成" not in content:
title = "微信阅读失败"
for attempt in range(attempts):
try:
response = requests.post(
url,
data=json.dumps({"title": title, "desp": content}).encode("utf-8"),
headers=self.headers,
timeout=10,
)
response.raise_for_status()
logger.info("ServerChan 响应: %s", response.text)
return True
except requests.exceptions.RequestException as exc:
logger.error("ServerChan 推送失败: %s", exc)
if attempt < attempts - 1:
sleep_time = random.randint(180, 360)
logger.info("%d 秒后重试...", sleep_time)
time.sleep(sleep_time)
return False
def push(content, method):
notifier = PushNotification()
if method in (None, ""):
logger.warning("未配置推送渠道,跳过推送。")
return False
method = str(method).lower()
if method == "pushplus":
return notifier.push_pushplus(content, PUSHPLUS_TOKEN)
if method == "telegram":
return notifier.push_telegram(content, TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID)
if method == "wxpusher":
return notifier.push_wxpusher(content, WXPUSHER_SPT)
if method == "serverchan":
return notifier.push_serverChan(content, SERVERCHAN_SPT)
logger.warning("无效的通知渠道 '%s',已跳过推送。支持:pushplus、telegram、wxpusher、serverchan", method)
return False