File tree 2 files changed +8
-10
lines changed
2 files changed +8
-10
lines changed Original file line number Diff line number Diff line change 11
11
(def password " letswinthisthing" )
12
12
; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
13
14
- ; ; FIXME: this makes an HTTP call at *compile* time
15
- ; ; that means that if the session expires, you need to recompile
16
- ; ; compilation/deployment can also fail if the network is flaky
17
14
(def get-coursera-page
18
15
(cache/persist (scraper/get-protected-page username password)))
19
16
Original file line number Diff line number Diff line change 2
2
(:require [clj-http.client :as http]
3
3
[clj-http.cookies :as cookies]))
4
4
5
+
5
6
; ; inspiration from: https://github.com/coursera-dl/coursera/blob/master/coursera%2Fcookies.py
6
7
(defn- authenticated-cookie-store
7
8
" returns a cookie store that lets you get protected pages"
28
29
:cookie-store cs})
29
30
cs)))
30
31
32
+ (def authenticated-cs (atom nil ))
31
33
; ; Username -> Password -> (ProtectedUrl -> Page)
32
34
(defn get-protected-page [username password]
33
- ; ; FIXME: authenticated-cookie-store is not quite a referentially transparent
34
- ; ; function........
35
- ; ; serializing the cookie store + caching it with an expiration date would be
36
- ; ; a possible solution
37
- (let [authenticated-cs ((memoize authenticated-cookie-store) username password)]
38
- (fn [protected-url]
39
- (:body (http/get protected-url {:cookie-store authenticated-cs})))))
35
+ (fn [protected-url]
36
+ (let [cs (if (nil? @authenticated-cs)
37
+ (reset! authenticated-cs
38
+ (authenticated-cookie-store username password))
39
+ @authenticated-cs)]
40
+ (:body (http/get protected-url {:cookie-store cs})))))
You can’t perform that action at this time.
0 commit comments