Split out file cleanup code

This commit is contained in:
Cadence Ember 2021-01-26 01:05:40 +13:00
parent b454ebd6e5
commit 268457394f
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17
2 changed files with 15 additions and 13 deletions

View File

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

11
tools/files.py Normal file
View File

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