From 12b46bbd4152c997bed9a356b1d042a70eb9b174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milien=20Devos?= Date: Fri, 13 Aug 2021 01:00:26 +0200 Subject: [PATCH] switch to innertube API for about channels (#2255) --- src/invidious.cr | 2 -- src/invidious/channels/about.cr | 36 ++++++++------------------------ src/invidious/helpers/helpers.cr | 4 ---- src/invidious/routes/channels.cr | 1 - src/invidious/search.cr | 2 -- src/invidious/videos.cr | 7 ------- 6 files changed, 9 insertions(+), 43 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 5b61b895..c940dadf 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -1338,7 +1338,6 @@ get "/feed/channel/:ucid" do |env| description_html: description_html, length_seconds: 0, live_now: false, - paid: false, premium: false, premiere_timestamp: nil, }) @@ -2154,7 +2153,6 @@ get "/api/v1/channels/:ucid" do |env| json.field "subCount", channel.sub_count json.field "totalViews", channel.total_views json.field "joined", channel.joined.to_unix - json.field "paid", channel.paid json.field "autoGenerated", channel.auto_generated json.field "isFamilyFriendly", channel.is_family_friendly diff --git a/src/invidious/channels/about.cr b/src/invidious/channels/about.cr index 8b0ecfbc..628d5b6f 100644 --- a/src/invidious/channels/about.cr +++ b/src/invidious/channels/about.cr @@ -9,7 +9,6 @@ struct AboutChannel property author_thumbnail : String property banner : String? property description_html : String - property paid : Bool property total_views : Int64 property sub_count : Int32 property joined : Time @@ -29,29 +28,15 @@ struct AboutRelatedChannel end def get_about_info(ucid, locale) - result = YT_POOL.client &.get("/channel/#{ucid}/about?gl=US&hl=en") - if result.status_code != 200 - result = YT_POOL.client &.get("/user/#{ucid}/about?gl=US&hl=en") + begin + # "EgVhYm91dA==" is the base64-encoded protobuf object {"2:string":"about"} + initdata = YoutubeAPI.browse(browse_id: ucid, params: "EgVhYm91dA==") + rescue + raise InfoException.new("Could not get channel info.") end - if md = result.headers["location"]?.try &.match(/\/channel\/(?UC[a-zA-Z0-9_-]{22})/) - raise ChannelRedirect.new(channel_id: md["ucid"]) - end - - if result.status_code != 200 - raise InfoException.new("This channel does not exist.") - end - - about = XML.parse_html(result.body) - if about.xpath_node(%q(//div[contains(@class, "channel-empty-message")])) - raise InfoException.new("This channel does not exist.") - end - - initdata = extract_initial_data(result.body) - if initdata.empty? - error_message = about.xpath_node(%q(//div[@class="yt-alert-content"])).try &.content.strip - error_message ||= translate(locale, "Could not get channel info.") - raise InfoException.new(error_message) + if initdata.dig?("alerts", 0, "alertRenderer", "type") == "ERROR" + raise InfoException.new(initdata["alerts"][0]["alertRenderer"]["text"]["simpleText"].as_s) end if browse_endpoint = initdata["onResponseReceivedActions"]?.try &.[0]?.try &.["navigateAction"]?.try &.["endpoint"]?.try &.["browseEndpoint"]? @@ -76,7 +61,6 @@ def get_about_info(ucid, locale) description = initdata["header"]["interactiveTabbedHeaderRenderer"]["description"]["simpleText"].as_s description_html = HTML.escape(description).gsub("\n", "
") - paid = false is_family_friendly = initdata["microformat"]["microformatDataRenderer"]["familySafe"].as_bool allowed_regions = initdata["microformat"]["microformatDataRenderer"]["availableCountries"].as_a.map { |a| a.as_s } @@ -99,9 +83,8 @@ def get_about_info(ucid, locale) description = initdata["metadata"]["channelMetadataRenderer"]?.try &.["description"]?.try &.as_s? || "" description_html = HTML.escape(description).gsub("\n", "
") - paid = about.xpath_node(%q(//meta[@itemprop="paid"])).not_nil!["content"] == "True" - is_family_friendly = about.xpath_node(%q(//meta[@itemprop="isFamilyFriendly"])).not_nil!["content"] == "True" - allowed_regions = about.xpath_node(%q(//meta[@itemprop="regionsAllowed"])).not_nil!["content"].split(",") + is_family_friendly = initdata["microformat"]["microformatDataRenderer"]["familySafe"].as_bool + allowed_regions = initdata["microformat"]["microformatDataRenderer"]["availableCountries"].as_a.map { |a| a.as_s } related_channels = initdata["contents"]["twoColumnBrowseResultsRenderer"] .["secondaryContents"]?.try &.["browseSecondaryContentsRenderer"]["contents"][0]? @@ -180,7 +163,6 @@ def get_about_info(ucid, locale) author_thumbnail: author_thumbnail, banner: banner, description_html: description_html, - paid: paid, total_views: total_views, sub_count: sub_count, joined: joined, diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index 99d7f440..fb33df1c 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -268,7 +268,6 @@ def extract_item(item : JSON::Any, author_fallback : String? = nil, author_id_fa .try &.["text"]?.try &.["simpleText"]?.try &.as_s.try { |t| decode_length_seconds(t) } || 0 live_now = false - paid = false premium = false premiere_timestamp = i["upcomingEventData"]?.try &.["startTime"]?.try { |t| Time.unix(t.as_s.to_i64) } @@ -281,8 +280,6 @@ def extract_item(item : JSON::Any, author_fallback : String? = nil, author_id_fa when "New", "4K", "CC" # TODO when "Premium" - paid = true - # TODO: Potentially available as i["topStandaloneBadge"]["metadataBadgeRenderer"] premium = true else nil # Ignore @@ -299,7 +296,6 @@ def extract_item(item : JSON::Any, author_fallback : String? = nil, author_id_fa description_html: description_html, length_seconds: length_seconds, live_now: live_now, - paid: paid, premium: premium, premiere_timestamp: premiere_timestamp, }) diff --git a/src/invidious/routes/channels.cr b/src/invidious/routes/channels.cr index efa9056d..6a32988e 100644 --- a/src/invidious/routes/channels.cr +++ b/src/invidious/routes/channels.cr @@ -34,7 +34,6 @@ module Invidious::Routes::Channels sort_by ||= "newest" count, items = get_60_videos(channel.ucid, channel.author, page, channel.auto_generated, sort_by) - items.reject! &.paid end templated "channel" diff --git a/src/invidious/search.cr b/src/invidious/search.cr index 882d21ad..a3fcc7a3 100644 --- a/src/invidious/search.cr +++ b/src/invidious/search.cr @@ -10,7 +10,6 @@ struct SearchVideo property description_html : String property length_seconds : Int32 property live_now : Bool - property paid : Bool property premium : Bool property premiere_timestamp : Time? @@ -91,7 +90,6 @@ struct SearchVideo json.field "publishedText", translate(locale, "`x` ago", recode_date(self.published, locale)) json.field "lengthSeconds", self.length_seconds json.field "liveNow", self.live_now - json.field "paid", self.paid json.field "premium", self.premium json.field "isUpcoming", self.is_upcoming diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index 851db0de..79897985 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -301,7 +301,6 @@ struct Video json.field "likeCount", self.likes json.field "dislikeCount", self.dislikes - json.field "paid", self.paid json.field "premium", self.premium json.field "isFamilyFriendly", self.is_family_friendly json.field "allowedRegions", self.allowed_regions @@ -693,12 +692,6 @@ struct Video items end - def paid - reason = info["playabilityStatus"]?.try &.["reason"]? - paid = reason == "This video requires payment to watch." ? true : false - paid - end - def premium keywords.includes? "YouTube Red" end