Skip to content

Commit

Permalink
[sd/np] you can now add whole albums! hooray!
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Deobald committed Mar 31, 2012
1 parent f9f323b commit fa574f5
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 11 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -5,3 +5,12 @@
Jukebox2 is a communal music player for team environments. Users can upload their own music, and jukebox2 will cycle through it and play a mix.

<img src="https://raw.github.com/thehammer/jukebox2/master/resources/img/screenshot.png" />

## getting started

* put your music in music/<yourname>

## hacking

* use `lein spec spec/jukebox_web/models/xyz_spec.clj` to run a single test suite
* use `lein ring server-headless` to run the app in development mode
10 changes: 10 additions & 0 deletions spec/jukebox_web/models/playlist_spec.clj
Expand Up @@ -55,6 +55,16 @@
(playlist/add-song! "user/artist/album/track2.mp3")
(should= first-value (first (playlist/queued-songs))))))

(describe "add-album!"
(with-test-music-library)

(it "adds all the songs in this album to the queued songs"
(playlist/add-album! "user/artist/album")
(should= 2 (count (playlist/queued-songs)))
(should= #{(library/file-on-disk "user/artist/album/track.mp3")
(library/file-on-disk "user/artist/album/track2.mp3")}
(set (map :song (playlist/queued-songs))))))

(describe "add-random-song!"
(it "adds a random song to the queued songs"
(should (empty? (playlist/queued-songs)))
Expand Down
2 changes: 1 addition & 1 deletion src/jukebox_web/controllers/library.clj
Expand Up @@ -10,7 +10,7 @@
"upload complete"))

(defn browse-root [request]
(view/browse request "Music Library" (library/list-directory)))
(view/browse request library/*music-library-title* (library/list-directory)))

(defn browse [request]
(let [path (-> request :params :path)
Expand Down
15 changes: 12 additions & 3 deletions src/jukebox_web/controllers/playlist.clj
Expand Up @@ -52,6 +52,15 @@
(let [song (-> request :params :song)
user (current-user request)]
(when (user/canAdd? user) (playlist/add-song! song user))
(if (json/request? ((:headers request) "accept"))
(json/response (build-playlist user))
{:status 302 :headers {"Location" "/playlist"}})))
(if (json/request? ((:headers request) "accept"))
(json/response (build-playlist user))
{:status 302 :headers {"Location" "/playlist"}})))

(defn add-album [request]
(let [album-directory (-> request :params :album-dir)
user (current-user request)]
(when (user/canAdd? user)
(playlist/add-album! album-directory user))
(if (json/request? ((:headers request) "accept"))
(json/response (build-playlist user))
{:status 302 :headers {"Location" "/playlist"}})))
1 change: 1 addition & 0 deletions src/jukebox_web/core.clj
Expand Up @@ -27,6 +27,7 @@
(POST "/playlist/add" [] playlist-controller/add)
(DELETE "/playlist/:id/delete" [] playlist-controller/delete)
(GET ["/playlist/add/:song" :song #".*"] [] playlist-controller/add)
(GET ["/playlist/add-album/:album-dir" :album-dir #".*"] [] playlist-controller/add-album)
(GET "/player/play" [] player-controller/play)
(GET "/player/pause" [] player-controller/pause)
(GET "/player/skip" [] player-controller/skip)
Expand Down
3 changes: 3 additions & 0 deletions src/jukebox_web/models/library.clj
Expand Up @@ -7,6 +7,9 @@
(:use [jukebox-player.tags]
[jukebox-web.util.file :only (strip-slashes relative-uri file-path mkdir-p mv)]))

;; constants
;; TODO: earmuffs should denote mutable vars, not constants
(def *music-library-title* "Music Library")
(def *music-library* "music")
(def *play-counts-model* "play-counts")
(def *skip-counts-model* "skip-counts")
Expand Down
4 changes: 4 additions & 0 deletions src/jukebox_web/models/playlist.clj
Expand Up @@ -56,6 +56,10 @@
(if (< (recent-songs-to-keep) (count @recent-songs-atom))
(swap! recent-songs-atom pop))))

(defn add-album! [album-directory & [user]]
(let [album-songs (library/list-directory album-directory)]
(doseq [song album-songs] (add-song! song user))))

(defn add-random-song! []
(loop [song (random-song) attempts 0]
(if (or (nil? song) (.contains @recent-songs-atom song))
Expand Down
1 change: 1 addition & 0 deletions src/jukebox_web/util/file.clj
Expand Up @@ -18,3 +18,4 @@

(defn strip-slashes [string]
(clojure-string/replace-str "/" " " string))

26 changes: 19 additions & 7 deletions src/jukebox_web/views/library.clj
Expand Up @@ -2,26 +2,38 @@
(:require [jukebox-web.views.layout :as layout]
[jukebox-web.models.library :as library]
[jukebox-web.models.user :as user]
[clojure.string :as string])
[clojure.string :as string]
[ring.util.codec :as ring-util])
(:use [hiccup core page-helpers]
[hiccup core form-helpers]
[jukebox-player.tags]
[jukebox-web.util.file :only (relative-uri)]))

(defn link-or-string [file request]
(if (user/canAdd? (-> request :session :current-user))
(link-to (str "/playlist/add/" (relative-uri file)) (.getName file) " (" (library/play-count (library/file-on-disk file)) ")")
(link-to (str "/playlist/add/" (relative-uri file))
(.getName file)
" ("
(library/play-count (library/file-on-disk file))
")")
(str (.getName file) " (" (library/play-count (library/file-on-disk file)) ")")))

(defn album-link [album-path user]
(when (and (not= album-path library/*music-library-title*)
(user/canAdd? user))
(link-to (str "/playlist/add-album/" (ring-util/url-encode album-path))
"Add Album!")))

(defn display-file [file request]
(if (library/track? file)
(link-or-string file request)
(link-to (str "/library/browse/" (relative-uri file)) (.getName file))))

(defn browse [request path files]
(let [parent-path (library/parent-directory path)]
(layout/main request "browse library"
[:h3 "Files in " path " (play count)"]
[:ul
(if-not (nil? parent-path) [:li (link-to (str "/library/browse/" parent-path) "..")])
(map #(vector :li (display-file % request)) (sort files))])))
(layout/main request "browse library"
[:h3 "Files in " path " (play count)"]
(album-link path (-> request :session :current-user))
[:ul
(if-not (nil? parent-path) [:li (link-to (str "/library/browse/" parent-path) "..")])
(map #(vector :li (display-file % request)) (sort files))])))

0 comments on commit fa574f5

Please sign in to comment.