472b20cb4c5f

Holy shit it pretty much works.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 04 Oct 2011 18:50:28 -0400
parents 66a5e4150fb4
children 541449fa9cff
branches/tags (none)
files Vagrantfile puppet/modules/environ/files/bashrc src/newseasons/loops/refresh.clj src/newseasons/models/shows.clj src/newseasons/views/main.clj

Changes

--- a/Vagrantfile	Tue Oct 04 18:06:06 2011 -0400
+++ b/Vagrantfile	Tue Oct 04 18:50:28 2011 -0400
@@ -3,8 +3,9 @@
   config.vm.box = "lucid32"
   config.vm.box_url = "http://files.vagrantup.com/lucid32.box"
 
-  #                              guest <-- host
-  config.vm.forward_port "http", 8000,     4565
+  #                               guest <-- host
+  config.vm.forward_port "http",  8000,     4565
+  config.vm.forward_port "swank", 4005,     4005
 
   config.vm.provision :puppet do |puppet|
     puppet.manifest_file  = "base.pp"
--- a/puppet/modules/environ/files/bashrc	Tue Oct 04 18:06:06 2011 -0400
+++ b/puppet/modules/environ/files/bashrc	Tue Oct 04 18:50:28 2011 -0400
@@ -49,22 +49,9 @@
     fi
 fi
 
-if [ "$color_prompt" = yes ]; then
-    PS1='\n${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
-else
-    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
-fi
+PS1='\n${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
 unset color_prompt force_color_prompt
 
-# If this is an xterm set the title to user@host:dir
-case "$TERM" in
-xterm*|rxvt*)
-    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
-    ;;
-*)
-    ;;
-esac
-
 # enable color support of ls and also add handy aliases
 if [ -x /usr/bin/dircolors ]; then
     test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
--- a/src/newseasons/loops/refresh.clj	Tue Oct 04 18:06:06 2011 -0400
+++ b/src/newseasons/loops/refresh.clj	Tue Oct 04 18:50:28 2011 -0400
@@ -3,20 +3,55 @@
   (:require [newseasons.itunes :as itunes])
   )
 
+
+; Dammit, Clojure.
+(defn- gt [a b]
+  (<= 1 (compare a b)))
+
+(defn- lt [a b]
+  (>= -1 (compare a b)))
+
+
+(defn- notify [show email]
+  (println "    to:" email)
+  (println "        Sweet, a new season of" (:title show) "has been released!")
+  (println "        New season:" (:latest show)))
+
+(defn- notify-all [show-id]
+  (let [show (shows/show-get show-id)
+        watchers (shows/show-get-watchers show-id)]
+    (dorun (map notify (cycle [show]) watchers))))
+
+(defn- check-and-notify [show]
+  (let [id (show "artistId")
+        old-release-date (shows/show-get-version id)
+        new-release-date (show "releaseDate")]
+    (when (gt new-release-date
+              old-release-date)
+      (notify-all id)
+      (shows/show-set-version! id new-release-date))))
+
 (defn- refresh-show [id]
   (println "  refreshing" id)
   (let [show (itunes/itunes-lookup-seasons id)]
-    (when show
-      (println "    ->" (show "artistName")))
-    (Thread/sleep 5000)))
+    (if show
+      (do
+        (check-and-notify show)
+        (shows/store-raw-show show)
+        (println (show "artistName") "/" (show "collectionName")))
+      (println "(unknown)"))
+    (Thread/sleep 4000)))
 
 (defn- refresh []
+  (println "")
   (println "Refreshing Shows")
+  (println "----------------")
+
   (let [shows (shows/shows-get-to-check)]
     (dorun (map refresh-show shows))))
 
 
 (defn main [& args]
   (println "Starting Refresh Loop!")
-  (println "----------------------")
+  (println "======================")
   (dorun (repeatedly refresh)))
--- a/src/newseasons/models/shows.clj	Tue Oct 04 18:06:06 2011 -0400
+++ b/src/newseasons/models/shows.clj	Tue Oct 04 18:50:28 2011 -0400
@@ -13,16 +13,21 @@
 ;     id: show id
 ;     title: show tile
 ;     latest: description of the latest season
