Commits

Steve Losh  committed 89f2414

Start a refresh loop.

  • Participants
  • Parent commits a77d48f

Comments (0)

Files changed (4)

File src/newseasons/itunes.clj

 (ns newseasons.itunes
   (:require [clj-http.client :as client]) 
+  (:use newseasons.utils)
   (:use [cheshire.core :only (parse-string)]))
 
 
                   "attribute" "showTerm"}))
 
 
-(defn itunes-lookup [field id]
-  ((parse-string (:body (client/get "http://itunes.apple.com/search"
-                                    {:query-params {field id}})))
+(defn itunes-lookup [field id entity]
+  ((parse-string (:body (client/get "http://itunes.apple.com/lookup"
+                                    {:query-params {field id
+                                                    "entity" entity}})))
      "results"))
 
-(defn itunes-lookup-artist [id]
-  (first (itunes-lookup "id" id)))
+(defn itunes-lookup-seasons [id]
+  (let [results (itunes-lookup "id" id "tvSeason")]
+    (last (sort-maps-by (filter #(% "collectionName")
+                                results)
+                        "releaseDate"))))
 
 

File src/newseasons/loops/refresh.clj

+(ns newseasons.loops.refresh
+  (:require [newseasons.models.shows :as shows])
+  (:require [newseasons.itunes :as itunes])
+  )
+
+(defn- refresh-show [id]
+  (println "  refreshing" id)
+  (let [show (itunes/itunes-lookup-seasons id)]
+    (println show)
+    (println "    ->" (show "artistName"))
+    (Thread/sleep 5000)))
+
+(defn- refresh []
+  (println "Refreshing Shows")
+  (let [shows (shows/shows-get-to-check)]
+    (dorun (map refresh-show shows))))
+
+
+(defn main [& args]
+  (println "Starting Refresh Loop!")
+  (println)
+  (dorun (repeatedly refresh)))

File src/newseasons/models/shows.clj

 (defn show-set-url! [id new-url]
   @(r [:hset (key-show id) "url" new-url]))
 
-(defn show-add-to-checked! [id]
+
+(defn show-add-to-check! [id]
   @(r [:sadd "shows:to-check" id]))
 
+(defn shows-get-to-check []
+  @(r [:smembers "shows:to-check"]))
+

File src/newseasons/views/main.clj

 (defpage [:post "/add"] {:keys [artist-id]}
          (login-required
            (users/user-add-show! (sess/get :email) artist-id)
-           (shows/show-add-to-checked! artist-id)
+           (shows/show-add-to-check! artist-id)
            (flash! "Added a show to your list.")
            (resp/redirect "/")))