From d2df18ff7519d1230cc51f2ef8682e654152f7d9 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 16 Aug 2021 21:56:32 +1200 Subject: [PATCH] Fix file detection and recommendations --- extractors/video.py | 2 +- tools/files.py | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/extractors/video.py b/extractors/video.py index f4da76e..fe23de7 100644 --- a/extractors/video.py +++ b/extractors/video.py @@ -209,7 +209,7 @@ def get_more_stuff_from_file(id, result): # Figure out what the name of the saved file was recommendations = [] created_files = files.get_created_files(id) - possible_files = [f for f in created_files if f[11:].startswith("_https_-_www.youtube.com")] + possible_files = [f for f in created_files if "_https_-_www.youtube.com_watch" in f] try: if len(possible_files) == 1: filename = possible_files[0] diff --git a/tools/files.py b/tools/files.py index 92cc474..027a486 100644 --- a/tools/files.py +++ b/tools/files.py @@ -1,14 +1,20 @@ import os +import re def get_created_files(id): - if id[0] == "-": - id = "_" + id[1:] # youtube-dl changes - to _ at the start, presumably to not accidentally trigger switches with * in shell - id += "_" + # youtube-dl transforms filenames when saving, for example changing - to _ at the start to presumbly not trigger switches in shell, but also in other strange ways too + patterns = [ + "__+", "_", + "^_*(-_)?", "", + "^-", "_" + ] + trim_id = id + for find, replace in zip(patterns[::-2], patterns[1::-2]): # for each 2 items in the list + trim_id = re.sub(find, replace, trim_id) - # youtube-dl thinks it's a really good idea to do this, for some reason. - trim_id = id.lstrip("_") - if trim_id.startswith("-"): - trim_id = "_" + trim_id[len("-"):] + # all file names then have an underscore before the converted URL + id += "_" + trim_id += "_" return (f for f in os.listdir() if f.startswith(id) or f.startswith(trim_id))