Skip to content

Instantly share code, notes, and snippets.

@ertugrulcetin
Last active March 15, 2021 08:28
Show Gist options
  • Save ertugrulcetin/4f35557962fac3d159d8c931e94873e9 to your computer and use it in GitHub Desktop.
Save ertugrulcetin/4f35557962fac3d159d8c931e94873e9 to your computer and use it in GitHub Desktop.
GitHub Clojure code search with unified repository results - Clojure, Babashka
#!/usr/bin/env bb
(require '[babashka.curl :as curl])
(require '[clojure.java.io :as io])
(require '[cheshire.core :as json])
(defn- call [q page]
(-> (curl/get "https://api.github.com/search/code"
{:headers {"Accept" "application/vnd.github.previe"}
:query-params {"q" (str q " language:Clojure")
"page" (str page)
"per_page" "100"}
;; YOUR CREDENTIALS
:basic-auth ["username" "GitHub Access Token"]})
:body
(json/parse-string true)))
(defn search-code [q]
(let [body (call q 1)
total (:total_count body)
;; Due to GitHub's API limit
reqs-count (min 9 (dec (Math/ceil (/ total 100))))]
(->> (range 2 (+ 2 reqs-count))
(pmap (partial call q))
(cons body)
(mapcat :items)
(map #(get-in % [:repository :html_url]))
(distinct)
(clojure.pprint/pprint))))
(search-code (first *command-line-args*))
;; Usage
;; bb ./repo-search.clj 'jackdaw.client'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment