-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRssHandler.py
285 lines (262 loc) · 11.5 KB
/
RssHandler.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import feedparser
from feedparser import FeedParserDict
from bs4 import BeautifulSoup, NavigableString
from datetime import datetime, timedelta
from pprint import pprint
import requests
from NotionBlock import NotionBlock
class RSSHandle:
def __init__(self, rss_url):
self.rss_url = rss_url
def update(self, rss_url):
self.rss_url = rss_url
def fetch_feed(self):
# 解析 RSS
feed:FeedParserDict = feedparser.parse(self.rss_url)
if feed.bozo:
raise ValueError(f"无法解析 RSS 源: {self.rss_url}")
return feed
def get_feed_info(self):
# 提取订阅源信息
try:
feed:FeedParserDict = self.fetch_feed()
return {
"title": feed.feed.title,
"link": feed.feed.link,
"description": feed.feed.get("description", "无描述")
}
except Exception as e:
print(f"An error occurred {e}")
return None
def get_articles(self):
# 提取文章信息
feed = self.fetch_feed()
articles = []
for entry in feed.entries:
raw_content = entry.get("content", [{"value": entry.get("description", "无正文")}])[0]["value"]
articles.append({
"title": entry.title,
"link": entry.link,
"published": self.date_transform(entry.published),
"content": RSSHandle.clean_html(raw_content)
})
return articles
@staticmethod
def date_transform(date_ori:str, time_zone_offset=8):
'''将data转换成标准iso86格式
"2020-03-17T19:10:04.968Z 参见
https://developers.notion.com/reference/database"'''
# 自己用字符串切片写的,发现有现成的
date = datetime.strptime(date_ori, "%a, %d %b %Y %H:%M:%S %Z")
date = date + timedelta(hours=time_zone_offset)
date = date.strftime("%Y-%m-%dT%H:%M:%SZ")
return date
@staticmethod
def clean_html(html_content):
"""
Recursively parse HTML content into Notion-compatible blocks.
"""
notionblock = NotionBlock("", "")
soup = BeautifulSoup(html_content, "html.parser")
blocks = []
def parse_element(element):
# Handle various HTML elements and convert them into Notion blocks
if element.name == "p":
# Process <p> as a single block with its children
rich_text = []
annotations_italic = False
if element.parent.name == "em":
annotations_italic = True
for child in element.children:
# print(f"child: {child.name}") # 添加调试信息
if child.name == "a": # Handle hyperlinks
link_text = child.get_text(strip=True)
href = child.get("href")
if link_text and href:
rich_text.append({
"type": "text",
"text": {"content": link_text, "link": {"url": href}},
"annotations": {
"bold": False,
"italic": annotations_italic,
"strikethrough": False,
"underline": True,
"code": False,
"color": "default"
}
})
elif child.name is None: # Handle plain text
text = child.strip()
if text:
rich_text.append({
"type": "text",
"text": {"content": text},
"annotations": {"bold": False, "italic": annotations_italic}
})
elif child.name == "strong":
text = child.get_text(strip=True)
if text:
rich_text.append({
"type": "text",
"text": {"content": text},
"annotations": {"bold": True, "italic": annotations_italic}
})
if rich_text:
return [{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": rich_text,
"color": "default"
}
}]
# return [notionblock.paragraph(element.get_text(strip=True))]
elif element.name in ["h1", "h2", "h3"]:
level = int(element.name[1]) # Extract heading level, e.g. h1 -> 1
return [notionblock.heading(level, element.get_text(strip=True))]
elif element.name == "hr":
return [notionblock.divider()]
elif element.name == "ul":
return [
notionblock.bulleted_list_item(li.get_text(strip=True))
for li in element.find_all("li")
]
elif element.name == "ol":
return [
notionblock.numbered_list_item(li.get_text(strip=True))
for li in element.find_all("li")
]
elif element.name == "blockquote":
return [notionblock.quote(element.get_text(strip=True))]
elif element.name == "img":
img_url = element.get("src")
if img_url:
try:
response = requests.get(img_url)
if response.status_code == 200 and "image" in response.headers.get("Content-Type", ""):
return [notionblock.image(img_url)]
except Exception as e:
print(f"An error occurred {e}")
return []
# return [notionblock.image(img_url)]
elif element.name == "audio":
print("audio block found")
audio_url = element.get("src")
if audio_url:
print(f"audio src: {audio_url}")
return [notionblock.embed(audio_url)]
elif element.name == "figure":
# Handle figure with image and caption
blocks = []
img = element.find("img")
caption = element.find("figcaption")
if img:
img_block = notionblock.image(img["src"])
if caption:
img_block["image"]["caption"] = [{"type": "text", "text": {"content": caption.get_text(strip=True)}}]
blocks.append(img_block)
return blocks # 缩进不能前移不能直接就跑了,要处理里面的元素
elif element.name == "small":
rich_text = []
for child in element.children:
print(f"child: {child.name}") # 添加调试信息
if child.name == "a": # Handle hyperlinks
link_text = child.get_text(strip=True)
href = child.get("href")
if link_text and href:
rich_text.append({
"type": "text",
"text": {"content": link_text, "link": {"url": href}},
"annotations": {
"bold": False,
"italic": False,
"strikethrough": False,
"underline": True,
"code": False,
"color": "default"
}
})
elif child.name is None: # Handle plain text
text = child.strip()
if text:
rich_text.append({
"type": "text",
"text": {"content": text}
})
elif child.name == "strong":
text = child.get_text(strip=True)
if text:
rich_text.append({
"type": "text",
"text": {"content": text},
"annotations": {"bold": True}
})
if rich_text:
return [{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": rich_text,
"color": "default"
}
}]
# return [notionblock.paragraph(element.get_text(strip=True))]
elif element.name is None: # Handle text nodes
# text = element.strip()
# if text:
# return [notionblock.paragraph(text)]
pass
try:
if not isinstance(element, NavigableString) and element.contents and (hasattr(element, 'name') or isinstance(element, str)):
result = []
for child in element.contents:
result.extend(parse_element(child))
return result
except AttributeError:
pass
return []
for element in soup.contents:
# print(f"Processing element: {element}") # 添加调试信息
try:
blocks.extend(parse_element(element))
except Exception as e:
print(f"An error occurred {e}")
continue
# blocks.extend(parse_element(element))
# # 先看效果,超过100会出问题,所以这里先把截断
blocks = blocks[:100]
return blocks
def main():
rss_url = "https://rss.soyet.icu/theinitium/app"
rss_handler = RSSHandle(rss_url)
feed_info = rss_handler.get_feed_info()
articles = rss_handler.get_articles()
print(feed_info)
# print(articles)
with open("content.txt", "w", encoding="utf-8") as file:
for article in articles:
file.write(f"{article['title']}\n{article['link']}\n{article['published']}\n{article['content']}\n\n")
break
def parse_content_file(file_path):
'''测试本地文件'''
with open(file_path, "r", encoding="utf-8") as file:
raw_content = file.read()
rss_handler = RSSHandle("")
notion_blocks = rss_handler.clean_html(raw_content)
return notion_blocks
def test_content_block():
# 解析 content.txt 并输出结果
filename1 = "content.txt"
filename2 = "audio2.txt"
filename3 = "audio.txt"
filename4 = "link_text.txt"
filename5 = "link_text2.txt"
content_blocks = parse_content_file(filename5)
with open("link_blocks3.txt", "w", encoding="utf-8") as file:
for block in content_blocks:
file.write(f"{block}\n")
print(block)
if __name__ == "__main__":
pass
test_content_block()
# main()