(ns hsm.data
    (:require 
      [taoensso.timbre :as log]
      [clj-http.client :as client]))

(defn extract 
  [data]
  (select-keys (:data data) [:url :score :title :domain :created_utc :author]))

(defn extract-links
  [data]
  (map extract data))

(defn get-reddit
  [url]
  (try 
    (-> url
      (client/get {:accept :json :as :json})
      (:body))
    (catch Throwable t 
      (do (log/error t) 
          []))))

(def ^:dynamic def-url "")

(defn alinks
  ([] 
     (alinks def-url))
  ([url]
     (let [jdata (get-reddit url)
        link-items (extract-links (get-in jdata [:data :children]))]
        (clojure.pprint/pprint link-items)
        link-items)))

(defn links
  []
  ; (throw (Throwable. "Testing"))
  [ {:url "" :title "Google" :shares 213} 
                       {:url "" :title "Github" :shares 2342}])

 (throw (Throwable. "Testing"))
(ns hsm.data
    (:require 
      [taoensso.timbre :as log]
      [clj-http.client :as client]))

(defn extract 
  [data]
  (select-keys (:data data) [:url :score :title :domain :created_utc :author]))

(defn extract-links
  [data]
  (map extract data))

(defn get-reddit
  [url]
  (try 
    (-> url
      (client/get {:accept :json :as :json})
      (:body))
    (catch Throwable t 
      (do (log/error t) 
          []))))

(def ^:dynamic def-url "")

(defn alinks
  ([] 
     (alinks def-url))
  ([url]
     (let [jdata (get-reddit url)
        link-items (extract-links (get-in jdata [:data :children]))]
        (clojure.pprint/pprint link-items)
        link-items)))

(defn links
  []
  [ {:url "" :title "Google" :shares 213} 
                       {:url "" :title "Github" :shares 2342}])
