diff options
author | Miguel Ángel Moreno <mail@migalmoreno.com> | 2024-11-05 00:02:10 +0100 |
---|---|---|
committer | Miguel Ángel Moreno <mail@migalmoreno.com> | 2024-11-05 01:18:11 +0100 |
commit | 01c3e19c6b0964a5947dad514533eb34a085dfb7 (patch) | |
tree | c5bf59a03408ad3f9b7358dada6848fd15908e46 /src/backend | |
parent | e3a01a35348e28469238b03717aa78cd2a4eedc2 (diff) |
chore: apply formatting and linting
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/tubo/core.clj | 8 | ||||
-rw-r--r-- | src/backend/tubo/downloader_impl.clj | 70 | ||||
-rw-r--r-- | src/backend/tubo/handler.clj | 57 | ||||
-rw-r--r-- | src/backend/tubo/http.clj | 9 | ||||
-rw-r--r-- | src/backend/tubo/routes.clj | 32 |
5 files changed, 90 insertions, 86 deletions
diff --git a/src/backend/tubo/core.clj b/src/backend/tubo/core.clj index a587909..48a8897 100644 --- a/src/backend/tubo/core.clj +++ b/src/backend/tubo/core.clj @@ -3,10 +3,6 @@ (:require [tubo.http :as http])) -(defn -main - [& _] - (http/start-server!)) +(defn -main [& _] (http/start-server!)) -(defn reset - [] - (http/stop-server!)) +(defn reset [] (http/stop-server!)) diff --git a/src/backend/tubo/downloader_impl.clj b/src/backend/tubo/downloader_impl.clj index e22a2da..7cb0872 100644 --- a/src/backend/tubo/downloader_impl.clj +++ b/src/backend/tubo/downloader_impl.clj @@ -1,33 +1,36 @@ (ns tubo.downloader-impl (:import - [org.schabi.newpipe.extractor.downloader Response Request] + [org.schabi.newpipe.extractor.downloader Response] [okhttp3 Request$Builder OkHttpClient$Builder RequestBody])) (gen-class - :name tubo.DownloaderImpl + :name tubo.DownloaderImpl :constructors {[okhttp3.OkHttpClient$Builder] []} - :extends org.schabi.newpipe.extractor.downloader.Downloader - :init downloader-impl) + :extends org.schabi.newpipe.extractor.downloader.Downloader + :init downloader-impl) (gen-class - :name tubo.DownloaderImpl + :name tubo.DownloaderImpl :constructors {[okhttp3.OkHttpClient$Builder] []} - :extends org.schabi.newpipe.extractor.downloader.Downloader - :prefix "-" - :main false - :state state - :init downloader-impl - :methods [#^{:static true} [init [] tubo.DownloaderImpl] - #^{:static true} [getInstance [] tubo.DownloaderImpl]]) + :extends org.schabi.newpipe.extractor.downloader.Downloader + :prefix "-" + :main false + :state state + :init downloader-impl + :methods [#^{:static true} [init [] tubo.DownloaderImpl] + #^{:static true} [getInstance [] tubo.DownloaderImpl]]) -(def user-agent "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0") +(def user-agent + "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0") (def instance (atom nil)) -(defn -downloader-impl [builder] - [[] (atom {:client - (.. builder - (readTimeout 30 (java.util.concurrent.TimeUnit/SECONDS)) - (build))})]) +(defn -downloader-impl + [builder] + [[] + (atom {:client + (.. builder + (readTimeout 30 (java.util.concurrent.TimeUnit/SECONDS)) + (build))})]) (defn -init ([] @@ -35,33 +38,37 @@ ([builder] (reset! instance (tubo.DownloaderImpl. builder)))) -(defn -getInstance [] +(defn -getInstance + [] (or @instance (-init))) -(defn -execute [this request] - (let [http-method (.httpMethod request) - url (.url request) - headers (.headers request) - data-to-send (.dataToSend request) - request-body (and data-to-send (RequestBody/create nil data-to-send)) +(defn -execute + [this request] + (let [http-method (.httpMethod request) + url (.url request) + headers (.headers request) + data-to-send (.dataToSend request) + request-body (and data-to-send (RequestBody/create nil data-to-send)) request-builder (.. (Request$Builder.) (method http-method request-body) (url url) (addHeader "User-Agent" user-agent))] (doseq [pair (.entrySet headers)] - (let [header-name (.getKey pair) + (let [header-name (.getKey pair) header-value-list (.getValue pair)] (if (> (.size header-value-list) 1) (do (.removeHeader request-builder header-name) (doseq [header-value header-value-list] (.addHeader request-builder header-name header-value))) - (if (= (.size header-value-list) 1) + (when (= (.size header-value-list) 1) (.header request-builder header-name (.get header-value-list 0)))))) - (let [response (.. (@(.state this) :client) (newCall (.build request-builder)) (execute)) - body (.body response) + (let [response (.. (@(.state this) :client) + (newCall (.build request-builder)) + (execute)) + body (.body response) response-body-to-return (and body (.string body)) - latest-url (.. response (request) (url) (toString))] + latest-url (.. response (request) (url) (toString))] (when (= (.code response) 429) (.close response)) (Response. (.code response) @@ -70,4 +77,5 @@ response-body-to-return latest-url)))) -(comment (compile 'tubo.downloader-impl)) +(comment + (compile 'tubo.downloader-impl)) diff --git a/src/backend/tubo/handler.clj b/src/backend/tubo/handler.clj index 4b37494..ab66d0f 100644 --- a/src/backend/tubo/handler.clj +++ b/src/backend/tubo/handler.clj @@ -3,11 +3,7 @@ [clojure.string :as str] [hiccup.page :as hiccup] [ring.util.response :refer [response]] - [tubo.api.streams :as streams] - [tubo.api.channels :as channels] - [tubo.api.playlists :as playlists] - [tubo.api.comments :as comments] - [tubo.api.services :as services])) + [tubo.api :as api])) (defn index [_] @@ -17,9 +13,10 @@ [:meta {:charset "UTF-8"}] [:meta {:name "viewport" - :content "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"}] + :content + "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"}] [:title "Tubo"] - [:link {:rel "icon" :type "image/png" :href "/icons/tubo.svg"}] + [:link {:rel "icon" :type "image/svg+xml" :href "/icons/tubo.svg"}] (hiccup/include-css "/styles/index.css")] [:body [:div#app] @@ -28,46 +25,54 @@ (defn search [{:keys [parameters] :as req}] - (let [{:keys [service-id]} (:path parameters) - {:keys [q]} (:query parameters) + (let [{:keys [service-id]} (:path parameters) + {:keys [q]} (:query parameters) {:strs [contentFilters sortFilter nextPage]} (:query-params req) - content-filters (and contentFilters (str/split contentFilters #","))] + content-filters (and contentFilters + (str/split + contentFilters + #","))] (response (if nextPage - (services/search service-id q contentFilters sortFilter nextPage) - (services/search service-id q contentFilters sortFilter))))) + (api/get-search service-id q contentFilters sortFilter nextPage) + (api/get-search service-id q contentFilters sortFilter))))) (defn channel [{{:keys [url]} :path-params {:strs [nextPage]} :query-params}] (response (if nextPage - (channels/get-channel url nextPage) - (channels/get-channel url)))) + (api/get-channel url nextPage) + (api/get-channel url)))) (defn playlist [{{:keys [url]} :path-params {:strs [nextPage]} :query-params}] (response (if nextPage - (playlists/get-playlist url nextPage) - (playlists/get-playlist url)))) + (api/get-playlist url nextPage) + (api/get-playlist url)))) (defn comments [{{:keys [url]} :path-params {:strs [nextPage]} :query-params}] (response (if nextPage - (comments/get-comments url nextPage) - (comments/get-comments url)))) + (api/get-comments url nextPage) + (api/get-comments url)))) (defn services [_] - (response (services/get-services))) + (response (api/get-services))) (defn kiosks [{{{:keys [service-id]} :path} :parameters}] - (response (services/get-kiosks service-id))) + (response (api/get-kiosks service-id))) (defn kiosk - [{{{:keys [kiosk-id service-id]} :path} :parameters {:strs [nextPage]} :query-params}] + [{{{:keys [kiosk-id service-id]} :path} :parameters + {:strs [nextPage]} :query-params}] (response (cond - (and kiosk-id service-id nextPage) (services/get-kiosk kiosk-id service-id nextPage) - (and kiosk-id service-id) (services/get-kiosk kiosk-id service-id) - :else (services/get-kiosk service-id)))) + (and kiosk-id service-id nextPage) (api/get-kiosk kiosk-id + service-id + nextPage) + (and kiosk-id service-id) (api/get-kiosk kiosk-id + service-id) + :else (api/get-kiosk service-id)))) -(defn stream [{{:keys [url]} :path-params}] - (response (streams/get-stream url))) +(defn stream + [{{:keys [url]} :path-params}] + (response (api/get-stream url))) diff --git a/src/backend/tubo/http.clj b/src/backend/tubo/http.clj index 9ee87f3..200b52e 100644 --- a/src/backend/tubo/http.clj +++ b/src/backend/tubo/http.clj @@ -10,15 +10,10 @@ (defonce server (atom nil)) (defn start-server! - ([] - (start-server! 3000)) + ([] (start-server! 3000)) ([port] (NewPipe/init (DownloaderImpl/init) (Localization. "en" "US")) (reset! server (run-server #'routes/app {:port port})) (println "Server running in port" port))) -(defn stop-server! - [] - (when @server - (@server :timeout 100) - (reset! server nil))) +(defn stop-server! [] (when @server (@server :timeout 100) (reset! server nil))) diff --git a/src/backend/tubo/routes.clj b/src/backend/tubo/routes.clj index edf4876..7a3a456 100644 --- a/src/backend/tubo/routes.clj +++ b/src/backend/tubo/routes.clj @@ -1,14 +1,11 @@ (ns tubo.routes (:require - [malli.experimental.lite :as l] [reitit.ring :as ring] - [reitit.coercion :as coercion] [reitit.ring.coercion :as rrc] [reitit.coercion.malli] [ring.middleware.reload :refer [wrap-reload]] [ring.middleware.params :refer [wrap-params]] [ring.middleware.json :refer [wrap-json-response]] - [ring.middleware.cors :refer [wrap-cors]] [tubo.handler :as handler])) (def router @@ -26,21 +23,24 @@ ["/services" ["" {:get handler/services}] ["/:service-id/search" - {:get {:coercion reitit.coercion.malli/coercion - :parameters {:path {:service-id int?} + {:get {:coercion reitit.coercion.malli/coercion + :parameters {:path {:service-id int?} :query {:q string?}} - :handler handler/search}}] + :handler handler/search}}] ["/:service-id" - ["/default-kiosk" {:get {:coercion reitit.coercion.malli/coercion - :parameters {:path {:service-id int?}} - :handler handler/kiosk}}] + ["/default-kiosk" + {:get {:coercion reitit.coercion.malli/coercion + :parameters {:path {:service-id int?}} + :handler handler/kiosk}}] ["/kiosks" - ["" {:get {:coercion reitit.coercion.malli/coercion - :parameters {:path {:service-id int?}} - :handler handler/kiosks}}] - ["/:kiosk-id" {:get {:coercion reitit.coercion.malli/coercion - :parameters {:path {:service-id int? :kiosk-id string?}} - :handler handler/kiosk}}]]]] + ["" + {:get {:coercion reitit.coercion.malli/coercion + :parameters {:path {:service-id int?}} + :handler handler/kiosks}}] + ["/:kiosk-id" + {:get {:coercion reitit.coercion.malli/coercion + :parameters {:path {:service-id int? :kiosk-id string?}} + :handler handler/kiosk}}]]]] ["/streams/:url" {:get handler/stream}] ["/channels/:url" {:get handler/channel}] ["/playlists/:url" {:get handler/playlist}] @@ -55,7 +55,7 @@ (ring/routes (ring/create-resource-handler {:path "/"}) (ring/create-default-handler - {:not-found (constantly {:status 404, :body "Not found"})})) + {:not-found (constantly {:status 404 :body "Not found"})})) {:middleware [wrap-params [wrap-json-response {:pretty true}] wrap-reload]})) |