-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.clj
45 lines (38 loc) · 1.43 KB
/
api.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(ns hackernews.api
(:require [clj-http.client :as client]
[cheshire.core :as json]))
;; The base URL for the HN API.
(def base-url "https://hacker-news.firebaseio.com/v0")
(defn get-json
"Given a URL, parses the body of the HTTP GET request as JSON."
[url]
(println "GET " url)
(->> (str url ".json")
(client/get )
(:body)
(json/parse-string)))
(defn get-front-page-story-ids
"Fetches an array of IDs to stories on the front page of HN."
[]
(get-json (str base-url "/topstories")))
(defn get-item
"Fetches the item with a given item-id from the HN API.
NOTE: In HN terminology, an 'item' can be either a comment or a story."
[item-id]
(get-json (str base-url "/item/" item-id)))
(defn get-item-deep
"Recursively fetches the story and all of its comments."
[item-id]
(let [item (get-item item-id)]
(assoc item "comments" (pmap get-item-deep (item "kids")))))
(defn get-front-page
"Fetches each story on the front page of HN. Takes an optional
number of stories to fetch. Default is 30."
([] (pmap get-item (take 30 (get-front-page-story-ids))))
([limit] (pmap get-item (take (Integer. limit) (get-front-page-story-ids)))))
(defn get-user
"Given a username, fetches that user from the HN API along with all of their
submitted stories."
[username]
(let [user (get-json (str base-url "/user/" username))]
(assoc user "stories" (pmap get-item (user "submitted")))))