Add 'playlistThumbnail' to playlist objects

This commit is contained in:
Omar Roth 2019-08-21 19:08:11 -05:00
parent 4c9975a7d9
commit 059f50dad4
No known key found for this signature in database
GPG key ID: B8254FB7EC3D37F2
4 changed files with 36 additions and 41 deletions

View file

@ -4101,8 +4101,10 @@ get "/api/v1/playlists/:plid" do |env|
response = JSON.build do |json| response = JSON.build do |json|
json.object do json.object do
json.field "type", "playlist"
json.field "title", playlist.title json.field "title", playlist.title
json.field "playlistId", playlist.id json.field "playlistId", playlist.id
json.field "playlistThumbnail", playlist.thumbnail
json.field "author", playlist.author json.field "author", playlist.author
json.field "authorId", playlist.ucid json.field "authorId", playlist.ucid

View file

@ -331,18 +331,8 @@ def extract_items(nodeset, ucid = nil, author_name = nil)
next next
end end
anchor = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-byline")]/a)) author_id = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-byline")]/a)).try &.["href"].split("/")[-1] || ucid || ""
if anchor author = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-byline")]/a)).try &.content.strip || author_name || ""
author = anchor.content.strip
author_id = anchor["href"].split("/")[-1]
end
author ||= author_name
author_id ||= ucid
author ||= ""
author_id ||= ""
description_html = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")])).try &.to_s || "" description_html = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")])).try &.to_s || ""
tile = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-tile")])) tile = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-tile")]))
@ -401,13 +391,13 @@ def extract_items(nodeset, ucid = nil, author_name = nil)
playlist_thumbnail ||= node.xpath_node(%q(.//span/img)).try &.["src"] playlist_thumbnail ||= node.xpath_node(%q(.//span/img)).try &.["src"]
items << SearchPlaylist.new( items << SearchPlaylist.new(
title, title: title,
plid, id: plid,
author, author: author,
author_id, ucid: author_id,
video_count, video_count: video_count,
videos, videos: videos,
playlist_thumbnail thumbnail: playlist_thumbnail
) )
when .includes? "yt-lockup-channel" when .includes? "yt-lockup-channel"
author = title.strip author = title.strip
@ -577,13 +567,13 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil)
end end
items << SearchPlaylist.new( items << SearchPlaylist.new(
playlist_title, title: playlist_title,
plid, id: plid,
author_name, author: author_name,
ucid, ucid: ucid,
video_count, video_count: video_count,
videos, videos: videos,
playlist_thumbnail thumbnail: playlist_thumbnail
) )
end end
end end
@ -592,13 +582,13 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil)
plid = HTTP::Params.parse(URI.parse(id).query.not_nil!)["list"] plid = HTTP::Params.parse(URI.parse(id).query.not_nil!)["list"]
items << SearchPlaylist.new( items << SearchPlaylist.new(
title, title: title,
plid, id: plid,
author_name, author: author_name,
ucid, ucid: ucid,
videos.size, video_count: videos.size,
videos, videos: videos,
"/vi/#{videos[0].id}/mqdefault.jpg" thumbnail: "https://i.ytimg.com/vi/#{videos[0].id}/mqdefault.jpg"
) )
end end
end end

View file

@ -51,6 +51,7 @@ struct Playlist
video_count: Int32, video_count: Int32,
views: Int64, views: Int64,
updated: Time, updated: Time,
thumbnail: String?,
}) })
end end
@ -223,6 +224,9 @@ def fetch_playlist(plid, locale)
description_html = document.xpath_node(%q(//span[@class="pl-header-description-text"]/div/div[1])).try &.to_s || description_html = document.xpath_node(%q(//span[@class="pl-header-description-text"]/div/div[1])).try &.to_s ||
document.xpath_node(%q(//span[@class="pl-header-description-text"])).try &.to_s || "" document.xpath_node(%q(//span[@class="pl-header-description-text"])).try &.to_s || ""
playlist_thumbnail = document.xpath_node(%q(//div[@class="pl-header-thumb"]/img)).try &.["data-thumb"]? ||
document.xpath_node(%q(//div[@class="pl-header-thumb"]/img)).try &.["src"]
# YouTube allows anonymous playlists, so most of this can be empty or optional # YouTube allows anonymous playlists, so most of this can be empty or optional
anchor = document.xpath_node(%q(//ul[@class="pl-header-details"])) anchor = document.xpath_node(%q(//ul[@class="pl-header-details"]))
author = anchor.try &.xpath_node(%q(.//li[1]/a)).try &.content author = anchor.try &.xpath_node(%q(.//li[1]/a)).try &.content
@ -234,15 +238,12 @@ def fetch_playlist(plid, locale)
video_count = anchor.try &.xpath_node(%q(.//li[2])).try &.content.gsub(/\D/, "").to_i? video_count = anchor.try &.xpath_node(%q(.//li[2])).try &.content.gsub(/\D/, "").to_i?
video_count ||= 0 video_count ||= 0
views = anchor.try &.xpath_node(%q(.//li[3])).try &.content.delete("No views, ").to_i64?
views = anchor.try &.xpath_node(%q(.//li[3])).try &.content.gsub(/\D/, "").to_i64?
views ||= 0_i64 views ||= 0_i64
updated = anchor.try &.xpath_node(%q(.//li[4])).try &.content.lchop("Last updated on ").lchop("Updated ") updated = anchor.try &.xpath_node(%q(.//li[4])).try &.content.lchop("Last updated on ").lchop("Updated ").try { |date| decode_date(date) }
if updated updated ||= Time.utc
updated = decode_date(updated)
else
updated = Time.utc
end
playlist = Playlist.new( playlist = Playlist.new(
title: title, title: title,
@ -253,7 +254,8 @@ def fetch_playlist(plid, locale)
description_html: description_html, description_html: description_html,
video_count: video_count, video_count: video_count,
views: views, views: views,
updated: updated updated: updated,
thumbnail: playlist_thumbnail,
) )
return playlist return playlist

View file

@ -117,6 +117,7 @@ struct SearchPlaylist
json.field "type", "playlist" json.field "type", "playlist"
json.field "title", self.title json.field "title", self.title
json.field "playlistId", self.id json.field "playlistId", self.id
json.field "playlistThumbnail", self.thumbnail
json.field "author", self.author json.field "author", self.author
json.field "authorId", self.ucid json.field "authorId", self.ucid