Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Created September 27, 2022 22:23
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 loganlinn/1d850a145481466e6e7c36d769280de1 to your computer and use it in GitHub Desktop.
Save loganlinn/1d850a145481466e6e7c36d769280de1 to your computer and use it in GitHub Desktop.
(ns openmoji
(:require
[clojure.string :as str]
[clojure.data.csv :as csv]
[clojure.java.io :as io]
[babashka.fs :as fs]
[babashka.curl :as curl]
[clojure.string :as str]))
(defn download-data
[{:keys [filename directory] :or {directory "."}}]
(assert filename)
(let [output (io/file directory filename)]
(some-> (curl/get (str "https://github.com/hfg-gmuend/openmoji/raw/master/data/" filename) {:as :bytes})
:body
(io/copy output))
(println "wrote" (str (fs/relativize "." (fs/path output))))))
(defn- println-err [& xs]
(binding [*out* *err*]
(apply println xs)))
(defn make
{:org.babashka/cli {:exec-args {:workdir "."
:output "openmoji"
:overwrite false}
:args->opts [:files]
:coerce {:files [:string]
:overwrite :bool}}}
[{:keys [workdir output files overwrite] :as params}]
(let [asset-dir (fs/path workdir "openmoji-72x72-color")
data-file (io/file workdir"openmoji.csv")]
(with-open [rdr (io/reader data-file)]
(let [[header & data] (csv/read-csv rdr)
ks (mapv keyword header)
xs (map (partial zipmap ks) data)
hextable (reduce (fn [m x] (assoc m (:hexcode x) x)) {} xs)
items (for [file files
:let [path (fs/path file)
_ (assert (fs/regular-file? path))
hexcode (-> path fs/strip-ext fs/file-name)
info (hextable hexcode)
_ (when-not info
(println-err "WARNING: no emoji data found for" file))]
:when info]
[path info])]
(doseq [[src {:keys [group subgroups annotation] :as info}] items
:let [filename (-> annotation
(str/replace #"\s+" "-")
(str/replace #"[^\w\-]+" "")
(str/replace #"--+" "-")
(str/replace #"^-+" "")
(str/replace #"-+$" "")
(str/lower-case)
(str (str \. (fs/extension src))))]
subgroup (str/split subgroups #",\s*")
:let [parent (fs/path output group subgroup)
_ (fs/create-dirs parent)
dest (fs/path parent filename)]]
(println-err (str src) "=>" (str dest))
(try
(fs/copy src dest {:replace-existing true})
(catch java.nio.file.FileAlreadyExistsException e
(println-err (str dest) "already exists! skipping..."))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment