nebula-rss/nebula_rss/nebula_feed.py
2022-01-11 17:04:32 +01:00

37 lines
1.3 KiB
Python

from feedgen.feed import FeedGenerator
from nebula_rss import NebulaLoader
class NebulaFeed:
def __init__(self, nebula_loader: NebulaLoader, feed_url: str):
self.nebula_loader = nebula_loader
self.feed_url = feed_url
def generate(self, format: str = 'atom') -> str:
videos = self.nebula_loader.load()
fg = FeedGenerator()
fg.id(self.feed_url)
fg.title('Nebula subscriptions')
fg.logo('https://nebula.app/apple-touch-icon.png')
fg.subtitle('Your subscribed videos')
fg.link(href=self.feed_url, rel='self')
fg.language('en')
for video in videos:
fe = fg.add_entry()
fe.id(video.url)
fe.title(video.title)
fe.link(href=video.url)
fe.author({'name': video.creator, 'email': 'Unknown'}) # Email is required for RSS format, but is not known
fe.enclosure(url=video.url)
fe.summary(f'New video by {video.creator}: {video.title}')
fe.published(video.release_at)
feed = ''
if format == 'atom':
feed = fg.atom_str(pretty=True)
elif format == 'rss':
feed = fg.rss_str(pretty=True)
else:
raise Exception(f'Unknown format: "{format}". Use "atom" or "rss')
return feed