Skip to content

Instantly share code, notes, and snippets.

@inscapist
Created January 12, 2022 14:29
Show Gist options
  • Save inscapist/a48ef678e4b28f0f325f8c77ee38c0a0 to your computer and use it in GitHub Desktop.
Save inscapist/a48ef678e4b28f0f325f8c77ee38c0a0 to your computer and use it in GitHub Desktop.
Simple babashka script with CSV, JSON, str, regex manipulation
#!/usr/bin/env bb
;; https://book.babashka.org
;; https://book.babashka.org/#built-in-namespaces
(ns parse-country-codes
(:require
[cheshire.core :as json]
[clojure.data.csv :as csv]
[clojure.java.io :as io]
[clojure.string :as str]))
;; ===========================
;; Parse country triplets
;; ===========================
(def countries-raw (-> "countries.json"
slurp
json/parse-string))
(def countries (->> ["resources" "string"]
(get-in countries-raw)
(partition 3)))
(defn extract-country-info [[name code number]]
(let [name (name "#text")
code (code "#text")
number (number "#text")]
{:name name
:code code
:number number}))
(extract-country-info (nth countries 0))
(def triplets (map extract-country-info countries))
triplets
;; ===========================
;; Parse flags
;; ===========================
(def flags-raw (->> "country_flags.txt"
slurp
str/split-lines
(partition 2)))
(defn extract-flag-info [[code flag]]
[(->> code
(re-find #"\"(.*?)\"")
last)
(->> flag
(re-find #"drawable\.(.*);")
last)])
(extract-flag-info (nth flags-raw 0))
(def flags (into {} (map extract-flag-info flags-raw)))
(def full-final (map (fn [{code :code :as t}]
(assoc t :flag (flags code))) triplets))
full-final
(with-open [writer (io/writer "generated-country-flags.csv")]
(csv/write-csv writer
(map vals full-final)))
@inscapist
Copy link
Author

to connect to cider,

bb nrepl-server 1667

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment