-
Notifications
You must be signed in to change notification settings - Fork 22
/
feed2notion.py
94 lines (72 loc) · 2.77 KB
/
feed2notion.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
import os
import requests
from utils import NotionAPI, deep_get, parse_rss
NOTION_SEC = os.environ.get("NOTION_SEC")
NOTION_DB_RSS = "90761665a1d141b984afca52a2b05410"
NOTION_DB_KEYWORDS = "26d213dcc8b641cd921db43eb7b23733"
NOTION_DB_READER = "bcc40eb17dc54258866faf765b151840"
# https://www.notion.so/v2xr/bcc40eb17dc54258866faf765b151840?v=4719f35c57024c5eb54c996846b2cae4&pvs=4
FEISHU_BOT_API = os.environ.get("FEISHU_BOT_API")
FEISHU_BOT_SEC = os.environ.get("FEISHU_BOT_SEC")
def feishu_bot_send_msg(msg):
"""
msg = {"title": "", "content": ""}
"""
if FEISHU_BOT_API:
requests.post(FEISHU_BOT_API, json={"pass": FEISHU_BOT_SEC, "msg": msg})
def _wrap_rss_warning_msg_fmt(title, uri):
content = f"{title} 读取失败!\n\t{uri}"
feishu_bot_send_msg({"title": "❗ RSS Warning", "content": content})
def _wrap_rss_new_msg_fmt(entries):
content = ""
for i, entry in enumerate(entries):
content += f"{i+1}. [{entry.get('title')}]({entry.get('link')}) | {entry.get('rss').get('title')}\n"
msg = {"title": "🔔 NEW RSS", "content": content}
feishu_bot_send_msg(msg)
def process_entry(entry: dict, keywords: list):
entropy = 0
match_keywords = []
# TODO: filter keywords -
text = f'{entry.get("title")} {entry.get("summary")}'
for kw in keywords:
if kw in text:
print(f"Keyword {kw} Matched! -> #{entry.get('title')}")
match_keywords.append(kw)
entropy += 1
if len(keywords) > 0:
entropy /= len(keywords)
if deep_get(entry, "rss.isWhiteList"):
entropy = 1
entry["entropy"] = float(f"{entropy}")
entry["match_keywords"] = match_keywords
return entry
def read_rss(rsslist):
for rss in rsslist:
# !! 必须和 Notion RSS DB 保持一致
entries = parse_rss(rss)
print(f"Got {len(entries)} items from #{rss.get('title')}#")
if len(entries) == 0:
# 飞书提示
_wrap_rss_warning_msg_fmt(rss.get("title"), rss.get("uri"))
for entry in entries:
yield entry
def run():
if NOTION_SEC is None:
print("NOTION_SEC secrets is not set!")
return
api = NotionAPI(NOTION_SEC, NOTION_DB_RSS, NOTION_DB_KEYWORDS, NOTION_DB_READER)
keywords = api.query_keywords()
new_entries = []
for entry in read_rss(api.query_open_rss()):
res = process_entry(entry, keywords)
if res.get("entropy") > 0:
if not api.is_page_exist(entry.get("link")):
api.save_page(entry)
new_entries.append(entry)
else:
print(f"Entry {entry.get('title')} already exist!")
# 飞书提示
if len(new_entries) > 0:
_wrap_rss_new_msg_fmt(new_entries)
if __name__ == "__main__":
run()