rockstar 0.6.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,50 +0,0 @@
1
- module Rockstar
2
- class Playing
3
- # you should read last.fm/api/submissions#np first!
4
-
5
- attr_accessor :session_id, :now_playing_url, :artist, :track,
6
- :album, :length, :track_number, :mb_track_id
7
- attr_reader :status
8
-
9
- def initialize(args = {})
10
- @session_id = args[:session_id] # from Rockstar::SimpleAuth
11
- @now_playing_url = args[:now_playing_url] # from Rockstar::SimpleAuth (can change)
12
- @artist = args[:artist] # track artist
13
- @track = args[:track] # track name
14
- @album = args[:album] || '' # track album (optional)
15
- @length = args[:length] || '' # track length in seconds (optional)
16
- @track_number = args[:track_number] || '' # track number (optional)
17
- @mb_track_id = args[:mb_track_id] || '' # MusicBrainz track ID (optional)
18
-
19
- if [@session_id, @now_playing_url, @artist, @track].any?(&:blank?)
20
- raise ArgumentError, 'Missing required argument'
21
- elsif !@length.to_s.empty? && @length.to_i <= 30 # see last.fm/api
22
- raise ArgumentError, 'Length must be greater than 30 seconds'
23
- end
24
-
25
- @connection = REST::Connection.new(@now_playing_url)
26
- end
27
-
28
- def submit!
29
- warn "[DEPRECATION] the `playing` class is deprecated. Please use track.updateNowPlaying"
30
- query = { :s => @session_id,
31
- :a => @artist,
32
- :t => @track,
33
- :b => @album,
34
- :l => @length,
35
- :n => @track_number,
36
- :m => @mb_track_id }
37
-
38
- @status = @connection.post('', false, query)
39
-
40
- case @status
41
- when /OK/
42
-
43
- when /BADSESSION/
44
- raise BadSessionError # rerun Rockstar::SimpleAuth#handshake!
45
- else
46
- raise RequestFailedError
47
- end
48
- end
49
- end
50
- end
@@ -1,68 +0,0 @@
1
- # exception definitions
2
- class BadSessionError < StandardError; end
3
- class RequestFailedError < StandardError; end
4
-
5
- module Rockstar
6
- class Scrobble
7
- # you need to read last.fm/api/submissions#subs first!
8
-
9
- attr_accessor :session_id, :submission_url, :artist, :track, :time,
10
- :source, :length, :album, :track_number, :mb_track_id, :rating
11
- attr_reader :status
12
-
13
- def initialize(args = {})
14
- @session_id = args[:session_id] # from Rockstar::TokenAuth
15
- @submission_url = args[:submission_url] # from Rockstar::TokenAuth (can change)
16
- @artist = args[:artist] # track artist
17
- @track = args[:track] # track name
18
- @time = args[:time] # a Time object set to the time the track started playing
19
- @source = args[:source] || 'P' # track source, see last.fm/api/submissions#subs
20
- @length = args[:length].to_s || '' # track length in seconds
21
- @album = args[:album] || '' # track album name (optional)
22
- @track_number = args[:track_number] || '' # track number (optional)
23
- @mb_track_id = args[:mb_track_id] || '' # MusicBrainz track ID (optional)
24
- @rating = args[:rating] || '' # Rating for clip (optional L=Love, B=Ban (if source=L), S=Skip (if source=L))
25
-
26
- if [@session_id, @submission_url, @artist, @track].any?(&:blank?)
27
- raise ArgumentError, 'Missing required argument'
28
- elsif @time.class.to_s != 'Time'
29
- raise ArgumentError, ":time must be a Time object"
30
- elsif !['P','R','E','U'].include?(@source) # see last.fm/api/submissions#subs
31
- raise ArgumentError, "Invalid source"
32
- elsif @source == 'P' && @length.blank? # length is not optional if source is P
33
- raise ArgumentError, 'Length must be set'
34
- elsif !@length.blank? && @length.to_i <= 30 # see last.fm/api/submissions#subs
35
- raise ArgumentError, 'Length must be greater than 30 seconds'
36
- end
37
-
38
- @connection = REST::Connection.new(@submission_url)
39
- end
40
-
41
- def submit!
42
- warn "[DEPRECATION] the `scrobble` class is deprecated. Please use track.scrobble"
43
- query = { :s => @session_id,
44
- 'a[0]' => @artist,
45
- 't[0]' => @track,
46
- 'i[0]' => @time.utc.to_i,
47
- 'o[0]' => @source,
48
- 'r[0]' => @rating,
49
- 'l[0]' => @length,
50
- 'b[0]' => @album,
51
- 'n[0]' => @track_number,
52
- 'm[0]' => @mb_track_id }
53
-
54
- @status = @connection.post('', false, query)
55
-
56
- case @status
57
- when /OK/
58
-
59
- when /BADSESSION/
60
- raise BadSessionError # rerun Rockstar::SimpleAuth#handshake!
61
- when /FAILED/
62
- raise RequestFailedError, @status
63
- else
64
- raise RequestFailedError
65
- end
66
- end
67
- end
68
- end
@@ -1,62 +0,0 @@
1
- require 'digest/md5'
2
-
3
- # exception definitions
4
- class BadAuthError < StandardError; end
5
- class BannedError < StandardError; end
6
- class BadTimeError < StandardError; end
7
- module Rockstar
8
- AUTH_URL = 'http://post.audioscrobbler.com'
9
- AUTH_VER = '1.2.1'
10
-
11
- # This class is deprecated. Please use TokenAuth instead.
12
- class SimpleAuth
13
- # you should read last.fm/api/submissions#handshake
14
-
15
- attr_accessor :user, :password, :client_id, :client_ver
16
- attr_reader :status, :session_id, :now_playing_url, :submission_url
17
-
18
- def initialize(args = {})
19
- warn "[DEPRECATION] This class is deprecated. Please use TokenAuth instead!"
20
-
21
- @user = args[:user] # last.fm username
22
- @password = args[:password] # last.fm password
23
- @client_id = 'rck' # Client ID assigned by last.fm; Don't change this!
24
- @client_ver = Rockstar::Version
25
-
26
- raise ArgumentError, 'Missing required argument' if @user.blank? || @password.blank?
27
-
28
- @connection = REST::Connection.new(AUTH_URL)
29
- end
30
-
31
- def handshake!
32
- password_hash = Digest::MD5.hexdigest(@password)
33
- timestamp = Time.now.to_i.to_s
34
- token = Digest::MD5.hexdigest(password_hash + timestamp)
35
-
36
- query = { :hs => 'true',
37
- :p => AUTH_VER,
38
- :c => @client_id,
39
- :v => @client_ver,
40
- :u => @user,
41
- :t => timestamp,
42
- :a => token }
43
- result = @connection.get('/', false, query)
44
-
45
- @status = result.split(/\n/)[0]
46
- case @status
47
- when /OK/
48
- @session_id, @now_playing_url, @submission_url = result.split(/\n/)[1,3]
49
- when /BANNED/
50
- raise BannedError # something is wrong with the gem, check for an update
51
- when /BADAUTH/
52
- raise BadAuthError # invalid user/password
53
- when /FAILED/
54
- raise RequestFailedError, @status
55
- when /BADTIME/
56
- raise BadTimeError # system time is way off
57
- else
58
- raise RequestFailedError
59
- end
60
- end
61
- end
62
- end
@@ -1,84 +0,0 @@
1
- require 'digest/md5'
2
-
3
- # exception definitions
4
- class BadAuthError < StandardError; end
5
- class BannedError < StandardError; end
6
- class BadTimeError < StandardError; end
7
- module Rockstar
8
-
9
- # = Token Authentification
10
- #
11
- # There are 2 ways to get an auth token :
12
- #
13
- # = Desktop-App
14
- # 1. Get a new token to request authorisation:
15
- # token = Rockstar::Auth.new.token
16
- # 2. Open a webbrowser with http://www.last.fm/api/auth/?api_key=xxxxxxxxxxx&token=xxxxxxxx
17
- # 3. Wait for the User to confirm that he accepted your request.
18
- # 4. Continue with "Get the session token"
19
- #
20
- # = Web-App
21
- # 1. Redirect the user to http://www.last.fm/api/auth/?api_key={YOUR_API_KEY}&amp;cb={YOUR_RETURN_URL}
22
- # 2. If the user accepts, lastfm will redirect to YOUR_RETURN_URL?token=TOKEN
23
- # token = params[:token]
24
- # 3. Continue with "Get the session token"
25
- #
26
- # = Get the session token
27
- # 1. Use the previous token and call
28
- # session = Rockstar::Auth.new.session(token)
29
- # 2. Store the session.key and session.username returned. The session.key will not
30
- # expire. It is save to store it into your database.
31
- # 3. Use this session.key as token to authentificate with this class :
32
- # auth = Rockstar::TokenAuth.new({:username => 'chunky', :token => 'bacon'})
33
- # auth.handshake!
34
- #
35
- class TokenAuth
36
- # you should read last.fm/api/submissions#handshake
37
-
38
- attr_accessor :user, :token, :client_id, :client_ver
39
- attr_reader :status, :session_id, :now_playing_url, :submission_url
40
-
41
- def initialize(args = {})
42
- @user = args[:username] # last.fm user
43
- @token = args[:token] # last.fm token
44
- @client_id = 'rck' # Client ID assigned by last.fm; Don't change this!
45
- @client_ver = Rockstar::Version
46
-
47
- raise ArgumentError, 'Missing required argument' if @user.blank? || @token.blank?
48
-
49
- @connection = REST::Connection.new(Rockstar::AUTH_URL)
50
- end
51
-
52
- def handshake!
53
- timestamp = Time.now.to_i.to_s
54
- auth = Digest::MD5.hexdigest("#{Rockstar.lastfm_api_secret}#{timestamp}")
55
-
56
- query = { :hs => 'true',
57
- :p => AUTH_VER,
58
- :c => @client_id,
59
- :v => @client_ver,
60
- :u => @user,
61
- :t => timestamp,
62
- :a => auth,
63
- :api_key=>Rockstar.lastfm_api_key,
64
- :sk => @token }
65
- result = @connection.get('/', true, query)
66
-
67
- @status = result.split(/\n/)[0]
68
- case @status
69
- when /OK/
70
- @session_id, @now_playing_url, @submission_url = result.split(/\n/)[1,3]
71
- when /BANNED/
72
- raise BannedError # something is wrong with the gem, check for an update
73
- when /BADAUTH/
74
- raise BadAuthError # invalid user/password
75
- when /FAILED/
76
- raise RequestFailedError, @status
77
- when /BADTIME/
78
- raise BadTimeError # system time is way off
79
- else
80
- raise RequestFailedError
81
- end
82
- end
83
- end
84
- end
@@ -1,53 +0,0 @@
1
- require File.expand_path('../../test_helper.rb', __FILE__)
2
-
3
- class TestPlaying < Test::Unit::TestCase
4
-
5
- def setup
6
- @session_id = '17E61E13454CDD8B68E8D7DEEEDF6170'
7
- @now_playing_url = 'http://62.216.251.203:80/nowplaying'
8
- @artist = 'Anberlin'
9
- @track = 'A Day Late'
10
- @album = 'Never Take Friendship Personal'
11
-
12
- @playing = Rockstar::Playing.new(:session_id => @session_id, :now_playing_url => @now_playing_url,
13
- :artist => @artist, :track => @track, :album => @album, :length => 214,
14
- :track_number => 5)
15
- end
16
-
17
- test 'should require a session id' do
18
- assert_raises(ArgumentError) { Rockstar::Playing.new(
19
- :now_playing_url => @now_playing_url, :artist => @artist, :track => @track,
20
- :album => @album, :length => 214, :track_number => 5) }
21
- end
22
-
23
- test 'should require a now playing url' do
24
- assert_raises(ArgumentError) { Rockstar::Playing.new(
25
- :session_id => @session_id, :artist => @artist, :track => @track,
26
- :album => @album, :length => 214, :track_number => 5) }
27
- end
28
-
29
- test 'should require an artist' do
30
- assert_raises(ArgumentError) { Rockstar::Playing.new(
31
- :session_id => @session_id, :now_playing_url => @now_playing_url,
32
- :track => @track, :album => @album, :length => 214, :track_number => 5) }
33
- end
34
-
35
- test 'should require a track' do
36
- assert_raises(ArgumentError) { Rockstar::Playing.new(
37
- :session_id => @session_id, :now_playing_url => @now_playing_url,
38
- :artist => @artist, :album => @album, :length => 214, :track_number => 5) }
39
- end
40
-
41
- test 'should require a length greater than 30 seconds' do
42
- assert_raises(ArgumentError) { Rockstar::Playing.new(
43
- :session_id => @session_id, :now_playing_url => @now_playing_url,
44
- :artist => @artist, :track => @track, :album => @album, :length => 29,
45
- :track_number => 5) }
46
- end
47
-
48
- test 'should submit successfully' do
49
- @playing.submit!
50
- assert_equal('OK', @playing.status)
51
- end
52
-
53
- end
@@ -1,69 +0,0 @@
1
- require File.expand_path('../../test_helper.rb', __FILE__)
2
-
3
- class TestScrobble < Test::Unit::TestCase
4
-
5
- def setup
6
- @session_id = '17E61E13454CDD8B68E8D7DEEEDF6170'
7
- @submission_url = 'http://62.216.251.205:80/protocol_1.2'
8
- @artist = 'Coldplay'
9
- @name = 'Viva La Vida'
10
-
11
- @scrobble = Rockstar::Scrobble.new(:session_id => @session_id, :submission_url => @submission_url,
12
- :artist => @artist, :track => @name, :album => @name, :time => Time.new,
13
- :length => 244, :track_number => 7)
14
- end
15
-
16
- test 'should require a session id' do
17
- assert_raises(ArgumentError) { Rockstar::Scrobble.new(:submission_url => @submission_url,
18
- :artist => @artist, :track => @name, :album => @name, :time => Time.new,
19
- :length => 244, :track_number => 7) }
20
- end
21
-
22
- test 'should require a submission url' do
23
- assert_raises(ArgumentError) { Rockstar::Scrobble.new(:session_id => @session_id,
24
- :artist => @artist, :track => @name, :album => @name, :time => Time.new,
25
- :length => 244, :track_number => 7) }
26
- end
27
-
28
- test 'should require an artist' do
29
- assert_raises(ArgumentError) { Rockstar::Scrobble.new(:session_id => @session_id, :submission_url => @submission_url,
30
- :track => @name, :album => @name, :time => Time.new,
31
- :length => 244, :track_number => 7) }
32
- end
33
-
34
- test 'should require a track' do
35
- assert_raises(ArgumentError) { Rockstar::Scrobble.new(:session_id => @session_id, :submission_url => @submission_url,
36
- :artist => @artist, :album => @name, :time => Time.new,
37
- :length => 244, :track_number => 7) }
38
- end
39
-
40
- test 'should require a Time object' do
41
- assert_raises(ArgumentError) { Rockstar::Scrobble.new(:session_id => @session_id, :submission_url => @submission_url,
42
- :artist => @artist, :track => @name, :album => @name, :time => 'chunky_bacon',
43
- :length => 244, :track_number => 7) }
44
- end
45
-
46
- test 'should require a valid source' do
47
- assert_raises(ArgumentError) { Rockstar::Scrobble.new(:session_id => @session_id, :submission_url => @submission_url,
48
- :artist => @artist, :track => @name, :album => @name, :time => Time.new,
49
- :length => 244, :track_number => 7, :source => 'Z') }
50
- end
51
-
52
- test 'should require a length if source is set to P' do
53
- assert_raises(ArgumentError) { Rockstar::Scrobble.new(:session_id => @session_id, :submission_url => @submission_url,
54
- :artist => @artist, :track => @name, :album => @name, :time => Time.new,
55
- :track_number => 7, :source => 'P') }
56
- end
57
-
58
- test 'should require a length greater than 30 if source is set to P' do
59
- assert_raises(ArgumentError) { Rockstar::Scrobble.new(:session_id => @session_id, :submission_url => @submission_url,
60
- :artist => @artist, :track => @name, :album => @name, :time => Time.new,
61
- :length => 29, :track_number => 7, :source => 'P') }
62
- end
63
-
64
- test 'should submit successfully' do
65
- @scrobble.submit!
66
- assert_equal('OK', @scrobble.status)
67
- end
68
-
69
- end
@@ -1,45 +0,0 @@
1
- require File.expand_path('../../test_helper.rb', __FILE__)
2
-
3
- class TestSimpleAuth < Test::Unit::TestCase
4
-
5
- def setup
6
- @auth = Rockstar::SimpleAuth.new(:user => 'chunky', :password => 'bacon')
7
- end
8
-
9
- test 'should require a user' do
10
- assert_raises(ArgumentError) { Rockstar::SimpleAuth.new(:password => 'bacon') }
11
- end
12
-
13
- test 'should require a password' do
14
- assert_raises(ArgumentError) { Rockstar::SimpleAuth.new(:user => 'chunky') }
15
- end
16
-
17
- test 'should have the right client id' do
18
- assert_equal('rck', @auth.client_id)
19
- end
20
-
21
- test 'should have the right version' do
22
- assert_equal(Rockstar::Version, @auth.client_ver)
23
- end
24
-
25
- test 'should handshake successfully' do
26
- @auth.handshake!
27
- assert_equal('OK', @auth.status)
28
- end
29
-
30
- test 'should get a session id' do
31
- @auth.handshake!
32
- assert_equal('17E61E13454CDD8B68E8D7DEEEDF6170', @auth.session_id)
33
- end
34
-
35
- test 'should get a now playing url' do
36
- @auth.handshake!
37
- assert_equal('http://62.216.251.203:80/nowplaying', @auth.now_playing_url)
38
- end
39
-
40
- test 'should get a submission url' do
41
- @auth.handshake!
42
- assert_equal('http://62.216.251.205:80/protocol_1.2', @auth.submission_url)
43
- end
44
-
45
- end
@@ -1,45 +0,0 @@
1
- require File.expand_path('../../test_helper.rb', __FILE__)
2
-
3
- class TestTokenAuth < Test::Unit::TestCase
4
-
5
- def setup
6
- Rockstar.lastfm = {"api_secret" => 'secret', "api_key" => 'api'}
7
- @auth = Rockstar::TokenAuth.new(:username => 'chunky', :token => 'bacon')
8
- end
9
-
10
- test 'should require a user' do
11
- assert_raises(ArgumentError) { Rockstar::SimpleAuth.new(:token => 'bacon') }
12
- end
13
-
14
- test 'should require a password' do
15
- assert_raises(ArgumentError) { Rockstar::SimpleAuth.new(:user => 'chunky') }
16
- end
17
-
18
- test 'should have the right client id' do
19
- assert_equal('rck', @auth.client_id)
20
- end
21
-
22
- test 'should have the right version' do
23
- assert_equal(Rockstar::Version, @auth.client_ver)
24
- end
25
-
26
- test 'should handshake successfully' do
27
- @auth.handshake!
28
- assert_equal('OK', @auth.status)
29
- end
30
-
31
- test 'should get a session id' do
32
- @auth.handshake!
33
- assert_equal('17E61E13454CDD8B68E8D7DEEEDF6170', @auth.session_id)
34
- end
35
-
36
- test 'should get a now playing url' do
37
- @auth.handshake!
38
- assert_equal('http://62.216.251.203:80/nowplaying', @auth.now_playing_url)
39
- end
40
-
41
- test 'should get a submission url' do
42
- @auth.handshake!
43
- assert_equal('http://62.216.251.205:80/protocol_1.2', @auth.submission_url)
44
- end
45
- end