2021-07-30 11:48:32 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import feedparser
|
2021-08-01 17:02:15 +02:00
|
|
|
from inject import autoparams
|
2021-07-30 11:48:32 +02:00
|
|
|
|
2021-08-01 17:02:15 +02:00
|
|
|
from ytrssil.config import Configuration
|
2021-07-30 11:48:32 +02:00
|
|
|
from ytrssil.datatypes import Channel, Video
|
2021-08-02 01:38:28 +02:00
|
|
|
from ytrssil.exceptions import ChannelNotFound
|
2021-11-19 19:44:46 +01:00
|
|
|
from ytrssil.protocols import ChannelRepository, Parser
|
2021-07-30 11:48:32 +02:00
|
|
|
|
|
|
|
|
2021-11-19 19:44:46 +01:00
|
|
|
class ParserBase:
|
2021-08-01 17:02:15 +02:00
|
|
|
@autoparams('channel_repository')
|
2021-07-30 11:48:32 +02:00
|
|
|
def __init__(self, channel_repository: ChannelRepository) -> None:
|
|
|
|
self.repository = channel_repository
|
|
|
|
|
2021-08-01 17:02:15 +02:00
|
|
|
|
2021-11-19 19:44:46 +01:00
|
|
|
class FeedparserParser(ParserBase):
|
2021-07-30 11:48:32 +02:00
|
|
|
def __call__(self, feed_content: str) -> Channel:
|
|
|
|
d = feedparser.parse(feed_content)
|
|
|
|
channel_id: str = d['feed']['yt_channelid']
|
|
|
|
try:
|
|
|
|
channel = self.repository.get_channel(channel_id)
|
|
|
|
except ChannelNotFound:
|
|
|
|
channel = Channel(
|
|
|
|
channel_id=channel_id,
|
|
|
|
name=d['feed']['title'],
|
|
|
|
)
|
2021-08-01 17:02:15 +02:00
|
|
|
self.repository.create_channel(channel)
|
2021-07-30 11:48:32 +02:00
|
|
|
|
|
|
|
for entry in d['entries']:
|
2021-08-01 17:02:15 +02:00
|
|
|
video = Video(
|
2021-07-30 11:48:32 +02:00
|
|
|
video_id=entry['yt_videoid'],
|
|
|
|
name=entry['title'],
|
|
|
|
url=entry['link'],
|
|
|
|
timestamp=datetime.fromisoformat(entry['published']),
|
|
|
|
channel_id=channel.channel_id,
|
|
|
|
channel_name=channel.name,
|
2021-08-01 17:02:15 +02:00
|
|
|
)
|
|
|
|
if channel.add_video(video):
|
|
|
|
self.repository.add_new_video(channel, video)
|
2021-07-30 11:48:32 +02:00
|
|
|
|
|
|
|
return channel
|
2021-08-01 17:02:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
@autoparams()
|
|
|
|
def create_feed_parser(config: Configuration) -> Parser:
|
2021-08-06 00:07:30 +02:00
|
|
|
parser_type = config.parser_type
|
2021-08-01 17:02:15 +02:00
|
|
|
if parser_type == 'feedparser':
|
|
|
|
return FeedparserParser()
|
|
|
|
else:
|
|
|
|
raise Exception(f'Unknown feed parser type: "{parser_type}"')
|