Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
Base album update on folders, not album names.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom committed Jun 3, 2011
1 parent 591fded commit 75c3014
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 59 deletions.
2 changes: 1 addition & 1 deletion lib/mpd_proxy.rb
Expand Up @@ -22,7 +22,7 @@ def setup(server, port, callbacks = false)
end

def execute(action); mpd.send action end
def find_songs_for(album); mpd.find "album", album end
def songs_for(path); mpd.songs path end

def volume; @volume end
def volume=(value); @volume = value end
Expand Down
17 changes: 5 additions & 12 deletions models/album.rb
Expand Up @@ -4,6 +4,7 @@ class Album
property :id, Serial
property :artist, String, :length => 200
property :name, String, :length => 200
property :base_path, String, :length => 255
property :art, String, :length => 255

has n, :songs
Expand Down Expand Up @@ -106,22 +107,14 @@ def nominate_similar(current, track_count)
end

def update
MpdProxy.execute(:albums).each do |album|
Library.album_paths.each do |path|
print "."
next if first(:base_path => path)

album.gsub!(/"/, '')
next if first(:name => album)

songs = MpdProxy.find_songs_for(album).inject([]) do |list, song|
if song.title && !list.map { |s| s.title.downcase }.include?(song.title.downcase)
list << song
else
list
end
end
songs = MpdProxy.songs_for(path)
next if songs.empty?

new_album = Album.new(:name => album)
new_album = Album.new(:name => songs.first.album, :base_path => path)
songs.each do |song|
new_album.songs.new :track => song.track.to_i,
:artist => song.artist,
Expand Down
10 changes: 5 additions & 5 deletions spec/lib/mpd_proxy_spec.rb
Expand Up @@ -58,19 +58,19 @@
end
end

describe "find songs for" do
describe "songs for" do
before do
MpdProxy.stub!(:mpd).and_return @mpd = mock("MPD")
@mpd.stub!(:find).and_return "songs"
@mpd.stub!(:songs).and_return "songs"
end

it "should ask the mpd object for the songs" do
@mpd.should_receive(:find).with "album", "hits"
MpdProxy.find_songs_for "hits"
@mpd.should_receive(:songs).with "abc/hits"
MpdProxy.songs_for "abc/hits"
end

it "should return the result of the find" do
MpdProxy.find_songs_for("hits").should == "songs"
MpdProxy.songs_for("abc/hits").should == "songs"
end
end

Expand Down
50 changes: 9 additions & 41 deletions spec/models/album_spec.rb
Expand Up @@ -216,8 +216,8 @@
"file" => "path"
}.each { |k, v| @song[k] = v }

MpdProxy.stub!(:execute).with(:albums).and_return ["album1"]
MpdProxy.stub!(:find_songs_for).and_return @songs = [@song]
Library.stub!(:album_paths).and_return ["abc/album1"]
MpdProxy.stub!(:songs_for).and_return @songs = [@song]

Album.stub! :first

Expand All @@ -228,64 +228,32 @@
end

it "should fetch all albums from the server" do
MpdProxy.should_receive(:execute).with(:albums).and_return []
Library.should_receive(:album_paths).and_return []
Album.update
end

it "should not do anything if we already have that album in the DB" do
Album.should_receive(:first).with(:name => "album1").and_return "exists!"
Album.should_receive(:first).with(:base_path => "abc/album1").and_return "exists!"
Album.should_not_receive :build
Album.update
end

it "should build a new album if we dont know it yet" do
Album.should_receive(:new).with(:name => "album1").and_return @album
Album.should_receive(:new).with(hash_including(:name => "album1")).and_return @album
Album.update
end

it "should not build an album, if we can't find any songs" do
MpdProxy.stub!(:find_songs_for).and_return []
MpdProxy.stub!(:songs_for).and_return []
Album.should_not_receive :new
Album.update
end

it "should fetch all the songs for that album from the server" do
MpdProxy.should_receive(:find_songs_for).with "album1"
Album.update
end

it "should add the found songs to the album" do
@album.songs.should_receive(:new).with :track => 1, :artist => "me", :title => "song", :length => 123, :file => "path"
Album.update
end

it "should not add duplicate songs" do
song2 = MPD::Song.new
{ "track" => 2, "artist" => "someone", "title" => "song", "album" => "album1", "file" => "other" }.each { |k, v| song2[k] = v }
MpdProxy.stub!(:find_songs_for).and_return [@song, song2]

@album.songs.should_receive(:new).exactly(:once)
Album.update
end

it "should not care about title case when identifying duplicate songs" do
song2 = MPD::Song.new
{ "track" => 2, "artist" => "someone", "title" => "sOnG", "album" => "album1", "file" => "other" }.each { |k, v| song2[k] = v }
MpdProxy.stub!(:find_songs_for).and_return [@song, song2]

@album.songs.should_receive(:new).exactly(:once)
Album.update
end

it "should handle nil titles when identifying duplicate songs" do
song2 = MPD::Song.new
{ "track" => 2, "artist" => "someone", "title" => nil, "album" => "album1", "file" => "other" }.each { |k, v| song2[k] = v }
MpdProxy.stub!(:find_songs_for).and_return [@song, song2]

@album.songs.should_receive(:new).exactly(:once)
Album.update
end

["5", "5/11", "5 of 11"].each do |track|
it "should convert the track '#{track}' to a integer value" do
@song["track"] = track
Expand All @@ -311,7 +279,7 @@
{ "track" => 2, "artist" => "MJ", "title" => "song 2", "album" => "album1", "file" => "other" }.each { |k, v| song2[k] = v }
song3 = MPD::Song.new
{ "track" => 3, "artist" => "MJ & DJ", "title" => "song 3", "album" => "album1", "file" => "other" }.each { |k, v| song3[k] = v }
MpdProxy.stub!(:find_songs_for).and_return [@song, song2, song3]
MpdProxy.stub!(:songs_for).and_return [@song, song2, song3]

@album.should_receive(:artist=).with "MJ"
Album.update
Expand All @@ -322,7 +290,7 @@
{ "track" => 2, "artist" => "MJ", "title" => "song 2", "album" => "album1", "file" => "other" }.each { |k, v| song2[k] = v }
song3 = MPD::Song.new
{ "track" => 3, "artist" => "lala", "title" => "song 3", "album" => "album1", "file" => "other" }.each { |k, v| song3[k] = v }
MpdProxy.stub!(:find_songs_for).and_return [@song, song2, song3]
MpdProxy.stub!(:songs_for).and_return [@song, song2, song3]

@album.should_receive(:artist=).with "Various Artists"
Album.update
Expand All @@ -334,7 +302,7 @@
{ "track" => 2, "artist" => "Method Man", "title" => "song 2", "album" => "album1", "file" => "other" }.each { |k, v| song2[k] = v }
song3 = MPD::Song.new
{ "track" => 3, "artist" => "Saukrates", "title" => "song 3", "album" => "album1", "file" => "other" }.each { |k, v| song3[k] = v }
MpdProxy.stub!(:find_songs_for).and_return [@song, song2, song3]
MpdProxy.stub!(:songs_for).and_return [@song, song2, song3]

@album.should_receive(:artist=).with "Method Man"
Album.update
Expand Down

0 comments on commit 75c3014

Please sign in to comment.