-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsshub_client.py
52 lines (42 loc) · 1.59 KB
/
rsshub_client.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
import dateutil.parser
import requests
import re
from collections import namedtuple
from xml.etree import ElementTree
RSSHUB_BILIBILI_URL = 'https://rsshub.app/bilibili/user/dynamic/{}'
FeedItem = namedtuple('FeedItem', 'feed_title content media_list url pub_date')
def extract_content_media(content):
media_list = []
for match in re.findall(r'(<img src="(.*?)".*?>)', content):
media_list.append(match[1])
content.replace(match[0], '')
content = re.sub('<br>', '\n', content, flags=re.IGNORECASE)
content = re.sub('<.*?>', '', content)
return content.strip(), media_list
def get_new_feed(feed_url):
r = requests.get(feed_url)
if r.status_code != 200:
raise Exception(f'RSSHub请求不成功,状态码:{r.status_code}')
rss = ElementTree.fromstring(r.content)
if rss.tag != 'rss':
raise ValueError('RSS格式有误')
channel = rss.find('channel')
feed_title = channel.find('title').text
ret = []
for item in channel:
if item.tag != 'item':
continue
content = item.find('description').text
content, media_list = extract_content_media(content)
pub_date = dateutil.parser.parse(item.find('pubDate').text)
url = item.find('guid').text
ret.append(FeedItem(
feed_title=feed_title,
content=content,
media_list=media_list,
url=url,
pub_date=pub_date))
return ret[::-1]
def get_new_bilibili_status(bilibili_uid):
new_feed_items = get_new_feed(RSSHUB_BILIBILI_URL.format(bilibili_uid))
return new_feed_items