Skip to content

Instantly share code, notes, and snippets.

@yhsiang
Created November 9, 2015 16:32
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 yhsiang/d6f3f3649ddb7feac1b4 to your computer and use it in GitHub Desktop.
Save yhsiang/d6f3f3649ddb7feac1b4 to your computer and use it in GitHub Desktop.
axe lv2 Clojure Implementation for http://axe.g0v.tw/level/2
(ns axe.core
(:require [clj-http.client :as client]
[net.cgrand.enlive-html :as html]
[clojure.data.json :as json]))
(def axe-lv2-url "http://axe-level-1.herokuapp.com/lv2/")
(def town-keys [:town :village :name])
(defn parse-table
"return a list of rows"
[url]
(html/select
(html/html-snippet
(:body (client/get url)))
[:table.table :tr]))
(defn parse-pagination
[url]
(html/select
(html/html-snippet
(:body (client/get url)))
[:a]))
(defn list->urls
[anchors]
(map #(str axe-lv2-url (:href (:attrs %))) anchors))
(defn town-list->map
"return a sequence of map like {:國語 90 :數學 90 :自然 90 :社會 90 :健康教育 90}"
[row]
(reduce (fn [row-map [key val]]
(assoc row-map key val))
{}
(map vector town-keys row)))
(defn get-content
"return map of content"
[{:keys [content]}]
content)
(defn list->str
"convert sequence to vector of strings"
[data-list]
(map #(first (get-content %)) data-list))
(defn parse-lv2-row
"conver a row to a sequence of map"
[row]
(let [strs (list->str (filter map? (:content row)))]
(town-list->map strs)))
(defn get-one-page
[url]
(map parse-lv2-row (drop 1 (parse-table url))))
(defn get-all-page
[urls]
(flatten (map get-one-page urls)))
(defn -main
"This will generate json from http://axe-level-1.herokuapp.com/"
[]
(println (json/write-str (get-all-page (list->urls
(parse-pagination axe-lv2-url)))
:escape-unicode false)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment