Skip to content

Instantly share code, notes, and snippets.

@yhsiang
Created November 10, 2015 04:16
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/3d161e25ff89f3765bed to your computer and use it in GitHub Desktop.
Save yhsiang/3d161e25ff89f3765bed to your computer and use it in GitHub Desktop.
Axe Lv3 Clojure Implementation http://axe.g0v.tw/level/3
(ns axe.core
(:require [clj-http.client :as client]
[net.cgrand.enlive-html :as html]
[clojure.data.json :as json]))
(def axe-lv3-url "http://axe-level-1.herokuapp.com/lv3/")
(def cs (clj-http.cookies/cookie-store))
(defn parse-cookie
[]
(:value (get (clj-http.cookies/get-cookies cs) "PHPSESSID")))
(defn parse-table
"return a list of rows"
[url]
(html/select
(html/html-snippet
(:body (client/get url {:cookie-store cs})))
[:table.table :tr]))
(defn parse-page-number
[url]
(let [tags (html/select
(html/html-snippet (:body (client/get url)))
[:body])]
(Integer. (nth (re-find #"共 (\d+) 頁"
(nth (:content (into {} tags)) 4)) 1))))
(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))
(def town-keys [:town :village :name])
(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 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
[]
(map get-one-page
(conj (take (- (parse-page-number axe-lv3-url) 1)
(repeat (str axe-lv3-url "?page=next")))
axe-lv3-url)))
(defn -main
"This will generate json from http://axe-level-1.herokuapp.com/"
[]
(println (json/write-str (flatten (get-all-page)) :escape-unicode false)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment