From 059f50dad477daab63246314198ec7e274493ece Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Wed, 21 Aug 2019 19:08:11 -0500 Subject: [PATCH] Add 'playlistThumbnail' to playlist objects --- src/invidious.cr | 2 ++ src/invidious/helpers/helpers.cr | 56 +++++++++++++------------------- src/invidious/playlists.cr | 18 +++++----- src/invidious/search.cr | 1 + 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 143be96d..0203e3b8 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -4101,8 +4101,10 @@ get "/api/v1/playlists/:plid" do |env| response = JSON.build do |json| json.object do + json.field "type", "playlist" json.field "title", playlist.title json.field "playlistId", playlist.id + json.field "playlistThumbnail", playlist.thumbnail json.field "author", playlist.author json.field "authorId", playlist.ucid diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index 20ff2de6..331f6360 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -331,18 +331,8 @@ def extract_items(nodeset, ucid = nil, author_name = nil) next end - anchor = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-byline")]/a)) - if anchor - author = anchor.content.strip - author_id = anchor["href"].split("/")[-1] - end - - author ||= author_name - author_id ||= ucid - - author ||= "" - author_id ||= "" - + author_id = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-byline")]/a)).try &.["href"].split("/")[-1] || ucid || "" + author = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-byline")]/a)).try &.content.strip || author_name || "" 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")])) @@ -401,13 +391,13 @@ def extract_items(nodeset, ucid = nil, author_name = nil) playlist_thumbnail ||= node.xpath_node(%q(.//span/img)).try &.["src"] items << SearchPlaylist.new( - title, - plid, - author, - author_id, - video_count, - videos, - playlist_thumbnail + title: title, + id: plid, + author: author, + ucid: author_id, + video_count: video_count, + videos: videos, + thumbnail: playlist_thumbnail ) when .includes? "yt-lockup-channel" author = title.strip @@ -577,13 +567,13 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil) end items << SearchPlaylist.new( - playlist_title, - plid, - author_name, - ucid, - video_count, - videos, - playlist_thumbnail + title: playlist_title, + id: plid, + author: author_name, + ucid: ucid, + video_count: video_count, + videos: videos, + thumbnail: playlist_thumbnail ) 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"] items << SearchPlaylist.new( - title, - plid, - author_name, - ucid, - videos.size, - videos, - "/vi/#{videos[0].id}/mqdefault.jpg" + title: title, + id: plid, + author: author_name, + ucid: ucid, + video_count: videos.size, + videos: videos, + thumbnail: "https://i.ytimg.com/vi/#{videos[0].id}/mqdefault.jpg" ) end end diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.cr index d28a4149..7965d990 100644 --- a/src/invidious/playlists.cr +++ b/src/invidious/playlists.cr @@ -51,6 +51,7 @@ struct Playlist video_count: Int32, views: Int64, updated: Time, + thumbnail: String?, }) 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 || 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 anchor = document.xpath_node(%q(//ul[@class="pl-header-details"])) 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 ||= 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 - updated = anchor.try &.xpath_node(%q(.//li[4])).try &.content.lchop("Last updated on ").lchop("Updated ") - if updated - updated = decode_date(updated) - else - updated = Time.utc - end + updated = anchor.try &.xpath_node(%q(.//li[4])).try &.content.lchop("Last updated on ").lchop("Updated ").try { |date| decode_date(date) } + updated ||= Time.utc playlist = Playlist.new( title: title, @@ -253,7 +254,8 @@ def fetch_playlist(plid, locale) description_html: description_html, video_count: video_count, views: views, - updated: updated + updated: updated, + thumbnail: playlist_thumbnail, ) return playlist diff --git a/src/invidious/search.cr b/src/invidious/search.cr index 784e3897..7a36f32e 100644 --- a/src/invidious/search.cr +++ b/src/invidious/search.cr @@ -117,6 +117,7 @@ struct SearchPlaylist json.field "type", "playlist" json.field "title", self.title json.field "playlistId", self.id + json.field "playlistThumbnail", self.thumbnail json.field "author", self.author json.field "authorId", self.ucid