Fix extraction for ytInitialData

This commit is contained in:
Omar Roth 2019-07-11 07:27:42 -05:00
parent bb096a0357
commit af592ea8c1
No known key found for this signature in database
GPG key ID: B8254FB7EC3D37F2
5 changed files with 18 additions and 19 deletions

View file

@ -636,8 +636,8 @@ def fetch_channel_community(ucid, continuation, locale, config, kemal_config, fo
ucid = response.body.match(/https:\/\/www.youtube.com\/channel\/(?<ucid>UC[a-zA-Z0-9_-]{22})/).not_nil!["ucid"] ucid = response.body.match(/https:\/\/www.youtube.com\/channel\/(?<ucid>UC[a-zA-Z0-9_-]{22})/).not_nil!["ucid"]
if !continuation || continuation.empty? if !continuation || continuation.empty?
response = JSON.parse(response.body.match(/window\["ytInitialData"\] = (?<info>.*?);\n/).try &.["info"] || "{}") initial_data = extract_initial_data(response.body)
body = response["contents"]?.try &.["twoColumnBrowseResultsRenderer"]["tabs"].as_a.select { |tab| tab["tabRenderer"]?.try &.["selected"].as_bool.== true }[0]? body = initial_data["contents"]?.try &.["twoColumnBrowseResultsRenderer"]["tabs"].as_a.select { |tab| tab["tabRenderer"]?.try &.["selected"].as_bool.== true }[0]?
if !body if !body
raise "Could not extract community tab." raise "Could not extract community tab."

View file

@ -826,3 +826,12 @@ def create_notification_stream(env, config, kemal_config, decrypt_function, topi
connection_channel.send({false, connection}) connection_channel.send({false, connection})
end end
end end
def extract_initial_data(body)
initial_data = body.match(/window\["ytInitialData"\] = (?<info>.*?);\n/).try &.["info"] || "{}"
if initial_data.starts_with?("JSON.parse(\"")
return JSON.parse(JSON.parse(%({"initial_data":"#{initial_data[12..-3]}"}))["initial_data"].as_s)
else
return JSON.parse(initial_data)
end
end

View file

@ -28,18 +28,13 @@ def fetch_mix(rdid, video_id, cookies = nil, locale = nil)
end end
response = client.get("/watch?v=#{video_id}&list=#{rdid}&gl=US&hl=en&has_verified=1&bpctr=9999999999", headers) response = client.get("/watch?v=#{video_id}&list=#{rdid}&gl=US&hl=en&has_verified=1&bpctr=9999999999", headers)
yt_data = response.body.match(/window\["ytInitialData"\] = (?<data>.*);/) initial_data = extract_initial_data(response.body)
if yt_data
yt_data = JSON.parse(yt_data["data"].rchop(";")) if !initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]?
else
raise translate(locale, "Could not create mix.") raise translate(locale, "Could not create mix.")
end end
if !yt_data["contents"]["twoColumnWatchNextResults"]["playlist"]? playlist = initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]["playlist"]
raise translate(locale, "Could not create mix.")
end
playlist = yt_data["contents"]["twoColumnWatchNextResults"]["playlist"]["playlist"]
mix_title = playlist["title"].as_s mix_title = playlist["title"].as_s
contents = playlist["contents"].as_a contents = playlist["contents"].as_a

View file

@ -14,14 +14,9 @@ def fetch_trending(trending_type, region, locale)
response = client.get("/feed/trending?gl=#{region}&hl=en", headers).body response = client.get("/feed/trending?gl=#{region}&hl=en", headers).body
yt_data = response.match(/window\["ytInitialData"\] = (?<data>.*);/) initial_data = extract_initial_data(response)
if yt_data
yt_data = JSON.parse(yt_data["data"].rchop(";"))
else
raise translate(locale, "Could not pull trending pages.")
end
tabs = yt_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][0]["tabRenderer"]["content"]["sectionListRenderer"]["subMenu"]["channelListSubMenuRenderer"]["contents"].as_a tabs = initial_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][0]["tabRenderer"]["content"]["sectionListRenderer"]["subMenu"]["channelListSubMenuRenderer"]["contents"].as_a
url = tabs.select { |tab| tab["channelListSubMenuAvatarRenderer"]["title"]["simpleText"] == trending_type }[0]? url = tabs.select { |tab| tab["channelListSubMenuAvatarRenderer"]["title"]["simpleText"] == trending_type }[0]?
if url if url

View file

@ -925,7 +925,7 @@ def extract_polymer_config(body, html)
end end
end end
initial_data = JSON.parse(body.match(/window\["ytInitialData"\] = (?<info>.*?);\n/).try &.["info"] || "{}") initial_data = extract_initial_data(body)
primary_results = initial_data["contents"]? primary_results = initial_data["contents"]?
.try &.["twoColumnWatchNextResults"]? .try &.["twoColumnWatchNextResults"]?