Class: Hearken::Scrobbler

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/hearken/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
# File 'lib/hearken/scrobbler.rb', line 9

def initialize preferences
  @preferences = preferences
end

Instance Method Details

#ask(question) ⇒ Object



33
34
35
36
# File 'lib/hearken/scrobbler.rb', line 33

def ask question
  print question
  $stdin.gets.chomp
end

#enabled=(tf) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/hearken/scrobbler.rb', line 13

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

#finished(track) ⇒ Object



21
22
23
24
25
# File 'lib/hearken/scrobbler.rb', line 21

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

#setupObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hearken/scrobbler.rb', line 38

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

#started(track) ⇒ Object



27
28
29
30
31
# File 'lib/hearken/scrobbler.rb', line 27

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