+;     release-date: release date of the latest season
 ;     image: url to show's image
 ;     url: url to view the show on iTunes
 ; }
 ;
-; The shows we need to check are stored in two places for durability and ease of
-; use:
+; The shows we need to check are stored in a set:
 ;
 ; shows:to-check = z#{<iTunes artist ID>, ...}
-; shows:to-check:queue = [<iTunes artist ID, ...]
-;              push to --^                      ^-- pop from
+;
+; All current version IDs for shows are stored as a hash:
+;
+; shows:versions = {
+;     <iTunes artist ID>: <iTunes collection release date>,
+;     ...
+; }
 
 ; Code ------------------------------------------------------------------------
 
@@ -33,6 +38,7 @@
        :title (show "title")
        :image (show "image")
        :latest (show "latest")
+       :release-date (show "release-date")
        :url (show "url")})))
 
 (defn show-set-id! [id new-id]
@@ -44,6 +50,9 @@
 (defn show-set-latest! [id new-latest]
   @(r [:hset (key-show id) "latest" new-latest]))
 
+(defn show-set-release-date! [id new-release-date]
+  @(r [:hset (key-show id) "release-date" new-release-date]))
+
 (defn show-set-image! [id new-image]
   @(r [:hset (key-show id) "image" new-image]))
 
@@ -51,9 +60,34 @@
   @(r [:hset (key-show id) "url" new-url]))
 
 
+(defn show-get-version [id]
+  @(r [:hget "shows:versions" id]))
+
+(defn show-set-version-maybe! [id release-date]
+  @(r [:hsetnx "shows:versions" id release-date]))
+
+(defn show-set-version! [id release-date]
+  @(r [:hset "shows:versions" id release-date]))
+
+
+(defn show-get-watchers [show-id]
+  @(r [:smembers (key-show-watchers show-id)]))
+
 (defn show-add-to-check! [id]
   @(r [:sadd "shows:to-check" id]))
 
 (defn shows-get-to-check []
   @(r [:smembers "shows:to-check"]))
 
+
+(defn store-raw-show [show]
+  (let [id (show "artistId")]
+    (show-set-id! id id)
+    (show-set-title! id (show "artistName"))
+    (show-set-latest! id (show "collectionName"))
+    (show-set-release-date! id (show "releaseDate"))
+    (show-set-image! id (show "artworkUrl100"))
+    (show-set-url! id (show "artistViewUrl"))))
+
+(defn store-raw-shows [seasons]
+  (dorun (map store-raw-show seasons)))
--- a/src/newseasons/views/main.clj	Tue Oct 04 18:06:06 2011 -0400
+++ b/src/newseasons/views/main.clj	Tue Oct 04 18:50:28 2011 -0400
@@ -98,22 +98,10 @@
 
 
 ; Search ----------------------------------------------------------------------
-(defn store-show [show]
-  (let [id (show "artistId")]
-    (shows/show-set-id! id id)
-    (shows/show-set-title! id (show "artistName"))
-    (shows/show-set-latest! id (show "collectionName"))
-    (shows/show-set-image! id (show "artworkUrl100"))
-    (shows/show-set-url! id (show "artistViewUrl"))))
-
-(defn store-shows [seasons]
-  (dorun (map store-show seasons)))
-
-
 (defpage [:get "/search"] {:keys [query]}
          (login-required
            (let [results (unique-shows (itunes-search-show query))]
-             (store-shows (sort-maps-by results "releaseDate"))
+             (shows/store-raw-shows results)
              (t/search query results))))
 
 
@@ -121,6 +109,8 @@
 (defpage [:post "/add"] {:keys [artist-id]}
          (login-required
            (users/user-add-show! (sess/get :email) artist-id)
+           (shows/show-set-version-maybe! artist-id
+                                          (:release-date (shows/show-get artist-id)))
            (shows/show-add-to-check! artist-id)
            (flash! "Added a show to your list.")
            (resp/redirect "/")))