/
rss_parser.py
53 lines (35 loc) · 1.42 KB
/
rss_parser.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
import httpx
import asyncio
from collections import deque
import feedparser
async def rss_parser(httpx_client, posted_q,
n_test_chars, send_message_func=None):
'''Парсер rss ленты'''
rss_link = 'https://rssexport.rbc.ru/rbcnews/news/20/full.rss'
while True:
try:
response = await httpx_client.get(rss_link)
except:
await asyncio.sleep(10)
continue
feed = feedparser.parse(response.text)
for entry in feed.entries[::-1]:
summary = entry['summary']
title = entry['title']
news_text = f'{title}\n{summary}'
head = news_text[:n_test_chars].strip()
if head in posted_q:
continue
if send_message_func is None:
print(news_text, '\n')
else:
await send_message_func(f'rbc.ru\n{news_text}')
posted_q.appendleft(head)
await asyncio.sleep(5)
if __name__ == "__main__":
# Очередь из уже опубликованных постов, чтобы их не дублировать
posted_q = deque(maxlen=20)
# 50 первых символов от текста новости - это ключ для проверки повторений
n_test_chars = 50
httpx_client = httpx.AsyncClient()
asyncio.run(rss_parser(httpx_client, posted_q, n_test_chars))