rssVideos: Add watch

This commit is contained in:
Geoffrey Frogeye 2021-12-17 23:16:32 +01:00
parent 7423a93203
commit 5b195bd141
Signed by: geoffrey
GPG key ID: C72403E7F82E6AD8

View file

@ -13,7 +13,9 @@ import functools
import logging
import os
import pickle
import random
import re
import subprocess
import sys
import typing
import urllib.parse
@ -43,17 +45,20 @@ def configure_logging(args: configargparse.Namespace) -> None:
class RVCommand(enum.Enum):
download = "download"
list = "list"
watch = "watch"
class RVElement:
parent: "RVDatabase"
item: minidom.Element
was_downloaded: bool
watched: bool
def __init__(self, parent: "RVDatabase", item: minidom.Element) -> None:
self.parent = parent
self.item = item
self.was_downloaded = False
self.watched = False
def get_tag_data(self, tag_name: str) -> str:
nodes = self.item.getElementsByTagName(tag_name)
@ -101,6 +106,8 @@ class RVElement:
log.debug(f"From cache: {self}")
if cache.was_downloaded:
self.was_downloaded = True
if cache.watched:
self.watched = True
def __str__(self) -> str:
return f"{self.guid}: {self.creator} {self.title} {self.link}"
@ -204,6 +211,8 @@ class RVElement:
def matches_search(self, args: configargparse.Namespace) -> bool:
if not self.is_video:
return False
if self.watched:
return False
if args.title and not re.search(args.title, self.title):
return False
if args.creator and not re.search(args.creator, self.creator):
@ -240,6 +249,16 @@ class RVElement:
return False
return True
def watch(self) -> None:
if not self.downloaded:
self.download()
proc = subprocess.run(['mpv', self.filepath])
proc.check_returncode()
self.watched = True
self.parent.save()
class RVDatabase:
SAVE_FILE = ".cache.p"
@ -400,11 +419,20 @@ def get_args() -> configargparse.Namespace:
sc_list = subparsers.add_parser("list")
sc_list.set_defaults(subcommand=RVCommand.list)
sc_list.add("--guid", help="Regex to filter guid")
sc_list.add("--creator", help="Regex to filter by creator")
sc_list.add("--title", help="Regex to filter by title")
sc_list.add("--link", help="Regex to filter by link")
sc_list.add("--duration", help="Comparative to filter by duration")
sc_watch = subparsers.add_parser("watch")
sc_watch.set_defaults(subcommand=RVCommand.watch)
sc_watch.add("order", choices=("old", "new", "random"), nargs="?", default="old", help="Watch X first")
# TODO Command to watch multiple
# Common arguments for filtering
for sc in (sc_list, sc_watch):
sc.add("--guid", help="Regex to filter guid")
sc.add("--creator", help="Regex to filter by creator")
sc.add("--title", help="Regex to filter by title")
sc.add("--link", help="Regex to filter by link")
sc.add("--duration", help="Comparative to filter by duration")
args = parser.parse_args()
args.videos = os.path.realpath(os.path.expanduser(args.videos))
@ -419,6 +447,8 @@ def main() -> None:
os.makedirs(args.videos, exist_ok=True)
os.chdir(args.videos)
# TODO Abstract a bit
if args.subcommand == RVCommand.download:
database = RVDatabase(args)
database.read_feed()
@ -438,6 +468,21 @@ def main() -> None:
continue
print(element)
elif args.subcommand == RVCommand.watch:
cache = RVDatabase.load()
if not cache:
raise FileNotFoundError("This command doesn't work without a cache yet.")
elements = cache.elements.copy()
if args.order == "new":
elements = reversed(elements)
elif args.order == "random":
random.shuffle(elements)
for element in elements:
if not element.matches_search(args):
continue
element.watch()
break
if __name__ == "__main__":
main()