forked from idealclover/RSS-OPML-to-Markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opml_merge.py
266 lines (224 loc) · 8.64 KB
/
opml_merge.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
import os
import re
from opyml import OPML, Outline, Body, Head
import sys
def opmlparse(filename):
with open(filename, encoding='utf-8') as f:
opml1=OPML.from_xml(f.read())
#opml2=Outline.parse_outlines(f.read())
return opml1
def get_domain(url):
"""
获取 URL 的主域名。
Args:
url: URL 字符串。
Returns:
URL 的主域名。
"""
match = re.match(r'(https?://)(.*?)/(.*)', url)
if match:
return re.sub(r'www\.', '', match.group(2))
else:
return None
def erase_second_domain(url):
match = re.match(r'(.*)\.(.*\..*)', url)
if match:
return match.group(2)
def merge_opml(opml_dir):
"""
合并目录中所有 OPML 文件,并处理嵌套的 outline。
Args:
opml_dir: OPML 文件所在的目录。
Returns:
合并后的 OPML 文件。
"""
# 读取目录中所有 OPML 文件
opml_files = [os.path.join(opml_dir, f) for f in os.listdir(opml_dir) if f.endswith('.opml')]
print(opml_files)
# 初始化去重列表
seen_urls = set()
# 初始化合并后的 OPML 文件
opml = OPML()
opml.head = Head(
title="OPML merged by using https://github.com/AboutRSS/RSS-OPML-to-Markdown/blob/master/RSS_OPML_to_Markdown/opml_merge.py",
docs="http://dev.opml.org/spec2.html",
)
# 遍历所有 OPML 文件
for opml_file in opml_files:
print(f'正在读取 OPML 文件:{opml_file}')
parser = opmlparse(opml_file)
# 遍历每个 outline
for outline in parser.body.outlines:
print(outline.xml_url)
if outline.xml_url is None: # 如果 outline 是父 outline,一般有两级 outline 时,第一级 outline 是 RSS Feed 的分类信息。
if outline.title:
cate=outline.title
elif outline.text:
cate=outline.text
else:
cate is None
for outline in outline.outlines:
print(outline.xml_url)
if outline.html_url is None: # 有些 OPML 里只有 xmlUrl,没有 htmlUrl
if outline.xml_url and outline.title: # 有些 OPML 里 RSS 名称是赋值在 title
url = outline.xml_url.lower()
if url[0:7]=='http://':
url = url[7:]
if url in seen_urls:
continue
else:
domain = get_domain(outline.xml_url)
if re.match(r'(.*)\.(.*\..*)', domain):
domain = erase_second_domain(domain)
print(domain)
if domain=='feed43.com':
continue
else:
seen_urls.add(url)
elif url[0:8]=='https://':
url = url[8:]
if url in seen_urls:
continue
else:
domain = get_domain(outline.xml_url)
if re.match(r'(.*)\.(.*\..*)', domain):
domain = erase_second_domain(domain)
print(domain)
if domain=='feed43.com':
continue
else:
seen_urls.add(url)
opml.body.outlines.append(Outline(
text=outline.title,
xml_url=outline.xml_url,
category=cate,
))
elif outline.xml_url and outline.text: # 有些 OPML 里 RSS 名称是赋值在 text
url = outline.xml_url.lower()
if url[0:7]=='http://':
url = url[7:]
if url in seen_urls:
continue
else:
domain = get_domain(outline.xml_url)
if re.match(r'(.*)\.(.*\..*)', domain):
domain = erase_second_domain(domain)
print(domain)
if domain=='feed43.com':
continue
else:
seen_urls.add(url)
elif url[0:8]=='https://':
url = url[8:]
if url in seen_urls:
continue
else:
domain = get_domain(outline.xml_url)
if re.match(r'(.*)\.(.*\..*)', domain):
domain = erase_second_domain(domain)
print(domain)
if domain=='feed43.com':
continue
else:
seen_urls.add(url)
opml.body.outlines.append(Outline(
text=outline.text,
xml_url=outline.xml_url,
category=cate,
))
else: # OPML 里有 htmlUrl 时, 用 htmlUrl 做查重
if outline.xml_url and outline.title:
url = outline.html_url.lower()
if url[0:7]=='http://':
url = url[7:]
print(url)
if url in seen_urls:
continue
else:
domain = get_domain(outline.xml_url)
if re.match(r'(.*)\.(.*\..*)', domain):
domain = erase_second_domain(domain)
print(domain)
if domain=='feed43.com':
continue
else:
seen_urls.add(url)
elif url[0:8]=='https://':
url = url[8:]
print(url)
if url in seen_urls:
continue
else:
domain = get_domain(outline.xml_url)
#print (re.match(r'(.*)\.(.*\..*)', domain))
#print(domain)
if re.match(r'(.*)\.(.*\..*)', domain):
domain = erase_second_domain(domain)
print(domain)
if domain=='feed43.com':
continue
else:
seen_urls.add(url)
opml.body.outlines.append(Outline(
text=outline.title,
xml_url=outline.xml_url,
html_url=outline.html_url,
category=cate,
))
elif outline.xml_url and outline.text:
url = outline.html_url.lower()
if url[0:7]=='http://':
url = url[7:]
print(url)
if url in seen_urls:
continue
else:
domain = get_domain(outline.xml_url)
if re.match(r'(.*)\.(.*\..*)', domain):
domain = erase_second_domain(domain)
print(domain)
if domain=='feed43.com':
continue
else:
seen_urls.add(url)
elif url[0:8]=='https://':
url = url[8:]
print(url)
if url in seen_urls:
continue
else:
domain = get_domain(outline.xml_url)
if re.match(r'(.*)\.(.*\..*)', domain):
domain = erase_second_domain(domain)
print(domain)
if domain=='feed43.com':
continue
else:
seen_urls.add(url)
opml.body.outlines.append(Outline(
text=outline.text,
xml_url=outline.xml_url,
html_url=outline.html_url,
category=cate,
))
return opml.to_xml()
def main():
intro = """
Use this Python script to merge OPML files.
"""
if len(sys.argv) == 1:
print(intro)
sys.exit()
elif len(sys.argv) == 2:
opml = merge_opml(sys.argv[1])
with open(os.path.join(sys.argv[1],'merged.opml'), 'w',encoding='utf-8') as f:
f.write(opml)
elif len(sys.argv) == 3:
opml = merge_opml(sys.argv[1])
with open(os.path.join(sys.argv[2],'merged.opml'), 'w',encoding='utf-8') as f:
f.write(opml)
else:
print("Error: argv number doesn't match")
exit(-1)
if __name__ == '__main__':
main()