generated from RockChinQ/HelloPlugin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmux.py
45 lines (33 loc) · 1.32 KB
/
mux.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
import re
import logging
from .sites import model
def process(url: str, brief_len: int, **kwargs) -> str:
"""处理网页内容"""
adapter_cls: model.SiteAdapterBase = model.SiteAdapterBase
found = False
for adapter in model.__site_adapters__:
for regexp in adapter['regexp']:
if re.match(regexp, url):
if not adapter['cls'].feed(url):
# 匹中了这个适配器,但适配器不接受这个链接
# 则直接检查下一个适配器
break
adapter_cls = adapter['cls']
found = True
break
if found:
break
logging.debug("site adapter: {}".format(adapter_cls))
processed: dict = adapter_cls.process(url, brief_len, **kwargs)
logging.debug("site adapter ret: {}".format(processed))
# 处理成纯文本
text: str = ""
if processed.get('status', 200) == 200:
if 'content' not in processed:
text = processed.get('message', "nothing found")
else:
text += ('title: '+processed['content']['title']+"\n") if 'title' in processed['content'] else ''
text += '\n'.join(processed['content'].get('briefs', []))
else:
raise Exception(processed.get('message', "error"))
return text