1
0
mirror of https://git.sr.ht/~cadence/NewLeaf synced 2024-11-21 23:27:29 +00:00

Add second__order field

This commit is contained in:
Cadence Ember 2020-08-25 01:47:56 +12:00
parent de3b3db6d0
commit ddf52e6346

View File

@ -7,6 +7,7 @@ import traceback
import youtube_dl import youtube_dl
from tools.converters import * from tools.converters import *
from tools.extractors import extract_yt_initial_data from tools.extractors import extract_yt_initial_data
from math import floor
from cachetools import TTLCache from cachetools import TTLCache
video_cache = TTLCache(maxsize=50, ttl=300) video_cache = TTLCache(maxsize=50, ttl=300)
@ -25,6 +26,27 @@ def get_created_files(id):
id = "_" + id[1:] # youtube-dl changes - to _ at the start, presumably to not accidentally trigger switches with * in shell 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))) return (f for f in os.listdir() if f.startswith("{}_".format(id)))
def format_order(format):
# most significant to least significant
# key, max, order, transform
# asc: lower number comes first, desc: higher number comes first
spec = [
["second__height", 8000, "desc", lambda x: floor(x/96) if x else 0],
["fps", 100, "desc", lambda x: floor(x/10) if x else 0],
["type", " "*60, "asc", lambda x: len(x)],
]
total = 0
for i in range(len(spec)):
s = spec[i]
diff = s[3](format[s[0]])
if s[2] == "asc":
diff = s[3](s[1]) - diff
total += diff
if i+1 < len(spec):
s2 = spec[i+1]
total *= s2[3](s2[1])
return -total
def extract_video(id): def extract_video(id):
if id in video_cache: if id in video_cache:
return video_cache[id] return video_cache[id]
@ -104,7 +126,7 @@ def extract_video(id):
"type": format_type(format), "type": format_type(format),
"second__mime": format_mime(format), "second__mime": format_mime(format),
"second__codecs": format_codecs(format), "second__codecs": format_codecs(format),
"clen": str(format["filesize"]), "clen": str(format["filesize"]) if format["filesize"] else None,
"lmt": None, "lmt": None,
"projectionType": None, "projectionType": None,
"fps": format["fps"], "fps": format["fps"],
@ -113,7 +135,9 @@ def extract_video(id):
"resolution": format["format_note"], "resolution": format["format_note"],
"qualityLabel": format["format_note"], "qualityLabel": format["format_note"],
"second__width": format["width"], "second__width": format["width"],
"second__height": format["height"] "second__height": format["height"],
"second__audioChannels": None,
"second__order": 0
} for format in info["formats"] if format_is_adaptive(format)], } for format in info["formats"] if format_is_adaptive(format)],
"formatStreams": [{ "formatStreams": [{
"url": format["url"], "url": format["url"],
@ -144,6 +168,10 @@ def extract_video(id):
"identifier": "VIDEO_DOES_NOT_EXIST" "identifier": "VIDEO_DOES_NOT_EXIST"
} }
except Exception:
traceback.print_exc()
print("messed up in original transform.")
finally: finally:
created_files = get_created_files(id) created_files = get_created_files(id)
for file in created_files: for file in created_files:
@ -233,6 +261,7 @@ def get_more_stuff_from_file(id, result):
if f["fps"] > 30: if f["fps"] > 30:
label += str(f["fps"]) label += str(f["fps"])
f["qualityLabel"] = label f["qualityLabel"] = label
f["second__order"] = format_order(f)
except Exception: except Exception:
print("messed up extracting recommendations.") print("messed up extracting recommendations.")