aboutsummaryrefslogtreecommitdiff
path: root/src/backend/tau/downloader_impl.clj
blob: 70839738bb868ff68823aedef5c4bfdcd34a9e65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(ns tau.downloader-impl
  (:import
   [org.schabi.newpipe.extractor.downloader Response Request]
   [okhttp3 Request$Builder OkHttpClient$Builder RequestBody])
  (:gen-class
   :extends org.schabi.newpipe.extractor.downloader.Downloader
   :constructors {[okhttp3.OkHttpClient$Builder] []}
   :name tau.DownloaderImpl
   :init downloader-impl
   :state state
   :methods [#^{:static true} [init [] tau.DownloaderImpl]
             #^{:static true} [getInstance [] tau.DownloaderImpl]]
   :prefix "-"
   :main false))

(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 -init
  ([]
   (-init (OkHttpClient$Builder.)))
  ([builder]
   (reset! instance (tau.DownloaderImpl. builder))))

(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 (when 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)
            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)
            (.header request-builder header-name (.get header-value-list 0))))))
    (let [response (.. (@(.state this) :client) (newCall (.build request-builder)) (execute))
          body (.body response)
          response-body-to-return (when body (.string body))
          latest-url (.. response (request) (url) (toString))]
      (when (= (.code response) 429)
        (.close response))
      (Response. (.code response)
                 (.message response)
                 (.. response (headers) (toMultimap))
                 response-body-to-return
                 latest-url))))