Skip to content

Instantly share code, notes, and snippets.

@ga2arch
Last active March 26, 2018 15:28
Show Gist options
  • Save ga2arch/7d8ae8b43a7dca775e7a431d88a7d6c3 to your computer and use it in GitHub Desktop.
Save ga2arch/7d8ae8b43a7dca775e7a431d88a7d6c3 to your computer and use it in GitHub Desktop.
train alert
{:paths ["."]
:deps {falcon {:mvn/version "0.1.0"}}}
(ns train
(:require [falcon.core :as falcon] [clojure.pprint :as pp])
(:gen-class)
(:import (java.text SimpleDateFormat)))
(def camnago-milano "http://www.trenord.it/IT/servizi/direttrici/D036/mariano-camnago---seveso---milano.aspx")
(defn parse-severity
[severity]
(let [k (-> (:children severity)
first
:attrs
:title
clojure.string/lower-case)]
(case k
"info" :info
"criticità" :warn
"grave criticità" :fatal)))
(defn parse-train-canceled
[content]
(if-let [[_ t1 t2] (re-find #"Il treno\s*(\d+) .* corrispondente\s*(\d+)" content)]
{:type :trains-canceled
:trains #{t1 t2}}))
(defn parse-content
[content]
(cond
(.contains content "investimento")
{:type :incident}
(or (.contains content "soppress")
(.contains content "cancellat"))
(parse-train-canceled content)))
(defn parse-update
[update]
(let [[severity time & rst] (:children update)
text (apply str (mapv :text rst))
content (parse-content text)
result {:severity (parse-severity severity)
:time (->> (:text time)
(.parse (SimpleDateFormat. "dd/MM/yyyy HH:mm")))
:text text}]
(if content
(assoc result :content content)
result)))
(defn get-updates
[url]
(let [data (falcon/parse url)
updates (falcon/select data ".aggiornamento")]
(mapv parse-update updates)))
(defn -main
[& args]
(let [updates (get-updates camnago-milano)
severities (into #{} (mapv keyword args))]
(pp/pprint
(if (not (empty? args))
(filterv #(contains? severities (:severity %)) updates)
updates))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment