tinyscrobbler 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Changelog ADDED
File without changes
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ Tinyscrobbler is a lightweight LastFM track scrobbler library that allows to quickly incorporate the track scrobbling feature in a ruby player app.
2
+
3
+ I'm currently learning ruby, so it's very likely that some code is not written the ruby way.
4
+
5
+ Tinyscrobbler is still under constant heavy changes but here's some early usage examples:
6
+
7
+ require 'tinyscrobbler.rb'
8
+
9
+ ls = Tinyscrobbler::Client.new(lastfm_username, lasftfm_password)
10
+
11
+ # Currently listening to...
12
+
13
+ current_track = {'artistname' => 'Moonspell', 'track' => 'Alma Mater',
14
+ 'time' => '1262185646', 'source' => 'P', 'rating' => '',
15
+ 'secs' => '337', 'album' => 'Wolfheart', 'tracknumber' => '8', 'mbtrackid' => ''}
16
+
17
+ ls.now_playing(current_track)
18
+
19
+ # after track played
20
+
21
+ ls.played(current_track)
22
+
23
+ If you prefer to have tinyscrobbler parse the audio metadata for you, here's an example on how to do it.
24
+ Please notice that currently on mp3 files are supported, and this also requires the "mp3info" gem.
25
+
26
+ require 'tinyscrobbler'
27
+ require 'audiometa.rb'
28
+
29
+ ls = Tinyscrobbler::Client.new(lastfm_username, lasftfm_password)
30
+
31
+ # Currently listening to...
32
+ file_path = '/Users/rogeriopvl/Music/Moonspell/Wolfheart/08_Alma_Mater.mp3'
33
+ current_track = Audiometa::Parser.new(file_path)
34
+
35
+ ls.now_playing(current_track)
36
+
37
+ # after track played
38
+
39
+ ls.played(current_track)
40
+
41
+
42
+ More info soon...
data/example.rb ADDED
File without changes
data/lib/audiometa.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'mp3info'
3
+
4
+ module Audiometa
5
+
6
+ # This class parses metadata from audio files.
7
+ # It currently supports only mp3 files.
8
+
9
+ class Parser
10
+
11
+ # Checks if given file exists and if is supported,
12
+ # and calls the parser method, if not
13
+ # raises exception.
14
+
15
+ def initialize(file_path)
16
+ @path = file_path
17
+ raise 'File not found or unreadable.' unless File.exists? @path and File.readable? @path
18
+ @extension = File.extname(file_path)
19
+ raise 'Not a valid audio file.' unless @extension == '.mp3'
20
+
21
+ parse_metadata
22
+ end
23
+
24
+ private
25
+
26
+ # Returns the file metadata in a hash
27
+
28
+ def parse_metadata
29
+ track = Mp3Info.new(@path)
30
+ tag = track.hastag2? ? track.tag2 : track.tag1
31
+
32
+ track_meta = {}
33
+ track_meta['track'] = tag.title
34
+ track_meta['artistname'] = tag.artist
35
+ track_meta['album'] = tag.album
36
+ track_meta['tracknumber'] = tag.tracknum.to_s
37
+ track_meta['secs'] = track.length.round.to_s
38
+ track_meta['time'] = Time.now.to_s
39
+ track_meta['source'] = 'P'
40
+ track_meta['mbtrackid'] = ''
41
+ track_meta['rating'] = ''
42
+
43
+ track_meta
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,175 @@
1
+ # Tinyscrobbler is a lightweight library for scrobbling tracks
2
+ # to the LastFM submissions API.
3
+ # For more info on the submissions API visit:
4
+ # http://www.last.fm/api/submissions
5
+ #
6
+ # Author:: Rogerio Vicente (http://rogeriopvl.com)
7
+ # Copyright:: Copyright (c) 2009 Rogerio Vicente
8
+ # License:: GPLv3
9
+
10
+ require 'net/http'
11
+ require 'Digest/md5'
12
+
13
+ module Tinyscrobbler
14
+
15
+ # Exception classes
16
+
17
+ class BadFormatedResponseError < Exception; end
18
+ class BadTimeError < Exception; end
19
+ class RequestFailedError < Exception; end
20
+ class BannedError < Exception; end
21
+ class BadAuthError < Exception; end
22
+ class BadSessionError < Exception; end
23
+
24
+ # This is the lastfm submission api client class.
25
+ # It contains necessary methods for full
26
+ # interaction with LastFM submissions API.
27
+
28
+ class Client
29
+
30
+ CLIENT_ID = 'tst'
31
+ CLIENT_VERSION = '1.0'
32
+
33
+ HANDSHAKE_BASE_URL = "http://post.audioscrobbler.com/?hs=true&p=1.2.1&c=#{CLIENT_ID}&v=#{CLIENT_VERSION}"
34
+
35
+ # Starts handshake and initializes instance attributes
36
+
37
+ def initialize(username, password)
38
+ @username = username
39
+ @password = password
40
+ @queue = []
41
+
42
+ response = handshake()
43
+
44
+ raise BadFormatedResponseError, 'Bad formated server response' if response.length != 4
45
+
46
+ @session_id = response[1]
47
+ @now_playing_url = response[2]
48
+ @submission_url = response[3]
49
+ end
50
+
51
+ # Adds a new track to the queue
52
+ # and submits the queue.
53
+ # * artistname => the name of the artist
54
+ # * track => the title of the track (song)
55
+ # * album => the album name
56
+ # * secs => the length of the track (seconds)
57
+ # * tracknumber => the number of the track in album
58
+ # * mbtrackid => the track's musicbrainz id
59
+ # * time => the time the track started playing
60
+ # * source => always 'P'
61
+ # * rating => the track rating: L, B or S (love, ban, skip)
62
+
63
+ def played(track)
64
+ @queue << track
65
+ submit_queue
66
+ end
67
+
68
+ # Sends a now playing track.
69
+ # track is a hash containing the following keys => values:
70
+ # * artistname => the name of the artist
71
+ # * track => the title of the track (song)
72
+ # * album => the album name
73
+ # * secs => the length of the track (seconds)
74
+ # * tracknumber => the number of the track in album
75
+ # * mbtrackid => the track's musicbrainz id
76
+
77
+ def now_playing(track)
78
+ params = "s=#{@session_id}"
79
+ params << "&a=#{URI.escape(track['artistname'])}"
80
+ params << "&t=#{URI.escape(track['track'])}"
81
+ params << "&b=#{URI.escape(track['album'])}"
82
+ params << "&l=#{URI.escape(track['secs'])}"
83
+ params << "&n=#{URI.escape(track['tracknumber'])}"
84
+ params << "&m=#{URI.escape(track['mbtrackid'])}"
85
+
86
+ send_submission(@now_playing_url, params)
87
+ end
88
+
89
+ private
90
+
91
+ # Executes the handshake with the API.
92
+ # If handshake returned OK then the entire
93
+ # response is returned in a array, where
94
+ # * index 0 is the handshake result
95
+ # * index 1 is the session id
96
+ # * index 2 is the now playing url
97
+ # * index 3 is the submissions url
98
+
99
+ def handshake
100
+ timestamp = Time.now.to_i.to_s
101
+ token = generate_token(timestamp)
102
+
103
+ url = URI.parse(HANDSHAKE_BASE_URL+"&u=#{@username}&t=#{timestamp}&a=#{token}")
104
+ response = Net::HTTP.get(url)
105
+
106
+ case response
107
+ when /OK/
108
+ response.split("\n")
109
+ when /BADTIME/
110
+ raise BadTimeError
111
+ when /FAILED/
112
+ raise RequestFailedError, response
113
+ when /BANNED/
114
+ raise BannedError
115
+ when /BADAUTH/
116
+ raise BadAuthError
117
+ else
118
+ raise RequestFailedError
119
+ end
120
+ end
121
+
122
+ # Generates the handshake token.
123
+ # This token is generated according to:
124
+ # md5(md5(password)+timestamp)
125
+
126
+ def generate_token(timestamp)
127
+ Digest::MD5.hexdigest(Digest::MD5.hexdigest(@password)+timestamp)
128
+ end
129
+
130
+ # Prepares the POST data to submit the track queue.
131
+ # After the submission the queue is emptied.
132
+
133
+ def submit_queue
134
+ params = "s=#{@session_id}"
135
+ i = 0
136
+ @queue.each do |item|
137
+ params << "&a[#{i}]=#{URI.escape(item['artistname'])}"
138
+ params << "&t[#{i}]=#{URI.escape(item['track'])}"
139
+ params << "&i[#{i}]=#{URI.escape(item['time'])}"
140
+ params << "&o[#{i}]=#{URI.escape(item['source'])}"
141
+ params << "&r[#{i}]=#{URI.escape(item['rating'])}"
142
+ params << "&l[#{i}]=#{URI.escape(item['secs'])}"
143
+ params << "&b[#{i}]=#{URI.escape(item['album'])}"
144
+ params << "&n[#{i}]=#{URI.escape(item['tracknumber'])}"
145
+ params << "&m[#{i}]=#{URI.escape(item['mbtrackid'])}"
146
+ i+=1
147
+ end
148
+
149
+ send_submission(@submission_url, params)
150
+ @queue = [] # clean it
151
+ end
152
+
153
+ # Submits the track queue POST data to the API.
154
+ # If any error occurs it raises exception.
155
+
156
+ def send_submission(url, params)
157
+
158
+ url = URI.parse(url)
159
+ http = Net::HTTP.new(url.host)
160
+ response = http.post(url.path, params).body
161
+
162
+ case response
163
+ when /OK/
164
+ # nice
165
+ when /BADSESSION/
166
+ raise BadSessionError
167
+ else
168
+ puts response.inspect
169
+ raise RequestFailedError, response
170
+ end
171
+ response
172
+ end
173
+
174
+ end
175
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tinyscrobbler
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Rogerio Vicente
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-10 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby-mp3info
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.6.13
24
+ version:
25
+ description:
26
+ email: rogerpvl@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - Changelog
37
+ - example.rb
38
+ - lib/audiometa.rb
39
+ - lib/tinyscrobbler.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/rogeriopvl/tinyscrobbler
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project: tinyscrobbler
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: A very lightweight last.fm scrobbler library written in ruby.
68
+ test_files: []
69
+