Skip to content

Instantly share code, notes, and snippets.

@ertugrulcetin
Last active October 22, 2022 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ertugrulcetin/56ad19a2d931d6a018ca8261dd8c6b31 to your computer and use it in GitHub Desktop.
Save ertugrulcetin/56ad19a2d931d6a018ca8261dd8c6b31 to your computer and use it in GitHub Desktop.
Download PlayCanvas project via shell/babashka in Clojure
#!/usr/bin/env bb
;; To install babashka -> https://github.com/babashka/babashka#quickstart
(require '[babashka.curl :as curl])
(require '[babashka.tasks :as tasks])
(require '[cheshire.core :as json])
(require '[clojure.java.io :as io])
(def project-id 1111)
(def scene-ids [2222])
(def api-token "<API_TOKEN>")
(def project-name "<Gamename>")
(def project-dir (str "/Users/<username>/Desktop/" project-name))
(def project-unzip-dir (str project-dir ".zip"))
(def sleep-timeout 5000)
(println "Preparing download...")
(def job-id
(-> (curl/post "https://playcanvas.com/api/apps/download"
{:headers {"accept" "application/json"
"content-type" "application/json"
"authorization" (str "Bearer " api-token)}
:body (json/generate-string {:project_id project-id
:scenes scene-ids
:name project-name})})
:body
json/parse-string
(get "id")))
(println "Preparing download completed.")
(while (let [_ (Thread/sleep sleep-timeout)
job-result (-> (curl/get (str "https://playcanvas.com/api/jobs/" job-id)
{:headers {"accept" "application/json"
"content-type" "application/json"
"authorization" (str "Bearer " api-token)}})
:body
json/parse-string)
job-status (get job-result "status")]
(case job-status
"running" true
"error" (throw (ex-info "Error happened during fetching job status" {}))
(do
(io/copy
(:body (curl/get (get-in job-result ["data" "download_url"])
{:as :bytes}))
(io/file project-unzip-dir))
false)))
(println "Fetching job status...")
(Thread/sleep sleep-timeout))
(tasks/shell (str "mkdir -p " project-dir))
(tasks/shell {:dir project-dir} (str "unzip -o " project-unzip-dir))
;; Usage
;; bb ./download-game.clj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment