Class: Scrapescrobbler::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/scrapescrobbler/listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(station) ⇒ Listener

Returns a new instance of Listener.



7
8
9
10
# File 'lib/scrapescrobbler/listener.rb', line 7

def initialize(station)
  @lastfm = Listener.authenticate
  @station = ::Scrapescrobbler::Stations.const_get(station.classify)
end

Instance Attribute Details

#lastfmObject

Returns the value of attribute lastfm.



5
6
7
# File 'lib/scrapescrobbler/listener.rb', line 5

def lastfm
  @lastfm
end

#startedObject

Returns the value of attribute started.



5
6
7
# File 'lib/scrapescrobbler/listener.rb', line 5

def started
  @started
end

#stationObject

Returns the value of attribute station.



5
6
7
# File 'lib/scrapescrobbler/listener.rb', line 5

def station
  @station
end

Instance Method Details

#listenObject



12
13
14
15
# File 'lib/scrapescrobbler/listener.rb', line 12

def listen
  raise if @started
  @started = DateTime.now
end

#now_playingObject



35
36
37
38
# File 'lib/scrapescrobbler/listener.rb', line 35

def now_playing
  update if not @now_playing
  @now_playing
end

#scrobble(song) ⇒ Object



29
30
31
32
33
# File 'lib/scrapescrobbler/listener.rb', line 29

def scrobble song
  puts "Scrobbling #{song} @ #{song.time || Time.now}"
  @lastfm.track.scrobble(song.artist, song.title,
      song.time.utc.to_i || Time.now.utc.to_i, song.album)
end

#updateObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/scrapescrobbler/listener.rb', line 17

def update
  playlist = Stations::Wyep::playlist
  if playlist and playlist.length > 0 then
    @now_playing = playlist.first
    playlist.each do |song|
      next if song == nil
      saved = song.save
      self.scrobble(song) if saved
    end
  end
end