From f5dd135ed8b59e278787f1ec092559ad3500a3e0 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 14 Apr 2019 19:04:10 -0500 Subject: [PATCH] Add 'view as playlist' option to trending page --- locales/ar.json | 1 + locales/de.json | 1 + locales/en-US.json | 3 ++- locales/es.json | 1 + locales/eu.json | 1 + locales/fr.json | 1 + locales/it.json | 1 + locales/nb_NO.json | 1 + locales/nl.json | 1 + locales/pl.json | 1 + locales/ru.json | 1 + src/invidious.cr | 4 ++-- src/invidious/trending.cr | 38 +++++++++++++++++++++++++++++++- src/invidious/views/trending.ecr | 7 +++--- 14 files changed, 55 insertions(+), 7 deletions(-) diff --git a/locales/ar.json b/locales/ar.json index 90747982..572ea904 100644 --- a/locales/ar.json +++ b/locales/ar.json @@ -278,6 +278,7 @@ "About": "حول", "Rating: ": "التقييم", "Language: ": "اللغة", + "View as playlist": "", "Default": "الكل", "Music": "الاغانى", "Gaming": "الألعاب", diff --git a/locales/de.json b/locales/de.json index 275e44e3..440f75e9 100644 --- a/locales/de.json +++ b/locales/de.json @@ -278,6 +278,7 @@ "About": "Über", "Rating: ": "Bewertung: ", "Language: ": "Sprache: ", + "View as playlist": "", "Default": "", "Music": "", "Gaming": "", diff --git a/locales/en-US.json b/locales/en-US.json index 7f5e3d3b..88c035b5 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -112,7 +112,7 @@ "Whitelisted regions: ": "Whitelisted regions: ", "Blacklisted regions: ": "Blacklisted regions: ", "Shared `x`": "Shared `x`", - "`x` views": "", + "`x` views": "`x` views", "Premieres in `x`": "Premieres in `x`", "Hi! Looks like you have JavaScript disabled. Click here to view comments, keep in mind it may take a bit longer to load.": "Hi! Looks like you have JavaScript disabled. Click here to view comments, keep in mind it may take a bit longer to load.", "View YouTube comments": "View YouTube comments", @@ -276,6 +276,7 @@ "About": "About", "Rating: ": "Rating: ", "Language: ": "Language: ", + "View as playlist": "View as playlist", "Default": "Default", "Music": "Music", "Gaming": "Gaming", diff --git a/locales/es.json b/locales/es.json index 6c9cc7d5..9cf54fe1 100644 --- a/locales/es.json +++ b/locales/es.json @@ -276,6 +276,7 @@ "About": "Acerca de", "Rating: ": "Valoración: ", "Language: ": "Idioma: ", + "View as playlist": "", "Default": "Por defecto", "Music": "Música", "Gaming": "Videojuegos", diff --git a/locales/eu.json b/locales/eu.json index 5f9d5c09..ddd00a4c 100644 --- a/locales/eu.json +++ b/locales/eu.json @@ -276,6 +276,7 @@ "About": "", "Rating: ": "", "Language: ": "", + "View as playlist": "", "Default": "", "Music": "", "Gaming": "", diff --git a/locales/fr.json b/locales/fr.json index 162ebe38..fec1053e 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -276,6 +276,7 @@ "About": "A Propos", "Rating: ": "Évaluation : ", "Language: ": "Langue : ", + "View as playlist": "", "Default": "Défaut", "Music": "Musique", "Gaming": "Jeux Vidéo", diff --git a/locales/it.json b/locales/it.json index d67df287..9bd72ea5 100644 --- a/locales/it.json +++ b/locales/it.json @@ -276,6 +276,7 @@ "About": "A proposito", "Rating: ": "Punteggio: ", "Language: ": "Lingua: ", + "View as playlist": "", "Default": "Predefinito", "Music": "Musica", "Gaming": "Videogiochi", diff --git a/locales/nb_NO.json b/locales/nb_NO.json index 926f3f91..6d032e6f 100644 --- a/locales/nb_NO.json +++ b/locales/nb_NO.json @@ -276,6 +276,7 @@ "About": "Om", "Rating: ": "Vurdering: ", "Language: ": "Språk: ", + "View as playlist": "", "Default": "Forvalg", "Music": "Musikk", "Gaming": "Spill", diff --git a/locales/nl.json b/locales/nl.json index 4129f378..69c43b5b 100644 --- a/locales/nl.json +++ b/locales/nl.json @@ -276,6 +276,7 @@ "About": "", "Rating: ": "", "Language: ": "", + "View as playlist": "", "Default": "", "Music": "", "Gaming": "", diff --git a/locales/pl.json b/locales/pl.json index ee883259..901892eb 100644 --- a/locales/pl.json +++ b/locales/pl.json @@ -276,6 +276,7 @@ "About": "Informacje", "Rating: ": "Ocena: ", "Language: ": "Język: ", + "View as playlist": "", "Default": "Domyślnie", "Music": "Muzyka", "Gaming": "Gry", diff --git a/locales/ru.json b/locales/ru.json index 811c01de..a287cb80 100644 --- a/locales/ru.json +++ b/locales/ru.json @@ -278,6 +278,7 @@ "About": "О сайте", "Rating: ": "Рейтинг: ", "Language: ": "Язык: ", + "View as playlist": "", "Default": "По-умолчанию", "Music": "Музыка", "Gaming": "Игры", diff --git a/src/invidious.cr b/src/invidious.cr index 6e4ee891..f0d7fbf1 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -1955,7 +1955,7 @@ get "/feed/trending" do |env| region ||= "US" begin - trending = fetch_trending(trending_type, proxies, region, locale) + trending, plid = fetch_trending(trending_type, proxies, region, locale) rescue ex error_message = "#{ex.message}" next templated "error" @@ -3205,7 +3205,7 @@ get "/api/v1/trending" do |env| trending_type = env.params.query["type"]? begin - trending = fetch_trending(trending_type, proxies, region, locale) + trending, plid = fetch_trending(trending_type, proxies, region, locale) rescue ex error_message = {"error" => ex.message}.to_json env.response.status_code = 500 diff --git a/src/invidious/trending.cr b/src/invidious/trending.cr index 15630721..8e55f207 100644 --- a/src/invidious/trending.cr +++ b/src/invidious/trending.cr @@ -7,6 +7,8 @@ def fetch_trending(trending_type, proxies, region, locale) region = region.upcase trending = "" + plid = nil + if trending_type && trending_type != "Default" trending_type = trending_type.downcase.capitalize @@ -23,9 +25,11 @@ def fetch_trending(trending_type, proxies, region, locale) url = tabs.select { |tab| tab["channelListSubMenuAvatarRenderer"]["title"]["simpleText"] == trending_type }[0]? if url + url["channelListSubMenuAvatarRenderer"]["navigationEndpoint"]["commandMetadata"]["webCommandMetadata"]["url"] url = url["channelListSubMenuAvatarRenderer"]["navigationEndpoint"]["commandMetadata"]["webCommandMetadata"]["url"].as_s url += "&disable_polymer=1&gl=#{region}&hl=en" trending = client.get(url).body + plid = extract_plid(url) else trending = client.get("/feed/trending?gl=#{region}&hl=en&disable_polymer=1").body end @@ -37,5 +41,37 @@ def fetch_trending(trending_type, proxies, region, locale) nodeset = trending.xpath_nodes(%q(//ul/li[@class="expanded-shelf-content-item-wrapper"])) trending = extract_videos(nodeset) - return trending + return {trending, plid} +end + +def extract_plid(url) + wrapper = HTTP::Params.parse(URI.parse(url).query.not_nil!)["bp"] + + wrapper = URI.unescape(wrapper) + wrapper = Base64.decode(wrapper) + + # 0xe2 0x02 0x2e + wrapper += 3 + + # 0x0a + wrapper += 1 + + # Looks like "/m/[a-z0-9]{5}", not sure what it does here + + item_size = wrapper[0] + wrapper += 1 + item = wrapper[0, item_size] + wrapper += item.size + + # 0x12 + wrapper += 1 + + plid_size = wrapper[0] + wrapper += 1 + plid = wrapper[0, plid_size] + wrapper += plid.size + + plid = String.new(plid) + + return plid end diff --git a/src/invidious/views/trending.ecr b/src/invidious/views/trending.ecr index efd9999a..d6c25266 100644 --- a/src/invidious/views/trending.ecr +++ b/src/invidious/views/trending.ecr @@ -6,9 +6,10 @@ <%= rendered "components/feed_menu" %>
-
-
-
+
+ <% if plid %> + <%= translate(locale, "View as playlist") %> + <% end %>