rssVideos: Show creator

Even if it's not always  present for all RSS feeds
This commit is contained in:
Geoffrey Frogeye 2021-12-12 14:27:08 +01:00
parent 9493edc1fd
commit 6a6f5401e6
Signed by: geoffrey
GPG key ID: C72403E7F82E6AD8

View file

@ -48,37 +48,54 @@ class RVCommand(enum.Enum):
class RVElement: class RVElement:
title: str
link: str
# creator: str
# description: str
# date: datetime.datetime
guid: int
parent: "RVDatabase" parent: "RVDatabase"
item: minidom.Element
was_downloaded: bool was_downloaded: bool
def __init__(self, parent: "RVDatabase", item: minidom.Element) -> None: def __init__(self, parent: "RVDatabase", item: minidom.Element) -> None:
def get_data(tag_name: str) -> str: self.parent = parent
nodes = item.getElementsByTagName(tag_name) self.item = item
self.was_downloaded = False
def get_tag_data(self, tag_name: str) -> str:
nodes = self.item.getElementsByTagName(tag_name)
if len(nodes) != 1: if len(nodes) != 1:
raise RuntimeError(f"Exepected 1 tag `{tag_name}`, got {len(nodes)}.") raise KeyError(f"Exepected 1 tag `{tag_name}`, got {len(nodes)}.")
children = nodes[0].childNodes children = nodes[0].childNodes
if len(children) != 1: if len(children) != 1:
raise RuntimeError( raise KeyError(
f"Exepected 1 children for tag `{tag_name}`, got {len(children)}." f"Exepected 1 children for tag `{tag_name}`, got {len(children)}."
) )
return children[0].data return children[0].data
self.title = get_data("title") @property
self.link = get_data("link") def title(self) -> str:
# self.creator = get_data("dc:creator") return self.get_tag_data("title")
# self.description = get_data("description")
# self.date = get_data("pubDate")
self.guid = int(get_data("guid"))
self.parent = parent @property
self.was_downloaded = False def link(self) -> str:
return self.get_tag_data("link")
@property
def creator(self) -> typing.Optional[str]:
try:
return self.get_tag_data("dc:creator")
except KeyError:
return None
@property
def description(self) -> str:
# TODO Testing
return self.get_tag_data("description")
@property
def date(self) -> str:
# TODO datetime format
return self.get_tag_data("pubDate")
@property
def guid(self) -> int:
return int(self.get_tag_data("guid"))
def read_cache(self, cache: "RVElement") -> None: def read_cache(self, cache: "RVElement") -> None:
if "ytdl_infos" in cache.__dict__: if "ytdl_infos" in cache.__dict__:
@ -88,7 +105,7 @@ class RVElement:
self.was_downloaded = True self.was_downloaded = True
def __str__(self) -> str: def __str__(self) -> str:
return f"{self.title} {self.link}" return f"{self.guid}: {self.creator} {self.title} {self.link}"
@property @property
def downloaded(self) -> bool: def downloaded(self) -> bool:
@ -204,12 +221,15 @@ class RVDatabase:
if el.guid in cache_els: if el.guid in cache_els:
el.read_cache(cache_els[el.guid]) el.read_cache(cache_els[el.guid])
@functools.cached_property
def feed_xml(self) -> minidom.Document:
with urllib.request.urlopen(self.args.feed) as request:
return minidom.parse(request)
def read_feed(self) -> None: def read_feed(self) -> None:
log.info("Fetching RSS feed") log.info("Fetching RSS feed")
self.elements = list() self.elements = list()
with urllib.request.urlopen(self.args.feed) as request: for item in self.feed_xml.getElementsByTagName("item"):
with minidom.parse(request) as xmldoc:
for item in xmldoc.getElementsByTagName("item"):
element = RVElement(self, item) element = RVElement(self, item)
self.elements.insert(0, element) self.elements.insert(0, element)
log.debug(f"Known: {element}") log.debug(f"Known: {element}")