2022-05-29 20:17:03 +00:00
|
|
|
import feedparser
|
|
|
|
|
|
|
|
|
|
|
|
class FeedItem():
|
2022-05-29 20:29:51 +00:00
|
|
|
def __init__(self, url: str, title: str, description: str) -> None:
|
|
|
|
self.url = url
|
|
|
|
self.title = title
|
|
|
|
self.description = description
|
2022-05-29 20:17:03 +00:00
|
|
|
|
|
|
|
class Feed():
|
2022-05-29 23:00:59 +00:00
|
|
|
def __init__(self, url: str, items: list[FeedItem]) -> None:
|
2022-05-29 20:18:12 +00:00
|
|
|
self.url = url
|
|
|
|
self.items = items
|
2022-05-29 20:17:03 +00:00
|
|
|
|
|
|
|
class RssReader():
|
2022-05-29 23:11:23 +00:00
|
|
|
def get_feed(self, url: str) -> Feed:
|
|
|
|
f = feedparser.parse(url)
|
|
|
|
items = self.__get_items(f.entries)
|
|
|
|
return Feed(url, items)
|
|
|
|
|
2022-05-29 23:00:59 +00:00
|
|
|
def __convert_to_feed_item(self, item: dict) -> FeedItem:
|
2022-05-29 20:38:49 +00:00
|
|
|
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)
|
2022-05-29 20:17:03 +00:00
|
|
|
|
2022-05-29 23:00:59 +00:00
|
|
|
def __get_items(self, items: list) -> list:
|
2022-05-29 20:28:48 +00:00
|
|
|
list_items = []
|
|
|
|
for item in items:
|
2022-05-29 23:00:59 +00:00
|
|
|
list_items.append(self.__convert_to_feed_item(item))
|
2022-05-29 20:28:48 +00:00
|
|
|
return list_items
|
2022-05-29 20:17:03 +00:00
|
|
|
|