From 71d05e8b950266b6af84d0ff904ad2ef75215eec Mon Sep 17 00:00:00 2001 From: Miguel Ángel Moreno Date: Thu, 19 Dec 2024 22:48:19 +0100 Subject: feat: add content country setting --- src/backend/tubo/api.clj | 48 +++++++++++++++++++++++++++++++++++--------- src/backend/tubo/handler.clj | 3 ++- src/backend/tubo/router.clj | 6 ++++-- 3 files changed, 45 insertions(+), 12 deletions(-) (limited to 'src/backend') diff --git a/src/backend/tubo/api.clj b/src/backend/tubo/api.clj index 802057f..6678567 100644 --- a/src/backend/tubo/api.clj +++ b/src/backend/tubo/api.clj @@ -3,10 +3,12 @@ [clojure.java.data :as j] [ring.util.codec :refer [url-decode]]) (:import + java.util.Locale org.schabi.newpipe.extractor.channel.ChannelInfo org.schabi.newpipe.extractor.channel.tabs.ChannelTabInfo org.schabi.newpipe.extractor.channel.tabs.ChannelTabs org.schabi.newpipe.extractor.comments.CommentsInfo + org.schabi.newpipe.extractor.localization.ContentCountry org.schabi.newpipe.extractor.kiosk.KioskInfo org.schabi.newpipe.extractor.playlist.PlaylistInfo org.schabi.newpipe.extractor.search.SearchInfo @@ -270,9 +272,14 @@ :next-page (j/from-java (.getNextPage info))}))) (defn get-kiosk - ([service-id] + ([{:keys [region]} service-id] + (println region) (let [service (NewPipe/getService service-id) - extractor (doto (.getDefaultKioskExtractor (.getKioskList service)) + extractor (doto (.getDefaultKioskExtractor + (if region + (doto (.getKioskList service) + (.forceContentCountry (ContentCountry. region))) + (.getKioskList service))) (.fetchPage)) info (KioskInfo/getInfo extractor)] {:id (.getId info) @@ -280,10 +287,16 @@ :service-id service-id :next-page (j/from-java (.getNextPage info)) :related-streams (get-items (.getRelatedItems info))})) - ([kiosk-id service-id] + ([{:keys [region]} kiosk-id service-id] (let [service (NewPipe/getService service-id) extractor - (doto (.getExtractorById (.getKioskList service) kiosk-id nil) + (doto (.getExtractorById + (if region + (doto (.getKioskList service) + (.forceContentCountry (ContentCountry. region))) + (.getKioskList service)) + kiosk-id + nil) (.fetchPage)) info (KioskInfo/getInfo extractor)] {:id (.getId info) @@ -291,9 +304,15 @@ :service-id service-id :next-page (j/from-java (.getNextPage info)) :related-streams (get-items (.getRelatedItems info))})) - ([kiosk-id service-id page-url] + ([{:keys [region]} kiosk-id service-id page-url] (let [service (NewPipe/getService service-id) - extractor (.getExtractorById (.getKioskList service) kiosk-id nil) + extractor (.getExtractorById + (if region + (doto (.getKioskList service) + (.forceContentCountry (ContentCountry. region))) + (.getKioskList service)) + kiosk-id + nil) url (url-decode page-url) kiosk-info (KioskInfo/getInfo extractor) info (KioskInfo/getMoreItems service @@ -311,9 +330,20 @@ (defn get-service [service] - {:id (.getServiceId service) - :info (j/from-java (.getServiceInfo service)) - :base-url (.getBaseUrl service)}) + {:id (.getServiceId service) + :info (j/from-java (.getServiceInfo service)) + :base-url (.getBaseUrl service) + :supported-languages (map (fn [lang] + {:name (.getDisplayLanguage + (Locale. (.getLanguageCode lang) + (.getCountryCode lang))) + :code (.getLocalizationCode lang)}) + (.getSupportedLocalizations service)) + :supported-countries (map (fn [country] + {:name (.getDisplayCountry + (Locale. "" (.toString country))) + :code (.toString country)}) + (.getSupportedCountries service))}) (defn get-services [] diff --git a/src/backend/tubo/handler.clj b/src/backend/tubo/handler.clj index 8a438b8..eff2f85 100644 --- a/src/backend/tubo/handler.clj +++ b/src/backend/tubo/handler.clj @@ -45,8 +45,9 @@ (defn kiosk [{{{:keys [kiosk-id service-id]} :path} :parameters - {:strs [nextPage]} :query-params}] + {:strs [nextPage region]} :query-params}] (response (apply api/get-kiosk + {:region region} (if kiosk-id (into [kiosk-id] diff --git a/src/backend/tubo/router.clj b/src/backend/tubo/router.clj index 8a031b5..46be06a 100644 --- a/src/backend/tubo/router.clj +++ b/src/backend/tubo/router.clj @@ -28,7 +28,8 @@ {:summary "returns default kiosk entries for a given service" :coercion reitit.coercion.malli/coercion - :parameters {:path {:service-id int?}} + :parameters {:path {:service-id int?} + :query {:region [:maybe string?]}} :handler handler/kiosk}} :api/all-kiosks {:get {:summary "returns all kiosks supported by a given service" @@ -39,7 +40,8 @@ {:summary "returns kiosk entries for a given service and a kiosk ID" :coercion reitit.coercion.malli/coercion - :parameters {:path {:service-id int? :kiosk-id string?}} + :parameters {:path {:service-id int? :kiosk-id string?} + :query {:region [:maybe string?]}} :handler handler/kiosk}} :api/stream {:get {:summary "returns stream data for a given URL" :handler handler/stream}} -- cgit v1.2.3