Class: LastFM

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/clients/lastfm.rb

Overview

Last.fm API client, implemented using httparty (github.com/jnunemaker/httparty/)

NOTE: you need to set the LAST_FM_API_KEY environment variable to your API key before you can use this class.

Class Method Summary collapse

Class Method Details

.friends(user_id) ⇒ Object



45
46
47
# File 'lib/clients/lastfm.rb', line 45

def friends(user_id)
  query('user.getfriends', :user=>user_id, :recenttracks=>false)['friends']['user']
end

.friends_loved_tracks(user_id, limit = 5) ⇒ Object

retrieve tracks recently loved by friends



54
55
56
57
58
59
60
# File 'lib/clients/lastfm.rb', line 54

def friends_loved_tracks(user_id, limit=5)
  friends(user_id).inject({}) do |h, u|
    h[u['name']] = loved_tracks(u['name'], limit)
    sleep 0.5
    h
  end
end

.loved_tracks(user_id, limit = 5) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/clients/lastfm.rb', line 18

def loved_tracks(user_id, limit=5)
  if tracks = query('user.getLovedTracks', :user=>user_id, :limit=>limit)['lovedtracks']['track']

    [tracks].flatten.map do |r|
      { 'artist' => r['artist']['name'], 'title'=>r['name'], 'mbid' => r['mbid'] } 
    end
  else
    []
  end
end

.metro_track_chart(city, country, unique = false, from = nil, to = nil) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/clients/lastfm.rb', line 66

def metro_track_chart(city, country, unique=false, from=nil, to=nil)
  args = { :country=>country, :metro=>city }      
  args.merge!(:start=>from, :end=>to) if from && to
    
  res = query("geo.getMetro#{unique ? 'Unique' : ''}TrackChart", args)
  return [] unless res['toptracks'] && res['toptracks']['track']
  res['toptracks']['track']
end

.metro_weekly_chartlistObject



62
63
64
# File 'lib/clients/lastfm.rb', line 62

def metro_weekly_chartlist
  query("geo.getMetroWeeklyChartlist")['weeklychartlist']['chart'].reverse
end

.neighbours(user_id, max = 10) ⇒ Object



49
50
51
# File 'lib/clients/lastfm.rb', line 49

def neighbours(user_id, max=10)
  query('user.getneighbours', :user=>user_id, :limit=>10)#['friends']['user']
end

.query(method, args = {}) ⇒ Object



75
76
77
78
79
# File 'lib/clients/lastfm.rb', line 75

def query(method, args={})
 result = get("/2.0/", :query => { :method => method }.merge(args))
 raise result['lfm']['error'] if result['lfm'] && result['lfm']['status'] == 'failed'
 result['lfm']
end

.recent_tracks(user_id) ⇒ Object



29
30
31
32
33
# File 'lib/clients/lastfm.rb', line 29

def recent_tracks(user_id)
  query('user.getRecentTracks', :user=>user_id, :limit=>100)['recenttracks']['track'].map do |r|
      { 'artist' => r['artist'], 'title'=>r['name'], 'mbid' => r['mbid'] }
  end
end

.top_tracks(user_id, period = 'overall') ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/clients/lastfm.rb', line 35

def top_tracks(user_id, period='overall')
  unless ['overall', '7day', '3month', '6month', '12month'].include?(period)
    raise ArgumentError, "invalid period"
  end
   
  query('user.getTopTracks', :period=>period, :user=>user_id)['toptracks']['track'].map do |r|
    { 'artist' => r['artist']['name'], 'title'=>r['name'], 'mbid' => r['mbid'] }
  end
end