invidious-experimenting/src/invidious/mixes.cr

118 lines
3.1 KiB
Crystal
Raw Normal View History

2019-03-30 03:00:02 +05:30
struct MixVideo
db_mapping({
2018-09-29 09:42:35 +05:30
title: String,
id: String,
author: String,
ucid: String,
length_seconds: Int32,
index: Int32,
2019-06-09 00:01:41 +05:30
rdid: String,
2018-09-29 09:42:35 +05:30
})
end
2019-03-30 03:00:02 +05:30
struct Mix
db_mapping({
2018-09-29 09:42:35 +05:30
title: String,
id: String,
videos: Array(MixVideo),
})
end
2018-12-21 03:02:09 +05:30
def fetch_mix(rdid, video_id, cookies = nil, locale = nil)
2018-09-29 09:42:35 +05:30
client = make_client(YT_URL)
headers = HTTP::Headers.new
headers["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
if cookies
headers = cookies.add_request_headers(headers)
end
2018-11-10 22:20:09 +05:30
response = client.get("/watch?v=#{video_id}&list=#{rdid}&gl=US&hl=en&has_verified=1&bpctr=9999999999", headers)
2018-09-29 09:42:35 +05:30
2019-07-11 17:57:42 +05:30
initial_data = extract_initial_data(response.body)
2018-09-29 09:42:35 +05:30
2019-07-11 17:57:42 +05:30
if !initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]?
2018-12-21 03:02:09 +05:30
raise translate(locale, "Could not create mix.")
2018-10-08 07:41:33 +05:30
end
2019-07-11 17:57:42 +05:30
playlist = initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]["playlist"]
2018-09-29 09:42:35 +05:30
mix_title = playlist["title"].as_s
contents = playlist["contents"].as_a
if contents.map { |video| video["playlistPanelVideoRenderer"]["videoId"] }.includes? video_id
until contents[0]["playlistPanelVideoRenderer"]["videoId"].as_s == video_id
contents.shift
end
2018-09-29 09:42:35 +05:30
end
videos = [] of MixVideo
contents.each do |item|
item = item["playlistPanelVideoRenderer"]
id = item["videoId"].as_s
2019-02-16 04:58:54 +05:30
title = item["title"]?.try &.["simpleText"].as_s
if !title
next
end
2018-09-29 09:42:35 +05:30
author = item["longBylineText"]["runs"][0]["text"].as_s
ucid = item["longBylineText"]["runs"][0]["navigationEndpoint"]["browseEndpoint"]["browseId"].as_s
length_seconds = decode_length_seconds(item["lengthText"]["simpleText"].as_s)
index = item["navigationEndpoint"]["watchEndpoint"]["index"].as_i
videos << MixVideo.new(
title,
id,
author,
ucid,
length_seconds,
index,
2019-06-09 00:01:41 +05:30
rdid
2018-09-29 09:42:35 +05:30
)
end
if !cookies
2018-12-21 03:02:09 +05:30
next_page = fetch_mix(rdid, videos[-1].id, response.cookies, locale)
2018-09-29 09:42:35 +05:30
videos += next_page.videos
end
videos.uniq! { |video| video.id }
videos = videos.first(50)
return Mix.new(mix_title, rdid, videos)
end
2018-10-08 07:41:33 +05:30
def template_mix(mix)
html = <<-END_HTML
<h3>
<a href="/mix?list=#{mix["mixId"]}">
#{mix["title"]}
</a>
</h3>
<div class="pure-menu pure-menu-scrollable playlist-restricted">
<ol class="pure-menu-list">
END_HTML
mix["videos"].as_a.each do |video|
html += <<-END_HTML
<li class="pure-menu-item">
<a href="/watch?v=#{video["videoId"]}&list=#{mix["mixId"]}">
<div class="thumbnail">
<img class="thumbnail" src="/vi/#{video["videoId"]}/mqdefault.jpg">
<p class="length">#{recode_length_seconds(video["lengthSeconds"].as_i)}</p>
</div>
2018-10-08 07:41:33 +05:30
<p style="width:100%">#{video["title"]}</p>
<p>
2019-05-02 06:33:39 +05:30
<b style="width:100%">#{video["author"]}</b>
2018-10-08 07:41:33 +05:30
</p>
</a>
</li>
END_HTML
end
html += <<-END_HTML
</ol>
</div>
<hr>
END_HTML
html
end