From 7423a9320388f5e8e72bf5078373850c42fcde61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Fri, 17 Dec 2021 22:42:35 +0100 Subject: [PATCH] rssVideos: Filter by duration --- config/scripts/rssVideos | 53 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/config/scripts/rssVideos b/config/scripts/rssVideos index 87c8ab7..532b26e 100755 --- a/config/scripts/rssVideos +++ b/config/scripts/rssVideos @@ -136,12 +136,17 @@ class RVElement: return infos @property - def skip(self) -> bool: + def duration(self) -> int: assert self.is_video assert self.ytdl_infos + return self.ytdl_infos["duration"] + + @property + def skip(self) -> bool: + assert self.is_video if ( self.parent.args.max_duration > 0 - and self.ytdl_infos["duration"] > self.parent.args.max_duration + and self.duration > self.parent.args.max_duration ): return True return False @@ -185,6 +190,17 @@ class RVElement: return self.download() + MATCHES_DURATION_MULTIPLIERS = {"s": 1, "m": 60, "h": 3600, None: 1} + + MATCHES_DURATION_COMPARATORS = { + "<": int.__lt__, + "-": int.__lt__, + ">": int.__gt__, + "+": int.__gt__, + "=": int.__eq__, + None: int.__le__, + } + def matches_search(self, args: configargparse.Namespace) -> bool: if not self.is_video: return False @@ -196,6 +212,32 @@ class RVElement: return False if args.link and not re.search(args.link, self.link): return False + if args.duration: + dur = args.duration + + mult_index = dur[-1].lower() + if mult_index.isdigit(): + mult_index = None + else: + dur = dur[:-1] + try: + multiplier = self.MATCHES_DURATION_MULTIPLIERS[mult_index] + except IndexError: + raise ValueError(f"Unknown duration multiplier: {mult_index}") + + comp_index = dur[0] + if comp_index.isdigit(): + comp_index = None + else: + dur = dur[1:] + try: + comparator = self.MATCHES_DURATION_COMPARATORS[comp_index] + except IndexError: + raise ValueError(f"Unknown duration comparator: {comp_index}") + + duration = int(dur) + if not comparator(self.duration, duration * multiplier): + return False return True @@ -359,9 +401,10 @@ 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 creator") - sc_list.add("--title", help="Regex to filter titles") - sc_list.add("--link", help="Regex to filter link") + 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") args = parser.parse_args() args.videos = os.path.realpath(os.path.expanduser(args.videos))