Skip to content

Instantly share code, notes, and snippets.

@slifin
Created November 25, 2020 18:51
Show Gist options
  • Save slifin/2a9bf62e30424dadcc96f458e011c99c to your computer and use it in GitHub Desktop.
Save slifin/2a9bf62e30424dadcc96f458e011c99c to your computer and use it in GitHub Desktop.
(ns slifin.io.weather
(:gen-class)
(:require [cheshire.core :as json]
[com.wsscode.pathom3.connect.indexes :as pci]
[com.wsscode.pathom3.connect.operation :as pco]
[com.wsscode.pathom3.interface.smart-map :as psm]))
(pco/defresolver ip->lat-long
[{:keys [ip]}]
{::pco/output [:latitude :longitude]}
(-> (slurp (str "https://get.geojs.io/v1/ip/geo/" ip ".json"))
(json/parse-string keyword)
(select-keys [:latitude :longitude])))
(pco/defresolver latlong->woeid
[{:keys [latitude longitude]}]
{:woeid
(-> (slurp (str "https://www.metaweather.com/api/location/search/?lattlong=" latitude "," longitude))
(json/parse-string keyword)
first
:woeid)})
(pco/defresolver woeid->temperature
[{:keys [woeid]}]
{:temperature
(-> (slurp (str "https://www.metaweather.com/api/location/" woeid))
(json/parse-string keyword)
:consolidated_weather
first
:the_temp)})
(def env
(pci/register [latlong->woeid
woeid->temperature
ip->lat-long]))
(defn main [args]
(let [sm (psm/smart-map env args)]
(println (str "It's currently "
(:temperature sm)
"C at " (pr-str args)))))
(comment
(-> (psm/smart-map env {:ip "198.29.213.3"})
:temperature)
(woeid->temperature {:woeid "1103816"})
(:woeid (first (json/parse-string (slurp (str "https://www.metaweather.com/api/location/search/?lattlong=-37.7512,144.7508")) keyword)))
(latlong->woeid {:latitude "-37.7512" :longitude "144.7508"})
(json/parse-string "{\"hey\":1}" keyword)
ip->lat-long
(ip->lat-long {:ip "192.29.213.3"}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment