Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Script up forecast.io api-token fetch + reset
  • Loading branch information
ericnormand committed Aug 12, 2016
1 parent 9730845 commit 8cf7a17
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
4 changes: 3 additions & 1 deletion project.clj
Expand Up @@ -4,4 +4,6 @@
:license {:name "CC0 1.0 Universal (CC0 1.0) Public Domain Dedication"
:url "http://creativecommons.org/publicdomain/zero/1.0/"}
:dependencies [[org.clojure/clojure "1.8.0"]
[clj-http "2.2.0"]])
[clj-http "2.2.0"]
[cheshire "5.6.3"]
[org.jsoup/jsoup "1.9.2"]])
66 changes: 60 additions & 6 deletions src/clj_http_playground/core.clj
@@ -1,10 +1,64 @@
(ns clj-http-playground.core
(:require [clj-http.client :as http]))
(:require [clj-http.client :as http]
[clj-http.cookies :as cookies])
(:import [org.jsoup Jsoup]))

(def response (http/get "http://lispcast.com"
{:debug? true}))
(defn fetch-api-key [email password]
(let [cookie-store (cookies/cookie-store)
login-page (http/get "https://developer.forecast.io/log_in"
{:cookie-store cookie-store})
login-doc (-> login-page
:body
Jsoup/parse)
login-form (-> login-doc
(.select "#login-form")
first)
token (-> login-form
(.select "[name=\"authenticity_token\"]")
first
(.attr "value"))
page (http/post "https://developer.forecast.io/sessions"
{:force-redirects true
:cookie-store cookie-store
:form-params {:utf8 ""
:authenticity_token token
:email email
:password password
}})
page-doc (-> page
:body
Jsoup/parse)
api-key (-> page-doc
(.select "#api_key")
first
(.attr "value"))]
api-key))

(:status response)
(:server (:headers response))
(type (:body response))
;;https://developer.forecast.io/users/reset_api_key

(defn reset-api-key [email password]
(let [cookie-store (cookies/cookie-store)
login-page (http/get "https://developer.forecast.io/log_in"
{:cookie-store cookie-store})
login-doc (-> login-page
:body
Jsoup/parse)
login-form (-> login-doc
(.select "#login-form")
first)
token (-> login-form
(.select "[name=\"authenticity_token\"]")
first
(.attr "value"))
page (http/post "https://developer.forecast.io/sessions"
{:force-redirects true
:cookie-store cookie-store
:form-params {:utf8 ""
:authenticity_token token
:email email
:password password
}})
response (http/get "https://developer.forecast.io/users/reset_api_key"
{:cookie-store cookie-store
:as :json})]
(:body response)))

0 comments on commit 8cf7a17

Please sign in to comment.