rssVideos: Show creator
Even if it's not always present for all RSS feeds
This commit is contained in:
parent
9493edc1fd
commit
6a6f5401e6
|
@ -48,37 +48,54 @@ class RVCommand(enum.Enum):
|
|||
|
||||
|
||||
class RVElement:
|
||||
title: str
|
||||
link: str
|
||||
# creator: str
|
||||
# description: str
|
||||
# date: datetime.datetime
|
||||
guid: int
|
||||
|
||||
parent: "RVDatabase"
|
||||
item: minidom.Element
|
||||
was_downloaded: bool
|
||||
|
||||
def __init__(self, parent: "RVDatabase", item: minidom.Element) -> None:
|
||||
def get_data(tag_name: str) -> str:
|
||||
nodes = item.getElementsByTagName(tag_name)
|
||||
self.parent = parent
|
||||
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:
|
||||
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
|
||||
if len(children) != 1:
|
||||
raise RuntimeError(
|
||||
raise KeyError(
|
||||
f"Exepected 1 children for tag `{tag_name}`, got {len(children)}."
|
||||
)
|
||||
return children[0].data
|
||||
|
||||
self.title = get_data("title")
|
||||
self.link = get_data("link")
|
||||
# self.creator = get_data("dc:creator")
|
||||
# self.description = get_data("description")
|
||||
# self.date = get_data("pubDate")
|
||||
self.guid = int(get_data("guid"))
|
||||
@property
|
||||
def title(self) -> str:
|
||||
return self.get_tag_data("title")
|
||||
|
||||
self.parent = parent
|
||||
self.was_downloaded = False
|
||||
@property
|
||||
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:
|
||||
if "ytdl_infos" in cache.__dict__:
|
||||
|
@ -88,7 +105,7 @@ class RVElement:
|
|||
self.was_downloaded = True
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.title} – {self.link}"
|
||||
return f"{self.guid}: {self.creator} – {self.title} – {self.link}"
|
||||
|
||||
@property
|
||||
def downloaded(self) -> bool:
|
||||
|
@ -204,12 +221,15 @@ class RVDatabase:
|
|||
if el.guid in cache_els:
|
||||
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:
|
||||
log.info("Fetching RSS feed")
|
||||
self.elements = list()
|
||||
with urllib.request.urlopen(self.args.feed) as request:
|
||||
with minidom.parse(request) as xmldoc:
|
||||
for item in xmldoc.getElementsByTagName("item"):
|
||||
for item in self.feed_xml.getElementsByTagName("item"):
|
||||
element = RVElement(self, item)
|
||||
self.elements.insert(0, element)
|
||||
log.debug(f"Known: {element}")
|
||||
|
|
Loading…
Reference in a new issue