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

Fix manifest

This commit is contained in:
Cadence Ember 2020-12-18 19:54:06 +13:00
parent e95d814709
commit 20b133dbb6
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17
2 changed files with 44 additions and 33 deletions

View File

@ -7,7 +7,7 @@ import traceback
import youtube_dlc 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 from tools.extractors import extract_yt_initial_data, extract_yt_initial_player_response
from math import floor from math import floor
from cachetools import TTLCache from cachetools import TTLCache
@ -19,7 +19,7 @@ ytdl_opts = {
"playlist_items": "1-100", "playlist_items": "1-100",
"extract_flat": "in_playlist", "extract_flat": "in_playlist",
"write_pages": True, "write_pages": True,
"source_address": "0.0.0.0" "source_address": "0.0.0.0"
} }
ytdl = youtube_dlc.YoutubeDL(ytdl_opts) ytdl = youtube_dlc.YoutubeDL(ytdl_opts)
@ -255,39 +255,41 @@ def get_more_stuff_from_file(id, result):
"second__liveNow": is_live(r) "second__liveNow": is_live(r)
} for r in [get_useful_recommendation_data(r) for r in recommendations if get_useful_recommendation_data(r)]) } for r in [get_useful_recommendation_data(r) for r in recommendations if get_useful_recommendation_data(r)])
m_yt_player_config = re.search(r_yt_player_config, content) # m_yt_player_config = re.search(r_yt_player_config, content)
if m_yt_player_config: # if m_yt_player_config:
yt_player_config = json.loads(m_yt_player_config.group(1)) # yt_player_config = json.loads(m_yt_player_config.group(1))
player_response = json.loads(yt_player_config["args"]["player_response"])
if "dashManifestUrl" in player_response["streamingData"]:
result["second__providedDashUrl"] = player_response["streamingData"]["dashManifestUrl"]
result["liveNow"] = player_response["videoDetails"]["isLiveContent"]
# result = player_response player_response = extract_yt_initial_player_response(content)
# return result
itagDict = {} # result = player_response
for f in player_response["streamingData"]["adaptiveFormats"]: # return result
if "indexRange" in f:
itagDict[str(f["itag"])] = { if "dashManifestUrl" in player_response["streamingData"]:
"initRange": f["initRange"], result["second__providedDashUrl"] = player_response["streamingData"]["dashManifestUrl"]
"indexRange": f["indexRange"], result["liveNow"] = player_response["videoDetails"]["isLiveContent"]
"audioChannels": f["audioChannels"] if "audioChannels" in f else None
} itagDict = {}
for f in result["adaptiveFormats"]: for f in player_response["streamingData"]["adaptiveFormats"]:
if f["itag"] in itagDict: if "indexRange" in f:
i = itagDict[f["itag"]] itagDict[str(f["itag"])] = {
f["init"] = "{}-{}".format(i["initRange"]["start"], i["initRange"]["end"]) "initRange": f["initRange"],
f["index"] = "{}-{}".format(i["indexRange"]["start"], i["indexRange"]["end"]) "indexRange": f["indexRange"],
f["second__audioChannels"] = i["audioChannels"] "audioChannels": f["audioChannels"] if "audioChannels" in f else None
if f["second__height"]: }
resolution = str(f["second__height"]) + "p" for f in result["adaptiveFormats"]:
f["resolution"] = resolution if f["itag"] in itagDict:
label = resolution i = itagDict[f["itag"]]
if f["fps"] > 30: f["init"] = "{}-{}".format(i["initRange"]["start"], i["initRange"]["end"])
label += str(f["fps"]) f["index"] = "{}-{}".format(i["indexRange"]["start"], i["indexRange"]["end"])
f["qualityLabel"] = label f["second__audioChannels"] = i["audioChannels"]
f["second__order"] = format_order(f) if f["second__height"]:
resolution = str(f["second__height"]) + "p"
f["resolution"] = resolution
label = resolution
if f["fps"] > 30:
label += str(f["fps"])
f["qualityLabel"] = label
f["second__order"] = format_order(f)
except Exception: except Exception:
print("messed up extracting recommendations.") print("messed up extracting recommendations.")

View File

@ -2,6 +2,7 @@ import re
import json import json
r_yt_initial_data = re.compile(r"""(?:^\s*window\["ytInitialData"\]|var ytInitialData) = (\{.+?\});(?:\s*$|</script>)""", re.S + re.M) r_yt_initial_data = re.compile(r"""(?:^\s*window\["ytInitialData"\]|var ytInitialData) = (\{.+?\});(?:\s*$|</script>)""", re.S + re.M)
r_yt_initial_player_response = re.compile(r"""(?:^\s*window\["ytInitialPlayerResponse"\]|var ytInitialPlayerResponse) = (\{.+?\});(?:\s*$|</script>|var )""", re.S + re.M)
def extract_yt_initial_data(content): def extract_yt_initial_data(content):
m_yt_initial_data = re.search(r_yt_initial_data, content) m_yt_initial_data = re.search(r_yt_initial_data, content)
@ -10,3 +11,11 @@ def extract_yt_initial_data(content):
return yt_initial_data return yt_initial_data
else: else:
raise Exception("Could not match ytInitialData in content") raise Exception("Could not match ytInitialData in content")
def extract_yt_initial_player_response(content):
m_yt_initial_player_response = re.search(r_yt_initial_player_response, content)
if m_yt_initial_player_response:
yt_initial_player_response = json.loads(m_yt_initial_player_response.group(1))
return yt_initial_player_response
else:
raise Exception("Could not match ytInitialPlayerResponse in content")