2018-09-29 09:42:35 +05:30
|
|
|
class PlaylistVideo
|
|
|
|
add_mapping({
|
|
|
|
title: String,
|
|
|
|
id: String,
|
|
|
|
author: String,
|
|
|
|
ucid: String,
|
|
|
|
length_seconds: Int32,
|
|
|
|
published: Time,
|
|
|
|
playlists: Array(String),
|
|
|
|
index: Int32,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2018-08-15 20:52:36 +05:30
|
|
|
class Playlist
|
|
|
|
add_mapping({
|
2018-09-05 05:57:10 +05:30
|
|
|
title: String,
|
|
|
|
id: String,
|
|
|
|
author: String,
|
2018-09-25 20:58:40 +05:30
|
|
|
author_thumbnail: String,
|
2018-09-05 05:57:10 +05:30
|
|
|
ucid: String,
|
|
|
|
description: String,
|
|
|
|
description_html: String,
|
|
|
|
video_count: Int32,
|
|
|
|
views: Int64,
|
|
|
|
updated: Time,
|
2018-08-15 20:52:36 +05:30
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2018-10-08 07:41:33 +05:30
|
|
|
def fetch_playlist_videos(plid, page, video_count, continuation = nil)
|
2018-08-15 20:52:36 +05:30
|
|
|
client = make_client(YT_URL)
|
2018-09-23 00:43:10 +05:30
|
|
|
|
2018-10-08 07:41:33 +05:30
|
|
|
if continuation
|
2018-11-04 21:07:12 +05:30
|
|
|
html = client.get("/watch?v=#{continuation}&list=#{plid}&bpctr=#{Time.new.to_unix + 2000}&gl=US&hl=en&disable_polymer=1")
|
2018-10-08 07:41:33 +05:30
|
|
|
html = XML.parse_html(html.body)
|
|
|
|
|
|
|
|
index = html.xpath_node(%q(//span[@id="playlist-current-index"])).try &.content.to_i?
|
|
|
|
if index
|
|
|
|
index -= 1
|
|
|
|
end
|
|
|
|
index ||= 0
|
|
|
|
else
|
2018-09-23 00:43:10 +05:30
|
|
|
index = (page - 1) * 100
|
2018-10-08 07:41:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
if video_count > 100
|
2018-09-23 00:43:10 +05:30
|
|
|
url = produce_playlist_url(plid, index)
|
|
|
|
|
|
|
|
response = client.get(url)
|
|
|
|
response = JSON.parse(response.body)
|
|
|
|
if !response["content_html"]? || response["content_html"].as_s.empty?
|
|
|
|
raise "Playlist is empty"
|
|
|
|
end
|
|
|
|
|
|
|
|
document = XML.parse_html(response["content_html"].as_s)
|
|
|
|
nodeset = document.xpath_nodes(%q(.//tr[contains(@class, "pl-video")]))
|
|
|
|
videos = extract_playlist(plid, nodeset, index)
|
|
|
|
else
|
2018-09-28 20:24:01 +05:30
|
|
|
# Playlist has less than one page of videos, so subsequent pages will be empty
|
2018-09-23 00:43:10 +05:30
|
|
|
if page > 1
|
|
|
|
videos = [] of PlaylistVideo
|
|
|
|
else
|
2018-09-28 20:24:01 +05:30
|
|
|
# Extract first page of videos
|
2018-09-26 04:25:32 +05:30
|
|
|
response = client.get("/playlist?list=#{plid}&gl=US&hl=en&disable_polymer=1")
|
2018-09-23 00:43:10 +05:30
|
|
|
document = XML.parse_html(response.body)
|
|
|
|
nodeset = document.xpath_nodes(%q(.//tr[contains(@class, "pl-video")]))
|
|
|
|
|
|
|
|
videos = extract_playlist(plid, nodeset, 0)
|
2018-10-11 06:17:51 +05:30
|
|
|
if continuation
|
|
|
|
until videos[0].id == continuation
|
|
|
|
videos.shift
|
|
|
|
end
|
|
|
|
end
|
2018-09-23 00:43:10 +05:30
|
|
|
end
|
2018-08-15 20:52:36 +05:30
|
|
|
end
|
|
|
|
|
2018-09-23 00:43:10 +05:30
|
|
|
return videos
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_playlist(plid, nodeset, index)
|
2018-08-15 20:52:36 +05:30
|
|
|
videos = [] of PlaylistVideo
|
|
|
|
|
2018-09-23 00:43:10 +05:30
|
|
|
nodeset.each_with_index do |video, offset|
|
|
|
|
anchor = video.xpath_node(%q(.//td[@class="pl-video-title"]))
|
|
|
|
if !anchor
|
|
|
|
next
|
2018-08-15 20:52:36 +05:30
|
|
|
end
|
2018-09-23 00:43:10 +05:30
|
|
|
|
|
|
|
title = anchor.xpath_node(%q(.//a)).not_nil!.content.strip(" \n")
|
|
|
|
id = anchor.xpath_node(%q(.//a)).not_nil!["href"].lchop("/watch?v=")[0, 11]
|
|
|
|
|
|
|
|
anchor = anchor.xpath_node(%q(.//div[@class="pl-video-owner"]/a))
|
|
|
|
if anchor
|
|
|
|
author = anchor.content
|
|
|
|
ucid = anchor["href"].split("/")[2]
|
|
|
|
else
|
|
|
|
author = ""
|
|
|
|
ucid = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
anchor = video.xpath_node(%q(.//td[@class="pl-video-time"]/div/div[1]))
|
|
|
|
if anchor && !anchor.content.empty?
|
|
|
|
length_seconds = decode_length_seconds(anchor.content)
|
|
|
|
else
|
|
|
|
length_seconds = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
videos << PlaylistVideo.new(
|
|
|
|
title,
|
|
|
|
id,
|
|
|
|
author,
|
|
|
|
ucid,
|
|
|
|
length_seconds,
|
|
|
|
Time.now,
|
|
|
|
[plid],
|
|
|
|
index + offset,
|
|
|
|
)
|
2018-08-15 20:52:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
return videos
|
|
|
|
end
|
|
|
|
|
|
|
|
def produce_playlist_url(id, index)
|
|
|
|
if id.starts_with? "UC"
|
|
|
|
id = "UU" + id.lchop("UC")
|
|
|
|
end
|
|
|
|
ucid = "VL" + id
|
|
|
|
|
2018-09-28 20:24:01 +05:30
|
|
|
meta = [0x08_u8] + write_var_int(index)
|
|
|
|
meta = Slice.new(meta.to_unsafe, meta.size)
|
2018-09-18 03:08:18 +05:30
|
|
|
meta = Base64.urlsafe_encode(meta, false)
|
|
|
|
meta = "PT:#{meta}"
|
|
|
|
|
|
|
|
wrapped = "\x7a"
|
|
|
|
wrapped += meta.bytes.size.unsafe_chr
|
|
|
|
wrapped += meta
|
|
|
|
|
|
|
|
wrapped = Base64.urlsafe_encode(wrapped)
|
|
|
|
meta = URI.escape(wrapped)
|
|
|
|
|
|
|
|
continuation = "\x12"
|
|
|
|
continuation += ucid.size.unsafe_chr
|
|
|
|
continuation += ucid
|
|
|
|
continuation += "\x1a"
|
|
|
|
continuation += meta.bytes.size.unsafe_chr
|
|
|
|
continuation += meta
|
|
|
|
|
|
|
|
continuation = continuation.size.to_u8.unsafe_chr + continuation
|
|
|
|
continuation = "\xe2\xa9\x85\xb2\x02" + continuation
|
|
|
|
|
|
|
|
continuation = Base64.urlsafe_encode(continuation)
|
|
|
|
continuation = URI.escape(continuation)
|
2018-08-15 20:52:36 +05:30
|
|
|
|
2018-09-23 00:43:10 +05:30
|
|
|
url = "/browse_ajax?continuation=#{continuation}"
|
2018-08-15 20:52:36 +05:30
|
|
|
|
|
|
|
return url
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_playlist(plid)
|
|
|
|
client = make_client(YT_URL)
|
2018-09-23 00:43:10 +05:30
|
|
|
|
|
|
|
if plid.starts_with? "UC"
|
|
|
|
plid = "UU#{plid.lchop("UC")}"
|
|
|
|
end
|
|
|
|
|
2018-09-26 04:25:32 +05:30
|
|
|
response = client.get("/playlist?list=#{plid}&hl=en&disable_polymer=1")
|
2018-09-23 22:56:12 +05:30
|
|
|
if response.status_code != 200
|
|
|
|
raise "Invalid playlist."
|
|
|
|
end
|
|
|
|
|
2018-11-04 21:07:12 +05:30
|
|
|
body = response.body.gsub(%(
|
2018-09-14 07:30:39 +05:30
|
|
|
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-link yt-uix-expander-head playlist-description-expander yt-uix-inlineedit-ignore-edit" type="button" onclick=";return false;"><span class="yt-uix-button-content"> less <img alt="" src="/yts/img/pixel-vfl3z5WfW.gif">
|
|
|
|
</span></button>
|
2018-11-04 21:07:12 +05:30
|
|
|
), "")
|
2018-09-14 07:30:39 +05:30
|
|
|
document = XML.parse_html(body)
|
2018-08-15 20:52:36 +05:30
|
|
|
|
2018-09-23 23:02:32 +05:30
|
|
|
title = document.xpath_node(%q(//h1[@class="pl-header-title"]))
|
|
|
|
if !title
|
|
|
|
raise "Playlist does not exist."
|
|
|
|
end
|
|
|
|
title = title.content.strip(" \n")
|
2018-08-15 20:52:36 +05:30
|
|
|
|
2018-09-05 05:57:10 +05:30
|
|
|
description_html = document.xpath_node(%q(//span[@class="pl-header-description-text"]/div/div[1]))
|
2018-09-25 20:58:57 +05:30
|
|
|
description_html ||= document.xpath_node(%q(//span[@class="pl-header-description-text"]))
|
2018-09-14 07:30:39 +05:30
|
|
|
description_html, description = html_to_content(description_html)
|
2018-08-15 20:52:36 +05:30
|
|
|
|
|
|
|
anchor = document.xpath_node(%q(//ul[@class="pl-header-details"])).not_nil!
|
|
|
|
author = anchor.xpath_node(%q(.//li[1]/a)).not_nil!.content
|
2018-09-25 20:58:40 +05:30
|
|
|
author_thumbnail = document.xpath_node(%q(//img[@class="channel-header-profile-image"])).try &.["src"]
|
|
|
|
author_thumbnail ||= ""
|
2018-11-05 20:30:39 +05:30
|
|
|
ucid = anchor.xpath_node(%q(.//li[1]/a)).not_nil!["href"].split("/")[-1]
|
2018-08-15 20:52:36 +05:30
|
|
|
|
2018-08-20 20:55:05 +05:30
|
|
|
video_count = anchor.xpath_node(%q(.//li[2])).not_nil!.content.delete("videos, ").to_i
|
2018-09-18 05:51:13 +05:30
|
|
|
views = anchor.xpath_node(%q(.//li[3])).not_nil!.content.delete("No views, ")
|
|
|
|
if views.empty?
|
|
|
|
views = 0_i64
|
|
|
|
else
|
|
|
|
views = views.to_i64
|
|
|
|
end
|
2018-08-15 20:52:36 +05:30
|
|
|
|
|
|
|
updated = anchor.xpath_node(%q(.//li[4])).not_nil!.content.lchop("Last updated on ").lchop("Updated ")
|
|
|
|
updated = decode_date(updated)
|
|
|
|
|
|
|
|
playlist = Playlist.new(
|
|
|
|
title,
|
|
|
|
plid,
|
|
|
|
author,
|
2018-09-25 20:58:40 +05:30
|
|
|
author_thumbnail,
|
2018-08-15 20:52:36 +05:30
|
|
|
ucid,
|
|
|
|
description,
|
2018-09-05 05:57:10 +05:30
|
|
|
description_html,
|
2018-08-15 20:52:36 +05:30
|
|
|
video_count,
|
|
|
|
views,
|
|
|
|
updated
|
|
|
|
)
|
|
|
|
|
|
|
|
return playlist
|
|
|
|
end
|
2018-10-08 07:41:33 +05:30
|
|
|
|
|
|
|
def template_playlist(playlist)
|
|
|
|
html = <<-END_HTML
|
|
|
|
<h3>
|
|
|
|
<a href="/playlist?list=#{playlist["playlistId"]}">
|
|
|
|
#{playlist["title"]}
|
|
|
|
</a>
|
|
|
|
</h3>
|
|
|
|
<div class="pure-menu pure-menu-scrollable playlist-restricted">
|
|
|
|
<ol class="pure-menu-list">
|
|
|
|
END_HTML
|
|
|
|
|
|
|
|
playlist["videos"].as_a.each do |video|
|
|
|
|
html += <<-END_HTML
|
|
|
|
<li class="pure-menu-item">
|
|
|
|
<a href="/watch?v=#{video["videoId"]}&list=#{playlist["playlistId"]}">
|
|
|
|
<img style="width:100%;" src="/vi/#{video["videoId"]}/mqdefault.jpg">
|
|
|
|
<p style="width:100%">#{video["title"]}</p>
|
|
|
|
<p>
|
|
|
|
<b style="width: 100%">#{video["author"]}</b>
|
|
|
|
</p>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
END_HTML
|
|
|
|
end
|
|
|
|
|
|
|
|
html += <<-END_HTML
|
|
|
|
</ol>
|
|
|
|
</div>
|
|
|
|
<hr>
|
|
|
|
END_HTML
|
|
|
|
|
|
|
|
html
|
|
|
|
end
|