jonaustin-rockstar 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +8 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +62 -0
- data/History.txt +6 -0
- data/MIT-LICENSE +19 -0
- data/Manifest +72 -0
- data/README.md +173 -0
- data/Rakefile +32 -0
- data/VERSION +1 -0
- data/examples/.gitignore +1 -0
- data/examples/album.rb +16 -0
- data/examples/artist.rb +25 -0
- data/examples/geo.rb +60 -0
- data/examples/lastfm.yml_example +3 -0
- data/examples/scrobble.rb +45 -0
- data/examples/tag.rb +20 -0
- data/examples/track.rb +14 -0
- data/examples/user.rb +18 -0
- data/lib/rockstar.rb +46 -0
- data/lib/rockstar/album.rb +104 -0
- data/lib/rockstar/artist.rb +183 -0
- data/lib/rockstar/auth.rb +38 -0
- data/lib/rockstar/base.rb +48 -0
- data/lib/rockstar/chart.rb +31 -0
- data/lib/rockstar/event.rb +73 -0
- data/lib/rockstar/geo.rb +47 -0
- data/lib/rockstar/library.rb +13 -0
- data/lib/rockstar/metro.rb +26 -0
- data/lib/rockstar/rest.rb +66 -0
- data/lib/rockstar/session.rb +19 -0
- data/lib/rockstar/tag.rb +100 -0
- data/lib/rockstar/track.rb +265 -0
- data/lib/rockstar/user.rb +205 -0
- data/lib/rockstar/venue.rb +59 -0
- data/lib/rockstar/version.rb +3 -0
- data/rockstar.gemspec +176 -0
- data/test/fixtures/xml/album/getinfo_album_Radio_Retalation_artist_Thievery_Corporation.xml +21 -0
- data/test/fixtures/xml/album/getinfo_album_Some_Hearts_artist_Carrie_Underwood.xml +63 -0
- data/test/fixtures/xml/artist/getevents_artist_Metallica.xml +1064 -0
- data/test/fixtures/xml/artist/getimages_artist_Metallica_page_2.xml +1074 -0
- data/test/fixtures/xml/artist/getinfo_artist_Metallica.xml +115 -0
- data/test/fixtures/xml/artist/getinfo_mbid_65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab.xml +115 -0
- data/test/fixtures/xml/artist/getsimilar_artist_Metallica.xml +1203 -0
- data/test/fixtures/xml/artist/gettopalbums_artist_Metallica.xml +704 -0
- data/test/fixtures/xml/artist/gettopfans_artist_Metallica.xml +504 -0
- data/test/fixtures/xml/artist/gettoptags_artist_Metallica.xml +403 -0
- data/test/fixtures/xml/artist/gettoptracks_artist_Metallica.xml +800 -0
- data/test/fixtures/xml/geo/getevents_location_london.xml +531 -0
- data/test/fixtures/xml/geo/getmetros_country_germany.xml +44 -0
- data/test/fixtures/xml/geo/gettopartists_country_spain_limit_50_page_.xml +561 -0
- data/test/fixtures/xml/geo/gettoptracks_country_spain_limit_50_page_.xml +1004 -0
- data/test/fixtures/xml/library/getalbums_user_jnunemaker.xml +704 -0
- data/test/fixtures/xml/library/getartists_user_jnunemaker.xml +554 -0
- data/test/fixtures/xml/tag/gettopalbums_tag_rock.xml +654 -0
- data/test/fixtures/xml/tag/gettopartists_tag_rock.xml +504 -0
- data/test/fixtures/xml/tag/gettoptags.xml +1253 -0
- data/test/fixtures/xml/tag/gettoptracks_tag_rock.xml +704 -0
- data/test/fixtures/xml/track/getinfo_artist_Carrie_Underwood_track_Before_He_Cheats.xml +55 -0
- data/test/fixtures/xml/track/getinfo_artist_Karrie_Underwood_track_Before_He_Cheats.xml +55 -0
- data/test/fixtures/xml/track/getsimilar_artist_Carrie_Underwood_limit_10_track_Before_He_Cheats.xml +185 -0
- data/test/fixtures/xml/track/gettopfans_artist_Carrie_Underwood_track_Before_He_Cheats.xml +504 -0
- data/test/fixtures/xml/track/gettoptags_artist_Carrie_Underwood_track_Before_He_Cheats.xml +403 -0
- data/test/fixtures/xml/track/gettoptags_artist_Carrie_Underwood_track_Wasted.xml +405 -0
- data/test/fixtures/xml/track/love_artist_Carrie_Underwood_sk_tag_track_Before_He_Cheats.xml +2 -0
- data/test/fixtures/xml/track/updatenowplaying_artist_Carrie_Underwood_sk_tag_track_Before_He_Cheats.xml +10 -0
- data/test/fixtures/xml/user/getevents_sk_token_user_bodot.xml +513 -0
- data/test/fixtures/xml/user/getfriends_user_jnunemaker.xml +173 -0
- data/test/fixtures/xml/user/getinfo_user_jnunemaker.xml +22 -0
- data/test/fixtures/xml/user/getinfo_user_oaknd1.xml +22 -0
- data/test/fixtures/xml/user/getinfo_user_wharle.xml +22 -0
- data/test/fixtures/xml/user/getlovedtracks_user_jnunemaker.xml +775 -0
- data/test/fixtures/xml/user/getneighbours_user_jnunemaker.xml +503 -0
- data/test/fixtures/xml/user/getrecenttracks_user_jnunemaker.xml +133 -0
- data/test/fixtures/xml/user/getrecommendedartists_sk_token_user_jnunemaker.xml +553 -0
- data/test/fixtures/xml/user/gettopalbums_user_jnunemaker.xml +704 -0
- data/test/fixtures/xml/user/gettopartists_period_3month_user_jnunemaker.xml +710 -0
- data/test/fixtures/xml/user/gettopartists_period_overall_user_jnunemaker.xml +554 -0
- data/test/fixtures/xml/user/gettoptags_user_jnunemaker.xml +63 -0
- data/test/fixtures/xml/user/gettoptracks_user_jnunemaker.xml +750 -0
- data/test/fixtures/xml/user/getweeklyalbumchart_from_1138536002_to_1139140802_user_jnunemaker.xml +143 -0
- data/test/fixtures/xml/user/getweeklyalbumchart_from__to__user_jnunemaker.xml +31 -0
- data/test/fixtures/xml/user/getweeklyartistchart_from_1138536002_to_1139140802_user_jnunemaker.xml +201 -0
- data/test/fixtures/xml/user/getweeklyartistchart_from__to__user_jnunemaker.xml +21 -0
- data/test/fixtures/xml/user/getweeklychartlist_user_jnunemaker.xml +232 -0
- data/test/fixtures/xml/user/getweeklytrackchart_from_1138536002_to_1139140802_user_jnunemaker.xml +883 -0
- data/test/fixtures/xml/user/getweeklytrackchart_from__to__user_jnunemaker.xml +423 -0
- data/test/fixtures/xml/venue/getevents_venue_8807850.xml +280 -0
- data/test/fixtures/xml/venue/search_country__limit__page__venue_country_DE_venue_cotton.xml +72 -0
- data/test/mocks/rest.rb +50 -0
- data/test/test_helper.rb +18 -0
- data/test/unit/test_album.rb +76 -0
- data/test/unit/test_artist.rb +123 -0
- data/test/unit/test_chart.rb +35 -0
- data/test/unit/test_geo.rb +51 -0
- data/test/unit/test_library.rb +65 -0
- data/test/unit/test_rockstar.rb +15 -0
- data/test/unit/test_tag.rb +57 -0
- data/test/unit/test_track.rb +93 -0
- data/test/unit/test_user.rb +309 -0
- data/test/unit/test_venue.rb +20 -0
- metadata +243 -0
data/examples/geo.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'rockstar'))
|
2
|
+
|
3
|
+
# Please enter your API-Keys into lastfm.yml first.
|
4
|
+
# You can find them here : http://www.lastfm.de/api/account
|
5
|
+
Rockstar.lastfm = YAML.load_file(File.join(File.dirname(__FILE__), 'lastfm.yml'))
|
6
|
+
|
7
|
+
geo = Rockstar::Geo.new
|
8
|
+
|
9
|
+
geo.events(:location => 'london').each{|e| p "#{e.title} at #{e.venue.name}"}
|
10
|
+
|
11
|
+
|
12
|
+
# Get Top Tracks by country of the last week
|
13
|
+
#<toptracks country="Spain">
|
14
|
+
# <track rank="1">
|
15
|
+
# <name>Violet Hill</name>
|
16
|
+
# <playcount>1055</playcount>
|
17
|
+
# <mbid/>
|
18
|
+
# <url>http://www.last.fm/music/Coldplay/_/Violet+Hill</url>
|
19
|
+
# <streamable fulltrack="0">1</streamable>
|
20
|
+
# <artist>
|
21
|
+
# <name>Coldplay</name>
|
22
|
+
# <mbid>cc197bad-dc9c-440d-a5b5-d52ba2e14234</mbid>
|
23
|
+
# <url>http://www.last.fm/music/Coldplay</url>
|
24
|
+
# </artist>
|
25
|
+
# <image size="small">...</image>
|
26
|
+
# <image size="medium">...</image>
|
27
|
+
# <image size="large">...</image>
|
28
|
+
# </track>
|
29
|
+
# ...
|
30
|
+
#</toptracks>
|
31
|
+
geo.toptracks('united kingdom', 10).each{|t| p "#{t.name} by #{t.artist}"}
|
32
|
+
|
33
|
+
|
34
|
+
#Get the most popular artists on Last.fm by country
|
35
|
+
#<topartists country="Spain" page="1" perPage="50" totalPages="10" total="500">
|
36
|
+
#
|
37
|
+
# <artist rank="1">
|
38
|
+
# <name>Muse</name>
|
39
|
+
# <listeners>1736</listeners>
|
40
|
+
# <mbid>1695c115-bf3f-4014-9966-2b0c50179193</mbid>
|
41
|
+
# <url>http://www.last.fm/music/Muse</url>
|
42
|
+
# <streamable>1</streamable>
|
43
|
+
# <image size="small">http://userserve-ak.last.fm/serve/34/416065.jpg</image>
|
44
|
+
# <image size="medium">http://userserve-ak.last.fm/serve/64/416065.jpg</image>
|
45
|
+
# <image size="large">http://userserve-ak.last.fm/serve/126/416065.jpg</image>
|
46
|
+
# <image size="extralarge">http://userserve-ak.last.fm/serve/252/416065.jpg</image>
|
47
|
+
# <image size="mega">http://userserve-ak.last.fm/serve/500/416065/Muse.jpg</image>
|
48
|
+
# </artist> <artist rank="2">
|
49
|
+
# <name>Coldplay</name>
|
50
|
+
# <listeners>1497</listeners>
|
51
|
+
# <mbid>cc197bad-dc9c-440d-a5b5-d52ba2e14234</mbid>
|
52
|
+
# <url>http://www.last.fm/music/Coldplay</url>
|
53
|
+
# <streamable>1</streamable>
|
54
|
+
# <image size="small">http://userserve-ak.last.fm/serve/34/210303.jpg</image>
|
55
|
+
# <image size="medium">http://userserve-ak.last.fm/serve/64/210303.jpg</image>
|
56
|
+
# <image size="large">http://userserve-ak.last.fm/serve/126/210303.jpg</image>
|
57
|
+
# <image size="extralarge">http://userserve-ak.last.fm/serve/252/210303.jpg</image>
|
58
|
+
# <image size="mega">http://userserve-ak.last.fm/serve/500/210303/Coldplay.jpg</image>
|
59
|
+
# </artist> <a
|
60
|
+
geo.topartists('united kingdom', 10).each{|a| p "artist #{a.name} with #{a.listenercount} listeners"}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'rockstar'))
|
2
|
+
|
3
|
+
# Please enter your API-Keys into lastfm.yml first.
|
4
|
+
# You can find them here : http://www.lastfm.de/api/account
|
5
|
+
Rockstar.lastfm = YAML.load_file(File.join(File.dirname(__FILE__), 'lastfm.yml'))
|
6
|
+
|
7
|
+
# This is the desktop app aproach to token auth. See Rockstar::TokenAuth for
|
8
|
+
# details on how to get a token for a web app.
|
9
|
+
|
10
|
+
a = Rockstar::Auth.new
|
11
|
+
token = a.token
|
12
|
+
|
13
|
+
puts
|
14
|
+
puts "Please open http://www.last.fm/api/auth/?api_key=#{Rockstar.lastfm_api_key}&token=#{token}"
|
15
|
+
puts
|
16
|
+
puts "Press enter when done."
|
17
|
+
|
18
|
+
gets
|
19
|
+
|
20
|
+
session = a.session(token)
|
21
|
+
|
22
|
+
Rockstar::Track.scrobble(
|
23
|
+
:session_key => session.key,
|
24
|
+
:track => "Viva La Vida",
|
25
|
+
:artist => "Coldplay",
|
26
|
+
:album => "Viva La Vida",
|
27
|
+
:time => Time.new,
|
28
|
+
:length => 244,
|
29
|
+
:track_number => 7
|
30
|
+
)
|
31
|
+
|
32
|
+
# Love the Song :
|
33
|
+
l_status = Rockstar::Track.new('Coldplay', 'Viva La Vida').love(session.key)
|
34
|
+
|
35
|
+
puts "Love track status : #{l_status}"
|
36
|
+
|
37
|
+
Rockstar::Track.updateNowPlaying(
|
38
|
+
:session_key => session.key,
|
39
|
+
:track => "Viva La Vida",
|
40
|
+
:artist => "Coldplay",
|
41
|
+
:album => "Viva La Vida",
|
42
|
+
:time => Time.new,
|
43
|
+
:length => 244,
|
44
|
+
:track_number => 7
|
45
|
+
)
|
data/examples/tag.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'rockstar'))
|
2
|
+
|
3
|
+
# Please enter your API-Keys into lastfm.yml first.
|
4
|
+
# You can find them here : http://www.lastfm.de/api/account
|
5
|
+
Rockstar.lastfm = YAML.load_file(File.join(File.dirname(__FILE__), 'lastfm.yml'))
|
6
|
+
|
7
|
+
tag = Rockstar::Tag.new('country')
|
8
|
+
|
9
|
+
puts 'Top Tags '
|
10
|
+
Rockstar::Tag.top_tags.each { |a| puts "(#{a.count}) #{a.name}" }
|
11
|
+
|
12
|
+
puts
|
13
|
+
|
14
|
+
puts 'Top Albums'
|
15
|
+
tag.top_albums.each { |a| puts "(#{a.count}) #{a.name} by #{a.artist}" }
|
16
|
+
|
17
|
+
puts
|
18
|
+
|
19
|
+
puts 'Top Tracks'
|
20
|
+
tag.top_tracks.each { |t| puts "(#{t.count}) #{t.name} by #{t.artist}" }
|
data/examples/track.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'rockstar'))
|
2
|
+
require 'pp'
|
3
|
+
|
4
|
+
# Please enter your API-Keys into lastfm.yml first.
|
5
|
+
# You can find them here : http://www.lastfm.de/api/account
|
6
|
+
Rockstar.lastfm = YAML.load_file(File.join(File.dirname(__FILE__), 'lastfm.yml'))
|
7
|
+
|
8
|
+
track = Rockstar::Track.new('Carrie Underwood', 'Before He Cheats')
|
9
|
+
puts 'Fans'
|
10
|
+
puts "=" * 4
|
11
|
+
track.fans.each { |u| puts "(#{u.weight}) #{u.username}" }
|
12
|
+
|
13
|
+
track = Rockstar::Track.new('U2 & Green Day', 'The Saints Are Coming')
|
14
|
+
pp track.tags
|
data/examples/user.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'rockstar'))
|
2
|
+
|
3
|
+
# Please enter your API-Keys into lastfm.yml first.
|
4
|
+
# You can find them here : http://www.lastfm.de/api/account
|
5
|
+
Rockstar.lastfm = YAML.load_file(File.join(File.dirname(__FILE__), 'lastfm.yml'))
|
6
|
+
|
7
|
+
user = Rockstar::User.new('jnunemaker')
|
8
|
+
|
9
|
+
puts "#{user.username}'s Recent Tracks"
|
10
|
+
puts "=" * (user.username.length + 16)
|
11
|
+
user.recent_tracks.each { |t| puts t.name }
|
12
|
+
|
13
|
+
puts
|
14
|
+
puts
|
15
|
+
|
16
|
+
puts "#{user.username}'s Top Tracks"
|
17
|
+
puts "=" * (user.username.length + 13)
|
18
|
+
user.top_tracks.each { |t| puts "(#{t.playcount}) #{t.name}" }
|
data/lib/rockstar.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
$: << File.expand_path(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'cgi'
|
5
|
+
require 'hpricot'
|
6
|
+
require 'active_support'
|
7
|
+
require 'active_support/core_ext/object/blank'
|
8
|
+
require 'active_support/core_ext/string/inflections'
|
9
|
+
require 'time'
|
10
|
+
require 'yaml'
|
11
|
+
|
12
|
+
require 'rockstar/base'
|
13
|
+
require 'rockstar/version'
|
14
|
+
|
15
|
+
require 'rockstar/album'
|
16
|
+
require 'rockstar/artist'
|
17
|
+
require 'rockstar/chart'
|
18
|
+
require 'rockstar/event'
|
19
|
+
require 'rockstar/geo'
|
20
|
+
require 'rockstar/library'
|
21
|
+
require 'rockstar/metro'
|
22
|
+
require 'rockstar/user'
|
23
|
+
require 'rockstar/tag'
|
24
|
+
require 'rockstar/track'
|
25
|
+
require 'rockstar/venue'
|
26
|
+
|
27
|
+
require 'rockstar/auth'
|
28
|
+
require 'rockstar/session'
|
29
|
+
require 'rockstar/rest'
|
30
|
+
|
31
|
+
module Rockstar
|
32
|
+
extend self
|
33
|
+
|
34
|
+
def lastfm=(args)
|
35
|
+
@api_key = args["api_key"] || args[:api_key]
|
36
|
+
@api_secret = args["api_secret"] || args[:api_secret]
|
37
|
+
end
|
38
|
+
|
39
|
+
def lastfm_api_key
|
40
|
+
@api_key
|
41
|
+
end
|
42
|
+
|
43
|
+
def lastfm_api_secret
|
44
|
+
@api_secret
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# Getting information about an album such as release date and the summary or description on it is very easy.
|
2
|
+
#
|
3
|
+
# album = Rockstar::Album.new('Carrie Underwood', 'Some Hearts', :include_info => true)
|
4
|
+
#
|
5
|
+
# puts "Album: #{album.name}"
|
6
|
+
# puts "Artist: #{album.artist}"
|
7
|
+
# puts "URL: #{album.url}"
|
8
|
+
# puts "Release Date: #{album.release_date.strftime('%m/%d/%Y')}"
|
9
|
+
#
|
10
|
+
# Would output:
|
11
|
+
#
|
12
|
+
# Album: Some Hearts
|
13
|
+
# Artist: Carrie Underwood
|
14
|
+
# URL: http://www.last.fm/music/Carrie+Underwood/Some+Hearts
|
15
|
+
# Release Date: 11/15/2005
|
16
|
+
#
|
17
|
+
module Rockstar
|
18
|
+
class Album < Base
|
19
|
+
attr_accessor :artist, :artist_mbid, :name, :mbid, :playcount, :rank, :url, :release_date
|
20
|
+
attr_accessor :image_large, :image_medium, :image_small, :summary, :content, :images
|
21
|
+
|
22
|
+
# needed on top albums for tag
|
23
|
+
attr_accessor :count, :streamable
|
24
|
+
|
25
|
+
# needed for weekly album charts
|
26
|
+
attr_accessor :chartposition
|
27
|
+
|
28
|
+
class << self
|
29
|
+
def find(artist, name, o={})
|
30
|
+
new(artist, name, o)
|
31
|
+
end
|
32
|
+
|
33
|
+
def new_from_xml(xml, doc=nil)
|
34
|
+
name = (xml).at(:name).inner_html if (xml).at(:name)
|
35
|
+
name = xml['name'] if name.nil? && xml['name']
|
36
|
+
name = xml.at(:title).inner_html if name.nil? && (xml).at(:title)
|
37
|
+
artist = (xml).at(:artist).at(:name).inner_html if (xml).at(:artist) && (xml).at(:artist).at(:name)
|
38
|
+
artist = (xml).at(:artist).inner_html if artist.nil? && (xml).at(:artist)
|
39
|
+
artist = doc.root['artist'] if artist.nil? && doc && doc.root['artist']
|
40
|
+
|
41
|
+
album = Album.new(artist, name)
|
42
|
+
album.load_info(xml)
|
43
|
+
album
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def initialize(artist, name, o={})
|
48
|
+
raise ArgumentError, "Artist is required" if artist.blank?
|
49
|
+
raise ArgumentError, "Name is required" if name.blank?
|
50
|
+
@artist = artist
|
51
|
+
@name = name
|
52
|
+
|
53
|
+
options = {:include_info => false}.merge(o)
|
54
|
+
load_info if options[:include_info]
|
55
|
+
end
|
56
|
+
|
57
|
+
def load_info(xml=nil)
|
58
|
+
unless xml
|
59
|
+
doc = self.class.fetch_and_parse("album.getInfo", {:artist => @artist, :album =>@name})
|
60
|
+
xml = (doc / :album).first
|
61
|
+
end
|
62
|
+
|
63
|
+
return self if xml.nil?
|
64
|
+
|
65
|
+
self.artist_mbid = (xml).at(:artist)['mbid'] if (xml).at(:artist) && (xml).at(:artist)['mbid']
|
66
|
+
self.artist_mbid = (xml).at(:artist).at(:mbid).inner_html if artist_mbid.nil? && (xml).at(:artist) && (xml).at(:artist).at(:mbid)
|
67
|
+
self.mbid = (xml).at(:mbid).inner_html if (xml).at(:mbid)
|
68
|
+
self.playcount = (xml).at(:playcount).inner_html if (xml).at(:playcount)
|
69
|
+
self.rank = xml['rank'] if xml['rank']
|
70
|
+
self.rank = (xml).at(:rank).inner_html if (xml).at(:rank) if rank.nil?
|
71
|
+
self.url = Base.fix_url((xml).at(:url).inner_html) if (xml).at(:url)
|
72
|
+
|
73
|
+
self.summary = (xml).at(:summary).to_plain_text if (xml).at(:summary)
|
74
|
+
self.content = (xml).at(:content).to_plain_text if (xml).at(:content)
|
75
|
+
|
76
|
+
self.release_date = Base.parse_time((xml).at(:releasedate).inner_html.strip) if (xml).at(:releasedate)
|
77
|
+
self.chartposition = rank
|
78
|
+
|
79
|
+
self.images = {}
|
80
|
+
(xml/'image').each {|image|
|
81
|
+
self.images[image['size']] = image.inner_html if self.images[image['size']].nil?
|
82
|
+
}
|
83
|
+
|
84
|
+
self.image_large = images['large']
|
85
|
+
self.image_medium = images['medium']
|
86
|
+
self.image_small = images['small']
|
87
|
+
|
88
|
+
# needed on top albums for tag
|
89
|
+
self.count = xml['count'] if xml['count']
|
90
|
+
self.streamable = xml['streamable'] if xml['streamable']
|
91
|
+
|
92
|
+
self
|
93
|
+
end
|
94
|
+
|
95
|
+
def image(which=:small)
|
96
|
+
which = which.to_s
|
97
|
+
raise ArgumentError unless ['small', 'medium', 'large', 'extralarge'].include?(which)
|
98
|
+
if (self.images.nil?)
|
99
|
+
load_info
|
100
|
+
end
|
101
|
+
self.images[which]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,183 @@
|
|
1
|
+
# Below are examples of how to find an artists top tracks and similar artists.
|
2
|
+
#
|
3
|
+
# artist = Rockstar::Artist.new('Carrie Underwood')
|
4
|
+
#
|
5
|
+
# puts 'Top Tracks'
|
6
|
+
# puts "=" * 10
|
7
|
+
# artist.top_tracks.each { |t| puts "#{t.name}" }
|
8
|
+
#
|
9
|
+
# puts
|
10
|
+
#
|
11
|
+
# puts 'Similar Artists'
|
12
|
+
# puts "=" * 15
|
13
|
+
# artist.similar.each { |a| puts "(#{a.match}%) #{a.name}" }
|
14
|
+
#
|
15
|
+
# Would output something similar to:
|
16
|
+
#
|
17
|
+
# Top Tracks
|
18
|
+
# ==========
|
19
|
+
# (8797) Before He Cheats
|
20
|
+
# (3574) Don't Forget to Remember Me
|
21
|
+
# (3569) Wasted
|
22
|
+
# (3246) Some Hearts
|
23
|
+
# (3142) Jesus, Take the Wheel
|
24
|
+
# (2600) Starts With Goodbye
|
25
|
+
# (2511) Jesus Take The Wheel
|
26
|
+
# (2423) Inside Your Heaven
|
27
|
+
# (2328) Lessons Learned
|
28
|
+
# (2040) I Just Can't Live a Lie
|
29
|
+
# (1899) Whenever You Remember
|
30
|
+
# (1882) We're Young and Beautiful
|
31
|
+
# (1854) That's Where It Is
|
32
|
+
# (1786) I Ain't in Checotah Anymore
|
33
|
+
# (1596) The Night Before (Life Goes On)
|
34
|
+
#
|
35
|
+
# Similar Artists
|
36
|
+
# ===============
|
37
|
+
# (100%) Rascal Flatts
|
38
|
+
# (84.985%) Keith Urban
|
39
|
+
# (84.007%) Kellie Pickler
|
40
|
+
# (82.694%) Katharine McPhee
|
41
|
+
# (81.213%) Martina McBride
|
42
|
+
# (79.397%) Faith Hill
|
43
|
+
# (77.121%) Tim McGraw
|
44
|
+
# (75.191%) Jessica Simpson
|
45
|
+
# (75.182%) Sara Evans
|
46
|
+
# (75.144%) The Wreckers
|
47
|
+
# (73.034%) Kenny Chesney
|
48
|
+
# (71.765%) Dixie Chicks
|
49
|
+
# (71.084%) Kelly Clarkson
|
50
|
+
# (69.535%) Miranda Lambert
|
51
|
+
# (66.952%) LeAnn Rimes
|
52
|
+
# (66.398%) Mandy Moore
|
53
|
+
# (65.817%) Bo Bice
|
54
|
+
# (65.279%) Diana DeGarmo
|
55
|
+
# (65.115%) Gretchen Wilson
|
56
|
+
# (62.982%) Clay Aiken
|
57
|
+
# (62.436%) Ashlee Simpson
|
58
|
+
# (62.160%) Christina Aguilera
|
59
|
+
module Rockstar
|
60
|
+
class Artist < Base
|
61
|
+
attr_accessor :name, :mbid, :listenercount, :playcount, :rank, :url, :thumbnail
|
62
|
+
attr_accessor :summary, :content, :images, :count, :streamable, :tags
|
63
|
+
attr_accessor :chartposition
|
64
|
+
|
65
|
+
# used for similar artists
|
66
|
+
attr_accessor :match
|
67
|
+
|
68
|
+
class << self
|
69
|
+
def new_from_xml(xml, doc=nil)
|
70
|
+
# occasionally name can be found in root of artist element (<artist name="">) rather than as an element (<name>)
|
71
|
+
name = (xml).at(:name).inner_html if (xml).at(:name)
|
72
|
+
name = xml['name'] if name.nil? && xml['name']
|
73
|
+
|
74
|
+
artist = Artist.new(name)
|
75
|
+
artist.load_info(xml)
|
76
|
+
artist
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def initialize(name, o={})
|
81
|
+
raise ArgumentError, "Name or mbid is required" if name.blank? && o[:mbid].blank?
|
82
|
+
@name = name unless name.blank?
|
83
|
+
@mbid = o[:mbid] unless o[:mbid].blank?
|
84
|
+
|
85
|
+
options = {:include_info => false}.merge(o)
|
86
|
+
load_info if options[:include_info]
|
87
|
+
end
|
88
|
+
|
89
|
+
def load_info(xml=nil)
|
90
|
+
unless xml
|
91
|
+
params = @mbid.blank? ? {:artist => @name} : {:mbid => @mbid}
|
92
|
+
|
93
|
+
doc = self.class.fetch_and_parse("artist.getInfo", params)
|
94
|
+
xml = (doc / :artist).first
|
95
|
+
end
|
96
|
+
|
97
|
+
return self if xml.nil?
|
98
|
+
|
99
|
+
self.name = (xml).at(:name).inner_html if (xml).at(:name)
|
100
|
+
self.mbid = (xml).at(:mbid).inner_html if (xml).at(:mbid)
|
101
|
+
self.listenercount = (xml).at(:listeners).inner_html if (xml).at(:listeners)
|
102
|
+
self.playcount = (xml).at(:playcount).inner_html if (xml).at(:playcount)
|
103
|
+
self.chartposition = self.rank = xml['rank'] if xml['rank']
|
104
|
+
self.chartposition = self.rank = (xml).at(:rank).inner_html if (xml).at(:rank) if self.rank.nil?
|
105
|
+
self.url = Base.fix_url((xml).at(:url).inner_html) if (xml).at(:url)
|
106
|
+
|
107
|
+
if bio_xml = xml.at(:bio)
|
108
|
+
self.summary = bio_xml.at(:summary).to_plain_text if bio_xml.at(:summary)
|
109
|
+
self.content = bio_xml.at(:content).to_plain_text if bio_xml.at(:content)
|
110
|
+
end
|
111
|
+
|
112
|
+
self.images = {}
|
113
|
+
(xml/'image').each {|image|
|
114
|
+
self.images[image['size']] = image.inner_html if self.images[image['size']].nil?
|
115
|
+
}
|
116
|
+
|
117
|
+
self.thumbnail = self.images['small']
|
118
|
+
self.match = (xml).at(:match).inner_html if (xml).at(:match)
|
119
|
+
|
120
|
+
# in top artists for tag
|
121
|
+
self.count = xml['count'] if xml['count']
|
122
|
+
self.streamable = xml['streamable'] if xml['streamable']
|
123
|
+
|
124
|
+
self.streamable = (xml).at(:streamable).inner_html == '1' ? 'yes' : 'no' if self.streamable.nil? && (xml).at(:streamable)
|
125
|
+
|
126
|
+
self.tags = (xml/'tag/name').collect(&:inner_html)
|
127
|
+
|
128
|
+
self
|
129
|
+
end
|
130
|
+
|
131
|
+
def events(force=false)
|
132
|
+
get_instance("artist.getEvents", :events, :event, {:artist => @name}, force)
|
133
|
+
end
|
134
|
+
|
135
|
+
def similar(force=false)
|
136
|
+
get_instance("artist.getSimilar", :similar, :artist, {:artist => @name}, force)
|
137
|
+
end
|
138
|
+
|
139
|
+
def top_fans(force=false)
|
140
|
+
get_instance("artist.getTopFans", :top_fans, :user, {:artist => @name}, force)
|
141
|
+
end
|
142
|
+
|
143
|
+
def top_tracks(force=false)
|
144
|
+
get_instance("artist.getTopTracks", :top_tracks, :track, {:artist => @name}, force)
|
145
|
+
end
|
146
|
+
|
147
|
+
def top_albums(force=false)
|
148
|
+
get_instance("artist.getTopAlbums", :top_albums, :album, {:artist => @name}, force)
|
149
|
+
end
|
150
|
+
|
151
|
+
def top_tags(force=false)
|
152
|
+
get_instance("artist.getTopTags", :top_tags, :tag, {:artist => @name}, force)
|
153
|
+
end
|
154
|
+
|
155
|
+
def image(which=:medium)
|
156
|
+
which = which.to_s
|
157
|
+
raise ArgumentError unless ['small', 'medium', 'large', 'extralarge'].include?(which)
|
158
|
+
load_info if self.images.nil?
|
159
|
+
self.images[which]
|
160
|
+
end
|
161
|
+
|
162
|
+
def user_images(opts={})
|
163
|
+
if mbid.nil?
|
164
|
+
opts[:artist] = name
|
165
|
+
else
|
166
|
+
opts[:mbid] = mbid
|
167
|
+
end
|
168
|
+
|
169
|
+
images = []
|
170
|
+
image_doc = self.class.fetch_and_parse("artist.getImages", opts, false)
|
171
|
+
(image_doc/'image').each do |xml|
|
172
|
+
image_sizes = {}
|
173
|
+
xml.search('/sizes/size').each do |image|
|
174
|
+
image_sizes[image['name']] = image.inner_html
|
175
|
+
end
|
176
|
+
|
177
|
+
images << image_sizes
|
178
|
+
end
|
179
|
+
|
180
|
+
images
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|