forked from ermaozi/get_subscribe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (112 loc) · 4.57 KB
/
main.py
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
import os
import re
import smtplib
import sys
import time
from email.mime.text import MIMEText
from email.utils import formataddr
import feedparser
import requests
requests.packages.urllib3.disable_warnings()
ok_code = [200, 201, 202, 203, 204, 205, 206]
# 邮箱域名过滤列表
blackhole_list = ["cnr.cn", "cyberpolice.cn", "gov.cn", "samr.gov.cn", "12321.cn"
"miit.gov.cn", "chinatcc.gov.cn"]
def write_log(content, level="INFO"):
date_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
update_log = f"[{date_str}] [{level}] {content}\n"
print(update_log)
with open(f'./log/{time.strftime("%Y-%m", time.localtime(time.time()))}-update.log', 'a', encoding="utf-8") as f:
f.write(update_log)
def get_mail():
comments = requests.get("https://api.github.com/repos/ermaozi/get_subscribe/issues/1/comments")
mail_re = re.compile("([\w\.\-+_]+@[\w\.\-+_]+\.\w+)")
mail_list = []
for i in comments.json():
body = i.get("body")
mails = mail_re.findall(body)
for mail in mails:
if mail.split("@")[-1] not in blackhole_list:
mail_list.append(mail)
return list(set(mail_list))
def send_mail(mail_list):
date_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
receivers = mail_list
with open("./mail/mail_template.html", "rb") as f:
mail_msg = f.read().decode("utf-8")
message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = formataddr(["二猫子的猛男助理", sender])
message['Subject'] = f'{date_str} 订阅更新提醒'
try:
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port)
smtpObj.login(mail_user, mail_pwd)
smtpObj.sendmail(sender, receivers, message.as_string())
print(f"邮件发送成功: {mail_list}")
except smtplib.SMTPException as e:
print("Error: 无法发送邮件", str(e))
def get_subscribe_url():
dirs = './subscribe'
if not os.path.exists(dirs):
os.makedirs(dirs)
log_dir = "./log"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
rss = feedparser.parse('http://feeds.feedburner.com/mattkaydiary/pZjG')
entries = rss.get("entries")
if not entries:
write_log("更新失败!无法拉取原网站内容", "ERROR")
return
update_list = []
summary = entries[0].get("summary")
if not summary:
write_log("暂时没有可用的订阅更新", "WARN")
return
v2ray_list = re.findall(r"v2ray\(若无法更新请开启代理后再拉取\):(.+?)</div>", summary)
# 获取普通订阅链接
if v2ray_list:
v2ray_url = v2ray_list[-1].replace('amp;', '')
v2ray_req = requests.request("GET", v2ray_url, verify=False)
v2ray_code = v2ray_req.status_code
if v2ray_code not in ok_code:
write_log(f"获取 v2ray 订阅失败:{v2ray_url} - {v2ray_code}", "WARN")
else:
update_list.append(f"v2ray: {v2ray_code}")
with open(dirs + '/v2ray.txt', 'w', encoding="utf-8") as f:
f.write(v2ray_req.text)
clash_list = re.findall(r"clash\(若无法更新请开启代理后再拉取\):(.+?)</div>", summary)
# 获取clash订阅链接
if clash_list:
clash_url = clash_list[-1].replace('amp;', '')
clash_req = requests.request("GET", clash_url, verify=False)
clash_code = clash_req.status_code
if clash_code not in ok_code:
write_log(f"获取 clash 订阅失败:{clash_url} - {clash_code}", "WARN")
else:
update_list.append(f"clash: {clash_code}")
with open(dirs + '/clash.yml', 'w', encoding="utf-8") as f:
clash_content = clash_req.content.decode("utf-8")
clash_content = clash_content.replace('https://www.mattkaydiary.com', "干死日本鬼子")
f.write(clash_content)
if update_list:
file_pat = re.compile(r"v2ray\.txt|clash\.yml")
if file_pat.search(os.popen("git status").read()):
write_log(f"更新成功:{update_list}", "INFO")
if mail_flag:
send_mail(get_mail())
else:
write_log(f"订阅暂未更新", "WARN")
else:
write_log(f"未能获取新的更新内容", "WARN")
def main():
get_subscribe_url()
# 主函数入口
if __name__ == '__main__':
mail_flag = False
if len(sys.argv) == 6:
mail_user = sys.argv[1]
sender = sys.argv[2]
mail_pwd = sys.argv[3]
mail_host = sys.argv[4]
mail_port = sys.argv[5]
mail_flag = True
main()