1
0
mirror of https://git.sr.ht/~cadence/NewLeaf synced 2024-10-31 21:37:30 +00:00

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 import urllib.error
from tools.converters import * from tools.converters import *
from tools.extractors import extract_yt_initial_data, extract_yt_initial_player_response from tools.extractors import extract_yt_initial_data, extract_yt_initial_player_response
import tools.files as files
from math import floor from math import floor
from cachetools import TTLCache from cachetools import TTLCache
@ -25,16 +26,6 @@ ytdl_opts = {
} }
ytdl = youtube_dlc.YoutubeDL(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): def format_order(format):
# most significant to least significant # most significant to least significant
# key, max, order, transform # key, max, order, transform
@ -195,7 +186,7 @@ def extract_video(id):
return result return result
except youtube_dlc.DownloadError as e: 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 isinstance(e.exc_info[1], urllib.error.HTTPError):
if e.exc_info[1].code == 429: if e.exc_info[1].code == 429:
@ -217,13 +208,13 @@ def extract_video(id):
print("messed up in original transform.") print("messed up in original transform.")
finally: finally:
clean_up_temp_files(id) files.clean_up_temp_files(id)
return result return result
def get_more_stuff_from_file(id, result): def get_more_stuff_from_file(id, result):
# Figure out what the name of the saved file was # Figure out what the name of the saved file was
recommendations = [] 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")] possible_files = [f for f in created_files if f[11:].startswith("_https_-_www.youtube.com")]
try: try:
if len(possible_files) == 1: 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)