   ([hash language] (subdb-download hash language 0))
   ([hash language version]
    (go-catch
-     (let [uri (str *subdb-endpoint* "?action=download&hash=" hash "&version=" version "&language=" language)
-           params {:uri     uri
-                   :headers {"User-Agent" subdb-ua}}
+     (let [params (subdb-query "download" {:hash hash :version version :language language})
            response (<? (http params))
            status (.-statusCode response)]
        (if (= 200 status)


(defn subdb-upload [hash stream]
  (go-catch
    (let [uri (str *subdb-endpoint* "?action=upload")
          params {:uri     uri
                  :headers {"User-Agent" subdb-ua}}
          build-form (fn [f]
                       (.append f "hash" hash)
                       (.append f "file" stream #js {:contentType "application/octet-stream"}))

  (:require [wilkerdev.util.nodejs :refer [lstat fopen fread http] :as node]))

(def ^:dynamic *subdb-endpoint* "http://api.thesubdb.com/")

(defn map->query [m]
  (.toString (.createFromMap goog.Uri.QueryData (clj->js m))))

(def subdb-ua "SubDB/1.0 (Subtitle Master/2.0.1; http://subtitlemaster.com)")

