forked from OreosLab/checkinpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_rssbot.py
82 lines (68 loc) · 2.47 KB
/
api_rssbot.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
# -*- encoding: utf-8 -*-
"""
cron: 22 6-22/2 * * *
new Env('RSS 订阅');
"""
from datetime import datetime, timedelta, timezone
from time import mktime
import feedparser
from notify_mtr import send
from utils_models import History, Rss, db
class RssRobot:
def main(self):
self.remove_old_history()
rss_list = Rss.select()
post_url_list = [
rss_history.url
for rss_history in History.select().where(
History.publish_at == datetime.now().strftime("%Y-%m-%d")
)
]
no = 0
msg = ""
for rss in rss_list:
rss_history_list = []
feed = feedparser.parse(rss.feed)
title = True
c_no = 1
for entry in feed.entries:
pub_t = datetime.fromtimestamp(mktime(entry["published_parsed"]))
# 此网站单独处理
if rss.url == "https://www.foreverblog.cn":
pub_t = pub_t.replace(
year=datetime.now(timezone.utc).year
) + timedelta(hours=8)
elif rss.url == "https://www.zhihu.com":
entry.link = entry.link.split("/answer")[0]
if (
entry.link not in post_url_list
and (
datetime.timestamp(datetime.now(timezone.utc))
- datetime.timestamp(pub_t)
)
< rss.before * 86400
):
if title:
msg += f"<b>{rss.title.strip()}</b>\n"
title = False
msg = f'{msg}{c_no}. <a href="{entry.link}">{entry.title}</a>\n'
no += 1
c_no += 1
if no % 20 == 0:
send("RSS 订阅", msg)
msg = ""
title = False
rss_history_list.append(History(url=entry.link))
with db.atomic():
History.bulk_create(rss_history_list, batch_size=10)
if no % 20 != 0 and msg:
send("RSS 订阅", msg)
@staticmethod
def remove_old_history():
# 只保留最近一周的记录
week_date_range = datetime.now() + timedelta(days=-7)
History.delete().where(
History.publish_at < week_date_range.strftime("%Y-%m-%d")
).execute()
if __name__ == "__main__":
RssRobot().main()