refactor rss.py
This commit is contained in:
parent
18f680a9e9
commit
b2c49a583a
38
rss.py
38
rss.py
|
@ -2,33 +2,23 @@ import feedparser
|
||||||
|
|
||||||
|
|
||||||
class FeedItem:
|
class FeedItem:
|
||||||
def __init__(self, url: str, title: str, description: str, date: str) -> None:
|
def __init__(self, item: feedparser.util.FeedParserDict) -> None:
|
||||||
self.url = url
|
self.url = item.get('link', '')
|
||||||
self.title = title
|
self.title = item.get('title', '')
|
||||||
self.description = description
|
self.description = item.get('summary', '')
|
||||||
self.date = date
|
self.date = item.get('published', '')
|
||||||
|
|
||||||
class Feed:
|
class Feed:
|
||||||
def __init__(self, url: str, items: list[FeedItem]) -> None:
|
def __init__(self, url: str, feed: feedparser.util.FeedParserDict) -> None:
|
||||||
self.url = url
|
self.url = url
|
||||||
self.items = items
|
self.items = self.__get_items(feed.entries)
|
||||||
|
|
||||||
|
def __get_items(self, items: list[feedparser.util.FeedParserDict]) -> list[FeedItem]:
|
||||||
|
list_items = []
|
||||||
|
for item in items:
|
||||||
|
list_items.append(FeedItem(item))
|
||||||
|
return list_items
|
||||||
|
|
||||||
class RssReader:
|
class RssReader:
|
||||||
def get_feed(self, url: str) -> Feed:
|
def get_feed(self, url: str) -> Feed:
|
||||||
f = feedparser.parse(url)
|
return Feed(url, feedparser.parse(url))
|
||||||
items = self.__get_items(f.entries)
|
|
||||||
return Feed(url, items)
|
|
||||||
|
|
||||||
def __convert_to_feed_item(self, item: feedparser.util.FeedParserDict) -> FeedItem:
|
|
||||||
title = item.get('title', '')
|
|
||||||
url = item.get('link', '')
|
|
||||||
description = item.get('summary', '')
|
|
||||||
date = item.get('published', '')
|
|
||||||
return FeedItem(url, title, description, date)
|
|
||||||
|
|
||||||
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