From 9361d6444548748445db1548d0e3a6291d802bd3 Mon Sep 17 00:00:00 2001 From: Miguel Ángel Moreno Date: Mon, 26 Dec 2022 21:46:15 +0100 Subject: feat: Abstract away result items into standalone namespace --- src/backend/tau/api/channel.clj | 37 +++++-------------------- src/backend/tau/api/kiosk.clj | 18 +++--------- src/backend/tau/api/playlist.clj | 29 +++----------------- src/backend/tau/api/result.clj | 59 ++++++++++++++++++++++++++++++++++++++++ src/backend/tau/api/search.clj | 18 +++--------- src/backend/tau/api/stream.clj | 44 ++++-------------------------- 6 files changed, 84 insertions(+), 121 deletions(-) create mode 100644 src/backend/tau/api/result.clj diff --git a/src/backend/tau/api/channel.clj b/src/backend/tau/api/channel.clj index 8ef29e5..89c6a7c 100644 --- a/src/backend/tau/api/channel.clj +++ b/src/backend/tau/api/channel.clj @@ -1,9 +1,8 @@ (ns tau.api.channel (:require - [tau.api.stream :as stream] - [tau.api.playlist :as playlist] [clojure.java.data :as j] - [ring.util.codec :refer [url-decode]]) + [ring.util.codec :refer [url-decode]] + [tau.api.result :as result]) (:import org.schabi.newpipe.extractor.channel.ChannelInfo org.schabi.newpipe.extractor.NewPipe @@ -14,32 +13,9 @@ subscriber-count donation-links next-page related-streams]) -(defrecord ChannelResult - [name description verified? thumbnail-url url - subscriber-count stream-count]) - (defrecord ChannelPage [next-page related-streams]) -(defn get-results - [items] - (map #(case (.name (.getInfoType %)) - "STREAM" (stream/get-result %) - "CHANNEL" (get-result %) - "PLAYLIST" (playlist/get-result %)) - items)) - -(defn get-result - [channel] - (map->ChannelResult - {:name (.getName channel) - :thumbnail-url (.getThumbnailUrl channel) - :url (.getUrl channel) - :description (.getDescription channel) - :subscriber-count (.getSubscriberCount channel) - :stream-count (.getStreamCount channel) - :verified? (.isVerified channel)})) - (defn get-info ([url] (let [info (ChannelInfo/getInfo (url-decode url))] @@ -49,13 +25,14 @@ :verified? (.isVerified info) :banner (.getBannerUrl info) :avatar (.getAvatarUrl info) - :subscriber-count (.getSubscriberCount info) + :description (.getDescription info) + :subscriber-count (if (= (.getSubscriberCount info) -1) false (.getSubscriberCount info)) :donation-links (.getDonationLinks info) :next-page (j/from-java (.getNextPage info)) - :related-streams (get-results (.getRelatedItems info))}))) + :related-streams (result/get-results (.getRelatedItems info))}))) ([url page-url] (let [service (NewPipe/getServiceByUrl (url-decode url)) - info (ChannelInfo/getMoreItems service url (Page. (url-decode page-url)))] + info (ChannelInfo/getMoreItems service (url-decode url) (Page. (url-decode page-url)))] (map->ChannelPage - {:related-streams (get-results (.getItems info)) + {:related-streams (result/get-results (.getItems info)) :next-page (j/from-java (.getNextPage info))})))) diff --git a/src/backend/tau/api/kiosk.clj b/src/backend/tau/api/kiosk.clj index 7ae2150..4243a32 100644 --- a/src/backend/tau/api/kiosk.clj +++ b/src/backend/tau/api/kiosk.clj @@ -1,10 +1,8 @@ (ns tau.api.kiosk (:require [clojure.java.data :as j] - [tau.api.stream :as stream] - [tau.api.channel :as channel] - [tau.api.playlist :as playlist] - [ring.util.codec :refer [url-decode]]) + [ring.util.codec :refer [url-decode]] + [tau.api.result :as result]) (:import org.schabi.newpipe.extractor.StreamingService org.schabi.newpipe.extractor.Page @@ -20,14 +18,6 @@ (defrecord KioskPage [next-page related-streams]) -(defn get-results - [items] - (map #(case (.name (.getInfoType %)) - "STREAM" (stream/get-result %) - "CHANNEL" (channel/get-result %) - "PLAYLIST" (playlist/get-result %)) - items)) - (defn get-info ([kiosk-id service-id] (let [service (NewPipe/getService service-id) @@ -37,7 +27,7 @@ {:id (.getId info) :url (.getUrl info) :next-page (j/from-java (.getNextPage info)) - :related-streams (get-results (.getRelatedItems info))}))) + :related-streams (result/get-results (.getRelatedItems info))}))) ([kiosk-id service-id page-url] (let [service (NewPipe/getService service-id) extractor (.getExtractorById (.getKioskList service) kiosk-id nil) @@ -46,7 +36,7 @@ info (KioskInfo/getMoreItems service (.getUrl kiosk-info) (Page. url))] (map->KioskPage {:next-page (j/from-java (.getNextPage info)) - :related-streams (get-results (.getItems info))})))) + :related-streams (result/get-results (.getItems info))})))) (defn get-kiosks [service-id] diff --git a/src/backend/tau/api/playlist.clj b/src/backend/tau/api/playlist.clj index 4bc7054..8f2b22c 100644 --- a/src/backend/tau/api/playlist.clj +++ b/src/backend/tau/api/playlist.clj @@ -1,9 +1,8 @@ (ns tau.api.playlist (:require [clojure.java.data :as j] - [tau.api.stream :as stream] - [tau.api.channel :as channel] - [ring.util.codec :refer [url-decode]]) + [ring.util.codec :refer [url-decode]] + [tau.api.result :as result]) (:import org.schabi.newpipe.extractor.playlist.PlaylistInfo org.schabi.newpipe.extractor.Page @@ -13,29 +12,9 @@ [id name playlist-type thumbnail-url uploader-name uploader-url uploader-avatar banner-url next-page stream-count related-streams]) -(defrecord PlaylistResult - [name thumbnail-url url upload-author stream-count]) - (defrecord PlaylistPage [next-page related-streams]) -(defn get-results - [items] - (map #(case (.name (.getInfoType %)) - "STREAM" (stream/get-result %) - "CHANNEL" (channel/get-result %) - "PLAYLIST" (get-result %)) - items)) - -(defn get-result - [playlist] - (map->PlaylistResult - {:name (.getName playlist) - :thumbnail-url (.getThumbnailUrl playlist) - :url (.getUrl playlist) - :upload-author (.getUploaderName playlist) - :stream-count (.getStreamCount playlist)})) - (defn get-info ([url] (let [service (NewPipe/getServiceByUrl (url-decode url)) @@ -51,10 +30,10 @@ :uploader-avatar (.getUploaderAvatarUrl info) :stream-count (.getStreamCount info) :next-page (j/from-java (.getNextPage info)) - :related-streams (get-results (.getRelatedItems info))}))) + :related-streams (result/get-results (.getRelatedItems info))}))) ([url page-url] (let [service (NewPipe/getServiceByUrl (url-decode url)) info (PlaylistInfo/getMoreItems service url (Page. (url-decode page-url)))] (map->PlaylistPage {:next-page (j/from-java (.getNextPage info)) - :related-streams (get-results (.getItems info))})))) + :related-streams (result/get-results (.getItems info))})))) diff --git a/src/backend/tau/api/result.clj b/src/backend/tau/api/result.clj new file mode 100644 index 0000000..901ea51 --- /dev/null +++ b/src/backend/tau/api/result.clj @@ -0,0 +1,59 @@ +(ns tau.api.result) + +(defrecord PlaylistResult + [name thumbnail-url url upload-author stream-count]) + +(defrecord ChannelResult + [name description verified? thumbnail-url url + subscriber-count stream-count]) + +(defrecord StreamResult + [name url thumbnail-url upload-author upload-url + upload-avatar upload-date short-description + duration view-count uploaded verified?]) + +(defn get-stream-result + [stream] + (map->StreamResult + {:url (.getUrl stream) + :name (.getName stream) + :thumbnail-url (.getThumbnailUrl stream) + :upload-author (.getUploaderName stream) + :upload-url (.getUploaderUrl stream) + :upload-avatar (.getUploaderAvatarUrl stream) + :upload-date (.getTextualUploadDate stream) + :short-description (.getShortDescription stream) + :duration (.getDuration stream) + :view-count (.getViewCount stream) + :uploaded (if (.getUploadDate stream) + (.. stream (getUploadDate) (offsetDateTime) (toInstant) (toEpochMilli)) + false) + :verified? (.isUploaderVerified stream)})) + +(defn get-channel-result + [channel] + (map->ChannelResult + {:url (.getUrl channel) + :name (.getName channel) + :thumbnail-url (.getThumbnailUrl channel) + :description (.getDescription channel) + :subscriber-count (.getSubscriberCount channel) + :stream-count (.getStreamCount channel) + :verified? (.isVerified channel)})) + +(defn get-playlist-result + [playlist] + (map->PlaylistResult + {:url (.getUrl playlist) + :name (.getName playlist) + :thumbnail-url (.getThumbnailUrl playlist) + :upload-author (.getUploaderName playlist) + :stream-count (.getStreamCount playlist)})) + +(defn get-results + [items] + (map #(case (.name (.getInfoType %)) + "STREAM" (get-stream-result %) + "CHANNEL" (get-channel-result %) + "PLAYLIST" (get-playlist-result %)) + items)) diff --git a/src/backend/tau/api/search.clj b/src/backend/tau/api/search.clj index 106999d..9f3132f 100644 --- a/src/backend/tau/api/search.clj +++ b/src/backend/tau/api/search.clj @@ -1,10 +1,8 @@ (ns tau.api.search (:require - [tau.api.stream :as stream] - [tau.api.channel :as channel] - [tau.api.playlist :as playlist] [clojure.java.data :as j] - [ring.util.codec :refer [url-encode url-decode]]) + [ring.util.codec :refer [url-encode url-decode]] + [tau.api.result :as result]) (:import org.schabi.newpipe.extractor.search.SearchInfo org.schabi.newpipe.extractor.InfoItem @@ -17,14 +15,6 @@ (defrecord SearchResultPage [items next-page]) -(defn get-results - [items] - (map #(case (.name (.getInfoType %)) - "STREAM" (stream/get-result %) - "CHANNEL" (channel/get-result %) - "PLAYLIST" (playlist/get-result %)) - items)) - (defn get-info ([service-id query content-filters sort-filter] (let [service (NewPipe/getService service-id) @@ -33,7 +23,7 @@ (fromQuery query (or content-filters '()) (or sort-filter ""))) info (SearchInfo/getInfo service query-handler)] (map->SearchResult - {:items (get-results (.getRelatedItems info)) + {:items (result/get-results (.getRelatedItems info)) :next-page (j/from-java (.getNextPage info)) :search-suggestion (.getSearchSuggestion info) :corrected-search? (.isCorrectedSearch info)}))) @@ -45,5 +35,5 @@ (fromQuery query (or content-filters '()) (or sort-filter ""))) info (SearchInfo/getMoreItems service query-handler (Page. url))] (map->SearchResultPage - {:items (get-results (.getItems info)) + {:items (result/get-results (.getItems info)) :next-page (j/from-java (.getNextPage info))})))) diff --git a/src/backend/tau/api/stream.clj b/src/backend/tau/api/stream.clj index 63ab183..d935a6f 100644 --- a/src/backend/tau/api/stream.clj +++ b/src/backend/tau/api/stream.clj @@ -1,9 +1,8 @@ (ns tau.api.stream (:require - [tau.api.playlist :as playlist] - [tau.api.channel :as channel] [clojure.java.data :as j] - [ring.util.codec :refer [url-decode]]) + [ring.util.codec :refer [url-decode]] + [tau.api.result :as result]) (:import org.schabi.newpipe.extractor.stream.StreamInfo org.schabi.newpipe.extractor.NewPipe @@ -18,37 +17,6 @@ dash-mpd-url category tags audio-streams video-streams related-streams]) -(defrecord StreamResult - [name url thumbnail-url upload-author upload-url - upload-avatar upload-date short-description - duration view-count uploaded verified?]) - -(defn get-result - [stream] - (map->StreamResult - {:url (.getUrl stream) - :name (.getName stream) - :thumbnail-url (.getThumbnailUrl stream) - :upload-author (.getUploaderName stream) - :upload-url (.getUploaderUrl stream) - :upload-avatar (.getUploaderAvatarUrl stream) - :upload-date (.getTextualUploadDate stream) - :short-description (.getShortDescription stream) - :duration (.getDuration stream) - :view-count (.getViewCount stream) - :uploaded (if (.getUploadDate stream) - (.. stream (getUploadDate) (offsetDateTime) (toInstant) (toEpochMilli)) - false) - :verified? (.isUploaderVerified stream)})) - -(defn get-results - [items] - (map #(case (.name (.getInfoType %)) - "STREAM" (get-result %) - "CHANNEL" (channel/get-result %) - "PLAYLIST" (playlist/get-result %)) - items)) - (defn get-info [url] (let [info (StreamInfo/getInfo (url-decode url))] @@ -67,11 +35,11 @@ :tags (.getTags info) :category (.getCategory info) :view-count (.getViewCount info) - :like-count (.getLikeCount info) - :dislike-count (.getDislikeCount info) - :subscriber-count (.getUploaderSubscriberCount info) + :like-count (if (= (.getLikeCount info) -1) nil (.getLikeCount info)) + :dislike-count (if (= (.getDislikeCount info) -1) nil (.getDislikeCount info)) + :subscriber-count (if (= (.getUploaderSubscriberCount info) -1) nil (.getUploaderSubscriberCount info)) :audio-streams (j/from-java (.getAudioStreams info)) :video-streams (j/from-java (.getVideoStreams info)) :hls-url (.getHlsUrl info) :dash-mpd-url (.getDashMpdUrl info) - :related-streams (get-results (.getRelatedStreams info))}))) + :related-streams (result/get-results (.getRelatedStreams info))}))) -- cgit v1.2.3