gingerhendrix / scrobbler2

A ruby library for last.fm webservices v2

This URL has Read+Write access

commit  1ec54d5c67384010f470dc3b6785ef6f317aa834
tree    10e5f1b4f05b67963097326bca00ec522e9e469d
parent  f89fb4f15935b52bcc149d5975f1cac9cc36edeb
README.rdoc

Scrobbler2

Scrobbler2 is a wrapper for the Last.fm web services (www.last.fm/api), inspired by Jnunemaker’s Original Scrobbler Gem (github.com/jnunemaker/scrobbler).

Installation

Install via ruby-gems

  sudo gem install gingerhendrix-scrobbler2 --source http://gems.github.com

Requirements

Scrobbler2 requires

Usage

API Key

All last.fm requests require an API Key. Register for an API key at www.last.fm/api

  Scrobbler2::Base.api_key = "abcdefghijklmnopqrstuvwxyz123456"

Authentication

Some requests (eg. write requests) require authentication. Authenticated request require an api secret (provided with your api key) and a session key.

  Scrobbler2::Base.api_secret = "9876543210zyxwvutsrqponmlkjihgfe"
  Scrobbler2::Base.session_key = "qwertyuioplkjhgfdsamnbvcxz"

Obtaining a session key

scrobbler2 currently supports last.fm’s desktop authentication protocol. In this process

  1. The app requests a authentication token
  2. The app sends the user to a last.fm authentication page
  3. The user agrees to allow the the app acess to their data.
  4. The app requests a session key.
  5. The app makes an authenticated request with the session key.
     auth = Scrobbler2::Auth.new
     token = auth.token
     puts "Auth Token #{token} \n"
     token.should_not be_nil
     puts "Now go to #{auth.url} and authorise\n"
    
     gets #Wait for enter
    
     session = auth.session
     puts "Session: #{session.inspect} \n"
     session.should be_kind_of(Hash)
     session['key'].should_not be_nil
    
     Scrobbler2::Base.session_key = session['key']
         user = Scrobbler2::User.new(username)
         user.info
    

Users

        username = 'gingerhendrix'
        user = Scrobbler2::User.new(username)

        puts "#{username}'s Recent Tracks"
        puts "=" * (username.length + 16)
        user.recent_tracks["track"].each { |t| puts t["name"] }

        puts
        puts

        puts "#{username}'s Top Tracks"
        puts "=" * (username.length + 13)
        user.top_tracks["track"].each { |t| puts "(#{t["playcount"]} plays) #{t["name"]}" }

Albums

        album = Scrobbler2::Album.new('Carrie Underwood', 'Some Hearts')

        puts "Album: #{album.info["name"]}"
        puts "Artist: #{album.info["artist"]}"
        puts "Listeners: #{album.info["listeners"]}"
        puts "URL: #{album.info["url"]}"
        puts "Release Date: #{album.info["releasedate"]}"

        puts
        puts

        puts "Album Tags"
        longest_tag_name = album.info["toptags"]["tag"].collect {|t| t["name"] }.sort {|x, y| y.length <=> x.length }.first.length
        puts "=" * longest_tag_name
        album.info["toptags"]["tag"].each { |t| puts t["name"] }

Artists

        artist = Scrobbler2::Artist.new('Carrie Underwood')

        puts 'Top Tracks'
        puts "=" * 10
        artist.top_tracks["track"].each { |t| puts "(#{t["playcount"]} plays) #{t["name"]}" }

        puts

        puts 'Similar Artists'
        puts "=" * 15
        artist.info["similar"]["artist"].each { |a| puts "#{a["name"]}" }

Tags

        tag = Scrobbler2::Tag.new('country')

        puts 'Top Albums'
        tag.top_albums["album"].each { |a| puts "(#{a["tagcount"]} tags) '#{a["name"]}' by #{a["artist"]["name"]}" }

        puts

        puts 'Top Tracks'
        tag.top_tracks.each { |t| puts "(#{t["tagcount"]} tags) '#{t["name"]}' by #{t["artist"]["name"]}" }

Tracks

        track = Scrobbler2::Track.new('Carrie Underwood', 'Before He Cheats')
        puts 'Fans'
        puts "=" * 4
        puts track.info["listeners"]

Development

Scrobbler2 is still in development. If you want to help out please feel free to fork the project on github (github.com/gingerhendrix/scrobbler2).

Build and install

The scrobbler2 gem can be built and installed via rake.

  rake build    # builds the gem (in pkg/)
  rake install  # builds and installs the gem (using sudo gem install)

Tests

Scrobbler2 has unit and acceptance tests written using RSpec. The tests can be run using rake.

  rake test:unit        # runs the unit tests
  rake test:acceptance  # runs the acceptance tests

The acceptance tests run against the live last.fm api. You will need to set the api_key and authentication parameters in test/acceptance/test_helper.rb before running these tests. You can obtain a session key by running the auth_test, this test is not invoked with the rest of the acceptance suite.

  ruby test/acceptance/auth/auth_test.rb

Internals

Scrobbler2 api methods use a simple set of macros to implement the api. All the hard work goes on in Scrobbler2::Base.

TODO

  • Write requests.
  • Submissions
  • Radio API