Permalink
Browse files

Merge branch 'upstream' into abstractClient

Conflicts:
	bin/setup
	config/play.example.yml
  • Loading branch information...
medwards committed Dec 2, 2011
2 parents c767f6e + 51678eb commit a135762601667dc35585f2751362ea061f4e77a2
View
@@ -87,6 +87,6 @@ Check out the [API docs on the wiki](https://github.com/holman/play/wiki/API).
## ♬ ★★★
This was created by [Zach Holman](http://zachholman.com). You can follow me on
-Twitter as [@holman](http://twitter.com).
+Twitter as [@holman](https://twitter.com/holman).
I usually find myself playing Justice, Deadmau5, and Muse at the office.
View
@@ -42,6 +42,13 @@ def index_music
Process.detach(pid)
end
+# Fetches all the album art in the world.
+#
+# Returns nothing.
+def fetch_art
+ Play::Album.fetch_art!
+end
+
case ARGV[0]
when 'start'
start_music
@@ -61,6 +68,8 @@ when 'pause'
when 'index'
migrate
index_music
+when 'fetch-art'
+ fetch_art
when 'migrate'
migrate
else
View
@@ -97,6 +97,13 @@ read client_id
echo " + What's your GitHub client secret? › \c"
read client_secret
+echo ""
+echo " + We hit the Last.fm API for beautiful gorgeous album art. If you have"
+echo " an API key, well, what is it? › \c"
+read lastfm_key
+
+echo ""
+
# Set up play.yml
if test -f config/play.yml
then
@@ -111,6 +118,7 @@ sed -i '' "s/__OAUTH_SECRET__/$client_secret/" config/play.yml
sed -i '' "s/__USER__/$user/" config/play.yml
sed -i '' "s/__PASSWORD__/$password/" config/play.yml
sed -i '' "s/__CLIENT__/$client/" config/play/yml
+sed -i '' "s/__LASTFM_KEY__/$lastfm_key/" config/play.yml
echo " + Configuration settings saved to config/play.yml."
echo " Feel free to manually update the file anytime you'd like."
View
@@ -3,6 +3,7 @@ gh_key: __OAUTH_KEY__
gh_secret: __OAUTH_SECRET__
office_url: http://example.com/office.macs
client: __CLIENT__
+lastfm_key: __LASTFM_KEY__
# Database stuffs that we'll pass on to ActiveRecord. Change if you want to. If
# you don't want to, you are probably a handsome-looking gentlemen or a
@@ -0,0 +1,9 @@
+class AddAlbumArt < ActiveRecord::Migration
+ def self.up
+ add_column :albums, :art_url, :string
+ end
+
+ def self.down
+ remove_column :albums, :art_url
+ end
+end
View
@@ -5,6 +5,31 @@ class Album < ActiveRecord::Base
has_many :songs
belongs_to :artist
+ before_save :fetch_art
+
+ # Runs through all of your albums and grabs album art for them.
+ #
+ # Returns nothing.
+ def self.fetch_art!
+ puts "Grabbing art for all your albums."
+ Album.find_each(:batch_size => 250) do |album|
+ album.save
+ end
+ end
+
+ # Fetches art for this album and sets the attribute (but doesn't save). lol
+ # check it out ma, XML parsing without a library and without heavy
+ # dependencies.
+ #
+ # Returns the found art_url String.
+ def fetch_art
+ key = Play.config['lastfm_key']
+ return if key.blank?
+ url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo"
+ xml = `curl --silent "#{url}&api_key=#{key}&artist=#{URI.escape artist.name}&album=#{URI.escape name}" | grep '<image size="extralarge">'`
+ self.art_url = xml.strip.sub('<image size="extralarge">','').sub('</image>','')
+ end
+
# Queue up an entire ALBUM!
#
# user - the User who is requesting the album to be queued
View
@@ -18,6 +18,10 @@ def current_user
session['user_id'].blank? ? nil : User.find_by_id(session['user_id'])
end
+ def current_song
+ Play.now_playing
+ end
+
configure :development do
# This should use Play.config eventually, but there's some weird loading
# problems right now with this file. So it goes. Dupe it for now.
@@ -45,7 +49,11 @@ def current_user
end
get "/" do
- @songs = Song.queue.includes(:album, :artist, :votes).all
+ @recent = History.limit(3).order('created_at').collect(&:song)
+ @current = current_song
+ @songs = [@current]
+ @songs << Song.queue.includes(:album, :artist, :votes).all
+ @songs.flatten!
mustache :index
end
@@ -64,10 +72,10 @@ def current_user
@user = User.authenticate(auth['user_info'])
session['user_id'] = @user.id
redirect '/'
- end
+ end
get "/now_playing" do
- @song = Play.now_playing
+ @song = current_song
mustache :now_playing
end
@@ -95,6 +103,13 @@ def current_user
"Removed"
end
+ get "/album/:id" do
+ @album = Album.find(params[:id])
+ @artist = @album.artist
+ @songs = @album.songs
+ mustache :album_songs
+ end
+
get "/artist/*/album/*" do
@artist = Artist.where(:name => params[:splat].first).first
@album = @artist.albums.where(:name => params[:splat].last).first
View
@@ -97,7 +97,7 @@ class App < Sinatra::Base
when 'song'
Song.where(:title => params[:q])
end
-
+
songs ? {:song_titles => songs.collect(&:title)}.to_json : error("Search. Problem?")
end
View
@@ -32,6 +32,21 @@ def self.import_songs(path=Play.path)
end
end
+ # Removes an songs in the database that do not exist or are not readable
+ # by AudioInfo
+ #
+ # Returns nothing.
+ def self.prune_songs
+ Song.all.each do |song|
+ begin
+ fs_get_artist_and_title_and_album(song.path)
+ rescue AudioInfoError
+ print "'#{song.path}' is bad, removing from database.\n"
+ song.destroy
+ end
+ end
+ end
+
# Imports a song into the database. This will identify a file's artist and
# albums, run through the associations, and so on. It should be idempotent,
# so you should be able to run it repeatedly on the same set of files and
@@ -53,6 +68,8 @@ def self.import_song(path)
:album => album,
:title => title)
end
+ rescue AudioInfoError => error
+ print "'#{path}' failed to import due to #{error.inspect}\n"
end
# Splits a music file up into three constituent parts: artist, title,
@@ -68,7 +85,6 @@ def self.fs_get_artist_and_title_and_album(path)
info.title.try(:strip),
info.album.try(:strip)
end
- rescue AudioInfoError
end
end
end
View
@@ -18,7 +18,7 @@ def self.users
# Opens up the office URL.
#
# Returns a String.
- def connection
+ def self.connection
open(url)
end
View
@@ -24,6 +24,13 @@ def album_name
album.name
end
+ # The gorgeous album art we fetched for you.
+ #
+ # Returns the String URL.
+ def album_art_url
+ album.art_url
+ end
+
# A nice, human-readable way of describing the playcount.
#
# Returns a String.
@@ -1,9 +1,13 @@
+<div id="history">
+ {{#recent}}
+ <div class="song">
+ <a href="/song/{{id}}">{{title}}</a>
+ by
+ <a href="/artist/{{artist_name}}">{{artist_name}}</a>
+ </div>
+ {{/recent}}
+</div>
+
{{#songs}}
{{>song}}
-{{/songs}}
-
-{{^songs}}
-<div class="content">
- The queue is empty. Quick, play some <a href="/artist/Ace+of+Base">Ace of Base</a>; no one's looking.
-</div>
-{{/songs}}
+{{/songs}}
@@ -2,7 +2,7 @@
<div style="height: 75px; margin-left: 10px;">
{{#user}}
<div class="profile_box">
- <img src="http://www.gravatar.com/avatar.php?gravatar_id={{gravatar_id}}&size=140" class="gravatar" height="75" width="75" />
+ <img src="//www.gravatar.com/avatar.php?gravatar_id={{gravatar_id}}&size=140" class="gravatar" height="75" width="75" />
</div>
<div class="bio">
@@ -1,7 +1,7 @@
<div class="content">
{{#users}}
<a href="/{{login}}">
- <img src="http://www.gravatar.com/avatar.php?gravatar_id={{gravatar_id}}&size=100" class="gravatar" height="50" width="50" />
+ <img src="//www.gravatar.com/avatar.php?gravatar_id={{gravatar_id}}&size=100" class="gravatar" height="50" width="50" />
</a>
{{/users}}
@@ -1,4 +1,12 @@
-<div class="song">
+<div class="song {{#now_playing?}}now-playing{{/now_playing?}}">
+ <div class="album">
+ <a href="/album/{{album_id}}"><img src="{{album_art_url}}" width="75" height="75" title="{{album_name}}" /></a>
+ </div>
+
+ <div class="star">
+ <a href="/star/{{id}}" title="star song">☆</a>
+ </div>
+
<div class="artist">
<a href="/artist/{{artist_name}}">{{artist_name}}</a>
</div>
@@ -8,31 +16,27 @@
</div>
<div class="controls">
- {{#current_votes}}
- {{#user}}
- <a href="/{{login}}">
- <img src="http://www.gravatar.com/avatar.php?gravatar_id={{gravatar_id}}&size=50" class="gravatar" height="25" width="25" />
- </a>
- {{/user}}
- {{/current_votes}}
-
- <span class="wrap">
- <a href="/song/{{id}}/download">Download</a>
-
- <a href="/artist/{{artist_name}}/album/{{album_name}}">Show Album</a>
-
- <span><a href="/star/{{id}}" title="star song" class="star">Star</a></span>
-
- {{#queued?}}
+ {{^now_playing?}}
+ {{#queued?}}
<span><a href="/remove/{{id}}" title="remove from queue" class="remove">Don't Play</a></span>
{{/queued?}}
{{^queued?}}
- <span><a href="/add/{{id}}" title="add to queue" class="add">Play</a></span>
+ <span><a href="/add/{{id}}" title="add to queue" class="add"></a></span>
{{/queued?}}
- </span>
+ {{/now_playing?}}
- &nbsp;
+ <a href="/song/{{id}}/download" class="secondary">Download</a>
+ </div>
+
+ <div class="stats">
+ {{to_playcount}}.
- <div class="stats">{{to_playcount}}.</div>
+ {{#current_votes}}
+ {{#user}}
+ <a href="/{{login}}">
+ <img src="//www.gravatar.com/avatar.php?gravatar_id={{gravatar_id}}&size=50" class="gravatar" height="25" width="25" />
+ </a>
+ {{/user}}
+ {{/current_votes}}
</div>
</div>
Oops, something went wrong.

0 comments on commit a135762

Please sign in to comment.