invidious-mod-JP/src/invidious/mixes.cr

120 lines
3.1 KiB
Crystal
Raw Normal View History

2019-03-29 21:30:02 +00:00
struct MixVideo
include DB::Serializable
property title : String
property id : String
property author : String
property ucid : String
property length_seconds : Int32
property index : Int32
property rdid : String
2018-09-29 04:12:35 +00:00
end
2019-03-29 21:30:02 +00:00
struct Mix
include DB::Serializable
property title : String
property id : String
property videos : Array(MixVideo)
2018-09-29 04:12:35 +00:00
end
2018-12-20 21:32:09 +00:00
def fetch_mix(rdid, video_id, cookies = nil, locale = nil)
2018-09-29 04:12:35 +00:00
headers = HTTP::Headers.new
if cookies
headers = cookies.add_request_headers(headers)
end
2020-07-26 15:50:18 +00:00
video_id = "CvFH_6DNRCY" if rdid.starts_with? "OLAK5uy_"
response = YT_POOL.client &.get("/watch?v=#{video_id}&list=#{rdid}&gl=US&hl=en", headers)
2019-07-11 12:27:42 +00:00
initial_data = extract_initial_data(response.body)
2018-09-29 04:12:35 +00:00
2019-07-11 12:27:42 +00:00
if !initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]?
2018-12-20 21:32:09 +00:00
raise translate(locale, "Could not create mix.")
2018-10-08 02:11:33 +00:00
end
2019-07-11 12:27:42 +00:00
playlist = initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]["playlist"]
2018-09-29 04:12:35 +00:00
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 04:12:35 +00:00
end
videos = [] of MixVideo
contents.each do |item|
item = item["playlistPanelVideoRenderer"]
id = item["videoId"].as_s
2019-02-15 23:28:54 +00:00
title = item["title"]?.try &.["simpleText"].as_s
next if !title
2018-09-29 04:12:35 +00:00
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: title,
id: id,
author: author,
ucid: ucid,
length_seconds: length_seconds,
index: index,
rdid: rdid,
})
2018-09-29 04:12:35 +00:00
end
if !cookies
2018-12-20 21:32:09 +00:00
next_page = fetch_mix(rdid, videos[-1].id, response.cookies, locale)
2018-09-29 04:12:35 +00:00
videos += next_page.videos
end
videos.uniq! { |video| video.id }
videos = videos.first(50)
return Mix.new({
title: mix_title,
id: rdid,
videos: videos,
})
2018-09-29 04:12:35 +00:00
end
2018-10-08 02:11:33 +00:00
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 02:11:33 +00:00
<p style="width:100%">#{video["title"]}</p>
<p>
2019-05-02 01:03:39 +00:00
<b style="width:100%">#{video["author"]}</b>
2018-10-08 02:11:33 +00:00
</p>
</a>
</li>
END_HTML
end
html += <<-END_HTML
</ol>
</div>
<hr>
END_HTML
html
end