rssVideos: Filter by duration

This commit is contained in:
Geoffrey Frogeye 2021-12-17 22:42:35 +01:00
parent 7aeecb1bff
commit 7423a93203
Signed by: geoffrey
GPG key ID: C72403E7F82E6AD8

View file

@ -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))