From 268457394ffb3ae6e4a7b488977c27de6f6bac8d Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 26 Jan 2021 01:05:40 +1300 Subject: [PATCH] Split out file cleanup code --- extractors/video.py | 17 ++++------------- tools/files.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 13 deletions(-) create mode 100644 tools/files.py diff --git a/extractors/video.py b/extractors/video.py index 4e2656f..11d7d73 100644 --- a/extractors/video.py +++ b/extractors/video.py @@ -8,6 +8,7 @@ import youtube_dlc import urllib.error from tools.converters import * from tools.extractors import extract_yt_initial_data, extract_yt_initial_player_response +import tools.files as files from math import floor from cachetools import TTLCache @@ -25,16 +26,6 @@ ytdl_opts = { } ytdl = youtube_dlc.YoutubeDL(ytdl_opts) -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 - return (f for f in os.listdir() if f.startswith("{}_".format(id))) - -def clean_up_temp_files(id): - created_files = get_created_files(id) - for file in created_files: - os.unlink(file) - def format_order(format): # most significant to least significant # key, max, order, transform @@ -195,7 +186,7 @@ def extract_video(id): return result except youtube_dlc.DownloadError as e: - clean_up_temp_files(id) + files.clean_up_temp_files(id) if isinstance(e.exc_info[1], urllib.error.HTTPError): if e.exc_info[1].code == 429: @@ -217,13 +208,13 @@ def extract_video(id): print("messed up in original transform.") finally: - clean_up_temp_files(id) + files.clean_up_temp_files(id) return result def get_more_stuff_from_file(id, result): # Figure out what the name of the saved file was recommendations = [] - created_files = get_created_files(id) + created_files = files.get_created_files(id) possible_files = [f for f in created_files if f[11:].startswith("_https_-_www.youtube.com")] try: if len(possible_files) == 1: diff --git a/tools/files.py b/tools/files.py new file mode 100644 index 0000000..9756add --- /dev/null +++ b/tools/files.py @@ -0,0 +1,11 @@ +import os + +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 + return (f for f in os.listdir() if f.startswith("{}_".format(id))) + +def clean_up_temp_files(id): + created_files = get_created_files(id) + for file in created_files: + os.unlink(file)