70 lines
1.3 KiB
Python
70 lines
1.3 KiB
Python
|
|
import json
|
|
from enum import Enum
|
|
from dataclasses import dataclass
|
|
|
|
from feedparser import FeedParserDict, parse
|
|
|
|
|
|
class Feeds(Enum):
|
|
THE_UPPER_LIP = "https://theupperlip.co.uk/rss"
|
|
THE_BABYLON_BEE= ""
|
|
|
|
|
|
@dataclass
|
|
class Source:
|
|
|
|
name: str
|
|
url: str
|
|
icon_url: str
|
|
feed: FeedParserDict
|
|
|
|
@classmethod
|
|
def from_parsed(cls, feed:FeedParserDict):
|
|
|
|
# print(json.dumps(feed, indent=8))
|
|
return cls(
|
|
name=feed.channel.title,
|
|
url=feed.channel.link,
|
|
icon_url=feed.feed.image.href,
|
|
feed=feed
|
|
)
|
|
|
|
def get_latest_article(self):
|
|
return Article.from_parsed(self.feed)
|
|
|
|
|
|
@dataclass
|
|
class Article:
|
|
|
|
title: str
|
|
description: str
|
|
url: str
|
|
thumbnail_url: str
|
|
|
|
@classmethod
|
|
def from_parsed(cls, feed:FeedParserDict):
|
|
entry = feed.entries[0]
|
|
return cls(
|
|
title=entry.title,
|
|
description=entry.description,
|
|
url=entry.link,
|
|
thumbnail_url=None
|
|
)
|
|
|
|
|
|
def get_source(feed: Feeds) -> Source:
|
|
"""
|
|
|
|
"""
|
|
|
|
parsed_feed = parse(feed.value)
|
|
return Source.from_parsed(parsed_feed)
|
|
|
|
|
|
def get_test():
|
|
|
|
parsed = parse(Feeds.THE_UPPER_LIP.value)
|
|
print(json.dumps(parsed, indent=4))
|
|
return parsed
|