Class: Songbirdsh::Scrobbler

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/songbirdsh/scrobbler.rb

Instance Method Summary collapse

Methods included from Debug

#debug, #pause

Constructor Details

#initialize(preferences) ⇒ Scrobbler

Returns a new instance of Scrobbler.



9
10
11
12
13
14
15
# File 'lib/songbirdsh/scrobbler.rb', line 9

def initialize preferences
  @preferences = preferences
  if preferences['lastfm']
    debug "Configuring scrobbler with #{preferences['lastfm'].inspect}"
    @scrobbler = SimpleScrobbler.new *%w{api_key secret user session_key}.map{|k| preferences['lastfm'][k]}
  end
end

Instance Method Details

#ask(question) ⇒ Object



29
30
31
32
# File 'lib/songbirdsh/scrobbler.rb', line 29

def ask question
  print question
  gets.chomp
end

#scrobble(track) ⇒ Object



17
18
19
20
21
# File 'lib/songbirdsh/scrobbler.rb', line 17

def scrobble track
  return unless @scrobbler
  debug "Scrobbling to last fm: #{track}"
  send_to_scrobbler :submit, track
end

#setupObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/songbirdsh/scrobbler.rb', line 34

def setup
  puts <<-EOF
  Because of the way lastfm authentication works, setting up lastfm involves two steps:
  Firstly, you need to register that you are developing an application. This will give you an api key and an associated 'secret'
  Secondly, you need to gives your application access your lastfm account.  This will give a authenticated session.

  Step 1. Logging in to your lastfm account and register an application
  This will launch a browser.  You need to log in and fill out the API registration form
  EOF
  answer = ask 'Are you ready ? '
  return unless answer.downcase.start_with? 'y'
  "http://www.last.fm/api/account".to_launcher
  preferences = {}
  preferences['api_key'] = ask 'What is your API key ? '
  preferences['secret'] = ask 'What is your secret ? '
  puts <<-EOF
  Now you've got you application details, you need to create an authentication session between your application and your lastfm account
  Step 2. Authorising your application to access your lastfm account
  This will launch another browser. You just need to give your application permission to access your account
  EOF
  preferences['user'] = ask 'What is your lastfm user name ? '
  @scrobbler = SimpleScrobbler.new preferences['api_key'], preferences['secret'], preferences['user']
  preferences['session_key'] = @scrobbler.fetch_session_key do |url|
    url.to_launcher
    ask 'Hit enter when you\'ve allowed your application access to your account'
  end
  @preferences['lastfm'] = preferences
end

#update(track) ⇒ Object



23
24
25
26
27
# File 'lib/songbirdsh/scrobbler.rb', line 23

def update track
  return unless @scrobbler
  debug "Updating now listening with last fm: #{track}"
  send_to_scrobbler :now_playing, track
end