Skip to content

Commit

Permalink
Improve love/unlove capabilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Jun 28, 2012
1 parent 8cbe380 commit b916b5e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -32,7 +32,7 @@ This allows for some neat features, like easily scrobbling from a radio, the fol
how to scrobble songs from trance.fm.

```ruby
%w(listened love now_playing).each {|name|
%w(listened love unlove now_playing).each {|name|
on name do |song|
next if song.artist || !song.stream?

Expand Down
12 changes: 11 additions & 1 deletion bin/LOLastfm-send
Expand Up @@ -21,6 +21,14 @@ OptionParser.new do |o|
options[:love] = true
end

o.on '--unlove', 'enable unlove sending' do
options[:unlove] = true
end

o.on '-c', '--current', 'work on the current song' do
options[:current] = true
end

o.on '-t', '--title TITLE', 'title of the song' do |value|
options[:song][:title] = value
end
Expand Down Expand Up @@ -72,7 +80,9 @@ else
if options[:now_playing]
socket.puts [:now_playing, options[:song]].to_json
elsif options[:love]
socket.puts [:love, options[:song].empty? ? nil : options[:song]].to_json
socket.puts [:love, options[:song].empty? ? (options[:current] ? :current : nil) : options[:song]].to_json
elsif options[:unlove]
socket.puts [:unlove, options[:song].empty? ? (options[:current] ? :current : nil) : options[:song]].to_json
else
socket.puts [:listened, options[:song]].to_json
end
Expand Down
24 changes: 24 additions & 0 deletions lib/LOLastfm.rb
Expand Up @@ -157,6 +157,7 @@ def listened! (song)

def love (song = nil)
song = @last_played or return unless song
song = now_playing? or return if song == 'current' || song == :current
song = Song.new(song) unless song.is_a? Song
song = song.dup

Expand All @@ -177,6 +178,29 @@ def love! (song)
false
end

def unlove (song = nil)
song = @last_played or return unless song
song = now_playing? or return if song == 'current' || song == :current
song = Song.new(song) unless song.is_a? Song
song = song.dup

return false unless fire :unlove, song

return false if song.nil?

unless unlove! song
@cache.unlove(song)
end

true
end

def unlove! (song)
@session.track.unlove(song.artist, song.title)
rescue
false
end

def stopped_playing!
@now_playing = nil
end
Expand Down
29 changes: 25 additions & 4 deletions lib/LOLastfm/cache.rb
Expand Up @@ -20,6 +20,7 @@ def initialize (fm)

@listened = []
@loved = []
@unloved = []
end

def listened (song)
Expand All @@ -34,8 +35,14 @@ def love (song)
@loved << song
end

def unlove (song)
return if @unloved.member? song

@unloved << song
end

def empty?
@listened.empty? && @loved.empty?
@listened.empty? && @loved.empty? && @unloved.empty?
end

def flush!
Expand All @@ -50,22 +57,36 @@ def flush!

@loved.shift
end

until @unloved.empty?
break unless fm.unlove! @unloved.first

@unloved.shift
end
end

def load (path)
data = YAML.parse_file(path).transform

data['listened'].each {|song|
listened(Song.new(song))
}
} if data['listened']

data['loved'].each {|song|
love(Song.new(song))
}
} if data['loved']

data['unloved'].each {|song|
unlove(Song.new(song))
} if data['unloved']
end

def to_yaml
{ 'listened' => @listened.map(&:to_hash), 'loved' => @loved.map(&:to_hash) }.to_yaml
{
'listened' => @listened.map(&:to_hash),
'loved' => @loved.map(&:to_hash),
'unloved' => @unloved.map(&:to_hash)
}.to_yaml
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/LOLastfm/connection.rb
Expand Up @@ -36,6 +36,10 @@ class LOLastfm
love song
end

define_command :unlove do |song|
unlove song
end

define_command :hint do |*args|
hint *args
end
Expand Down

0 comments on commit b916b5e

Please sign in to comment.