Last active
December 20, 2015 11:48
-
-
Save alexpw/6125634 to your computer and use it in GitHub Desktop.
parse metar xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns metar.core | |
(:require [clojure.zip :as z] | |
[clojure.xml :as x] | |
[cheshire.core :as j])) | |
(defn metar-zipper | |
[url] | |
(-> url x/parse z/xml-zip z/children)) | |
(defn tag-kv | |
[nodes] | |
(when (vector? nodes) | |
(reduce | |
(fn [xs {:keys [tag content]}] | |
(assoc xs tag | |
(if (map? (first content)) | |
(tag-kv content) | |
(first content)))) | |
{} | |
nodes))) | |
(defn parse-metar | |
[url] | |
(->> (metar-zipper url) | |
(filterv #(= :data (:tag %))) | |
first | |
:content | |
(mapv #(tag-kv (% :content))))) | |
(defn -main | |
[& args] | |
(let [url "http://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=PHTO&hoursBeforeNow=4"] | |
(parse-metar url))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment