From 8cf7a175d39f3a08228af21fb0a89c81b62a14a9 Mon Sep 17 00:00:00 2001 From: Eric Normand Date: Fri, 12 Aug 2016 10:50:31 -0500 Subject: [PATCH] Script up forecast.io api-token fetch + reset --- project.clj | 4 +- src/clj_http_playground/core.clj | 66 +++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/project.clj b/project.clj index 01cafda..a70fa60 100644 --- a/project.clj +++ b/project.clj @@ -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"]]) diff --git a/src/clj_http_playground/core.clj b/src/clj_http_playground/core.clj index ab15da2..f7cd9c4 100644 --- a/src/clj_http_playground/core.clj +++ b/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)))