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 logging
import os import os
import pickle import pickle
import random
import re import re
import subprocess
import sys import sys
import typing import typing
import urllib.parse import urllib.parse
@ -43,17 +45,20 @@ def configure_logging(args: configargparse.Namespace) -> None:
class RVCommand(enum.Enum): class RVCommand(enum.Enum):
download = "download" download = "download"
list = "list" list = "list"
watch = "watch"
class RVElement: class RVElement:
parent: "RVDatabase" parent: "RVDatabase"
item: minidom.Element item: minidom.Element
was_downloaded: bool was_downloaded: bool
watched: bool
def __init__(self, parent: "RVDatabase", item: minidom.Element) -> None: def __init__(self, parent: "RVDatabase", item: minidom.Element) -> None:
self.parent = parent self.parent = parent
self.item = item self.item = item
self.was_downloaded = False self.was_downloaded = False
self.watched = False
def get_tag_data(self, tag_name: str) -> str: def get_tag_data(self, tag_name: str) -> str:
nodes = self.item.getElementsByTagName(tag_name) nodes = self.item.getElementsByTagName(tag_name)
@ -101,6 +106,8 @@ class RVElement:
log.debug(f"From cache: {self}") log.debug(f"From cache: {self}")
if cache.was_downloaded: if cache.was_downloaded:
self.was_downloaded = True self.was_downloaded = True
if cache.watched:
self.watched = True
def __str__(self) -> str: def __str__(self) -> str:
return f"{self.guid}: {self.creator} {self.title} {self.link}" return f"{self.guid}: {self.creator} {self.title} {self.link}"
@ -204,6 +211,8 @@ class RVElement:
def matches_search(self, args: configargparse.Namespace) -> bool: def matches_search(self, args: configargparse.Namespace) -> bool:
if not self.is_video: if not self.is_video:
return False return False
if self.watched:
return False
if args.title and not re.search(args.title, self.title): if args.title and not re.search(args.title, self.title):
return False return False
if args.creator and not re.search(args.creator, self.creator): if args.creator and not re.search(args.creator, self.creator):
@ -240,6 +249,16 @@ class RVElement:
return False return False
return True 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: class RVDatabase:
SAVE_FILE = ".cache.p" SAVE_FILE = ".cache.p"
@ -400,11 +419,20 @@ def get_args() -> configargparse.Namespace:
sc_list = subparsers.add_parser("list") sc_list = subparsers.add_parser("list")
sc_list.set_defaults(subcommand=RVCommand.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_watch = subparsers.add_parser("watch")
sc_list.add("--title", help="Regex to filter by title") sc_watch.set_defaults(subcommand=RVCommand.watch)
sc_list.add("--link", help="Regex to filter by link") sc_watch.add("order", choices=("old", "new", "random"), nargs="?", default="old", help="Watch X first")
sc_list.add("--duration", help="Comparative to filter by duration")
# 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 = parser.parse_args()
args.videos = os.path.realpath(os.path.expanduser(args.videos)) 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.makedirs(args.videos, exist_ok=True)
os.chdir(args.videos) os.chdir(args.videos)
# TODO Abstract a bit
if args.subcommand == RVCommand.download: if args.subcommand == RVCommand.download:
database = RVDatabase(args) database = RVDatabase(args)
database.read_feed() database.read_feed()
@ -438,6 +468,21 @@ def main() -> None:
continue continue
print(element) 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__": if __name__ == "__main__":
main() main()