Merge pull request 'RSS module' (#19) from feature_rss into master
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #19 Reviewed-by: Alexey Skobkin <skobkin-ru@ya.ru>
This commit is contained in:
commit
04bdbbe79c
|
@ -4,3 +4,4 @@ idna==3.3
|
||||||
pyTelegramBotAPI==4.5.0
|
pyTelegramBotAPI==4.5.0
|
||||||
requests==2.27.1
|
requests==2.27.1
|
||||||
urllib3==1.26.9
|
urllib3==1.26.9
|
||||||
|
feedparser==6.0.2
|
35
rss.py
Normal file
35
rss.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import feedparser
|
||||||
|
|
||||||
|
|
||||||
|
class FeedItem():
|
||||||
|
def __init__(self, url: str, title: str, description: str) -> None:
|
||||||
|
self.url = url
|
||||||
|
self.title = title
|
||||||
|
self.description = description
|
||||||
|
|
||||||
|
class Feed():
|
||||||
|
def __init__(self, url: str, items: list[FeedItem]) -> None:
|
||||||
|
self.url = url
|
||||||
|
self.items = items
|
||||||
|
|
||||||
|
class RssReader():
|
||||||
|
def get_feed(self, url: str) -> Feed:
|
||||||
|
f = feedparser.parse(url)
|
||||||
|
items = self.__get_items(f.entries)
|
||||||
|
return Feed(url, items)
|
||||||
|
|
||||||
|
def __convert_to_feed_item(self, item: dict) -> FeedItem:
|
||||||
|
if 'title' in item:
|
||||||
|
title = item['title']
|
||||||
|
if 'link' in item:
|
||||||
|
url = item['link']
|
||||||
|
if 'summary' in item:
|
||||||
|
description = item['summary']
|
||||||
|
return FeedItem(url, title, description)
|
||||||
|
|
||||||
|
def __get_items(self, items: list) -> list:
|
||||||
|
list_items = []
|
||||||
|
for item in items:
|
||||||
|
list_items.append(self.__convert_to_feed_item(item))
|
||||||
|
return list_items
|
||||||
|
|
Loading…
Reference in a new issue