spotify-metadata 0.1.0
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/lib/spotify.rb +174 -0
- metadata +56 -0
data/lib/spotify.rb
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
require 'httparty'
|
|
2
|
+
require 'ostruct'
|
|
3
|
+
|
|
4
|
+
# Public: Various methods for interacting with the Spotify Metadata API. Please
|
|
5
|
+
# read the Terms of Use for this API (http://is.gd/0Mcl04) before using it.
|
|
6
|
+
#
|
|
7
|
+
# Examples
|
|
8
|
+
#
|
|
9
|
+
# require 'spotify'
|
|
10
|
+
#
|
|
11
|
+
# artists = Spotify.search_artist 'Radiohead'
|
|
12
|
+
# puts artists.first.name # => 'Radiohead'
|
|
13
|
+
#
|
|
14
|
+
# album = Spotify.search_album('The King of Limbs').first
|
|
15
|
+
# puts "#{album.artist.name} - #{album.name}" # => Radiohead - Kid A
|
|
16
|
+
module Spotify
|
|
17
|
+
include HTTParty
|
|
18
|
+
base_uri 'ws.spotify.com'
|
|
19
|
+
format :json
|
|
20
|
+
headers 'Accept' => 'application/json' # Spotify API prefers Accept headers.
|
|
21
|
+
|
|
22
|
+
# Internal: The version Integer of Spotify's API.
|
|
23
|
+
API_VERSION = 1
|
|
24
|
+
|
|
25
|
+
# Public: Searches the Spotify API for artists.
|
|
26
|
+
#
|
|
27
|
+
# name - The name String of the artist.
|
|
28
|
+
# options - The Hash options send along with the request (default: {}).
|
|
29
|
+
# :page - The page of the result set (optional).
|
|
30
|
+
#
|
|
31
|
+
# Returns nil or an Array of Spotify::Artist objects.
|
|
32
|
+
def self.search_artist(name, options = {})
|
|
33
|
+
self.search :artist, options.merge(:q => name)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Public: searches the Spotify API for albums.
|
|
37
|
+
#
|
|
38
|
+
# name - The name String of the album.
|
|
39
|
+
# options - The Hash options send along with the request (default: {}).
|
|
40
|
+
# :page - The page of the result set (optional).
|
|
41
|
+
#
|
|
42
|
+
# Returns nil or an Array of Spotify::Album objects.
|
|
43
|
+
def self.search_album(name, options = {})
|
|
44
|
+
self.search :album, options.merge(:q => name)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Public: searches the Spotify API for tracks.
|
|
48
|
+
#
|
|
49
|
+
# name - The name String of the track.
|
|
50
|
+
# options - The Hash options send along with the request (default: {}).
|
|
51
|
+
# :page - The page of the result set (optional).
|
|
52
|
+
#
|
|
53
|
+
# Returns nil or an Array of Spotify::Track objects.
|
|
54
|
+
def self.search_track(name, options = {})
|
|
55
|
+
self.search :track, options.merge(:q => name)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Public: Looks up an Spotify artist URI.
|
|
59
|
+
#
|
|
60
|
+
# uri - The Spotify URI String that needs to be lookup up.
|
|
61
|
+
# extras - A Symbol that define the detail level expected in the response
|
|
62
|
+
# (default: nil).
|
|
63
|
+
# :album - request basic information about all the albums the
|
|
64
|
+
# artist is featured in.
|
|
65
|
+
# :albumdetail - request detailed information about all the albums
|
|
66
|
+
# the artist is featured in
|
|
67
|
+
#
|
|
68
|
+
# Returns nil or an Spotify::Artist object.
|
|
69
|
+
def self.lookup_artist(uri, extras = nil)
|
|
70
|
+
query = extras.nil? ? { :uri => uri } : { :uri => uri, :extras => extras }
|
|
71
|
+
|
|
72
|
+
artist = Artist.new self.lookup(query)['artist']
|
|
73
|
+
artist.albums.map! { |album| Album.new album['album'] } if extras
|
|
74
|
+
|
|
75
|
+
artist
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Public: Looks up an Spotify album URI.
|
|
79
|
+
#
|
|
80
|
+
# uri - The Spotify URI String that needs to be lookup up.
|
|
81
|
+
# extras - A Symbol that defines the detail level expected in the response
|
|
82
|
+
# (default: nil).
|
|
83
|
+
# :track - request basic information about all tracks in the
|
|
84
|
+
# album.
|
|
85
|
+
# :trackdetail - request detailed information about all tracks in the
|
|
86
|
+
# album.
|
|
87
|
+
#
|
|
88
|
+
# Returns nil or an Spotify::Album object.
|
|
89
|
+
def self.lookup_album(uri, extras = nil)
|
|
90
|
+
query = extras.nil? ? { :uri => uri } : { :uri => uri, :extras => extras }
|
|
91
|
+
|
|
92
|
+
album = Album.new self.lookup(query)['album']
|
|
93
|
+
album.tracks.map! { |track| Track.new track } if extras
|
|
94
|
+
|
|
95
|
+
album
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Public: Looks up an Spotify track URI.
|
|
99
|
+
#
|
|
100
|
+
# uri - The Spotify URI String that needs to be lookup up.
|
|
101
|
+
#
|
|
102
|
+
# Returns nil or an Spotify::Track object.
|
|
103
|
+
def self.lookup_track(uri)
|
|
104
|
+
Track.new self.lookup(:uri => uri)['track']
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Internal: Represents an artist.
|
|
108
|
+
class Artist < OpenStruct
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Internal: Represents an album.
|
|
112
|
+
class Album < OpenStruct
|
|
113
|
+
# Public: Gets the artists.
|
|
114
|
+
#
|
|
115
|
+
# Returns an Array of Spotify::Artist objects.
|
|
116
|
+
def artists
|
|
117
|
+
super.nil? ? super : super.map { |artist| Artist.new artist }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Public: Gets the artist of artists.
|
|
121
|
+
#
|
|
122
|
+
# Returns an Array of Spotify::Artist objects or a Spotify::Artist object
|
|
123
|
+
# if only one artist was returned.
|
|
124
|
+
def artist
|
|
125
|
+
if artists.nil?
|
|
126
|
+
Artist.new :name => super
|
|
127
|
+
elsif artists.size > 1
|
|
128
|
+
artists
|
|
129
|
+
else
|
|
130
|
+
artists.first
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Internal: Represents a track.
|
|
136
|
+
class Track < Album
|
|
137
|
+
# Public: Gets the album.
|
|
138
|
+
#
|
|
139
|
+
# Returns a Spotify::Album object.
|
|
140
|
+
def album
|
|
141
|
+
Album.new super
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
private
|
|
146
|
+
|
|
147
|
+
# Internal: Performs a search query.
|
|
148
|
+
#
|
|
149
|
+
# method - The API method Symbol that needs to be called.
|
|
150
|
+
# query - The query Hash that needs to be passed.
|
|
151
|
+
#
|
|
152
|
+
# Returns nil or an Array of artist, album or track objects.
|
|
153
|
+
def self.search(method, query)
|
|
154
|
+
result = get "/search/#{API_VERSION}/#{method}", :query => query
|
|
155
|
+
|
|
156
|
+
items = result[method.to_s + 's']
|
|
157
|
+
return if items.nil? || items.empty?
|
|
158
|
+
|
|
159
|
+
items.map do |item|
|
|
160
|
+
# TODO: Replace hyphens with underscores, so OpenStruct can use them as
|
|
161
|
+
# methods.
|
|
162
|
+
self.const_get(method.to_s.capitalize).new item
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Internal: Performs a lookup query.
|
|
167
|
+
#
|
|
168
|
+
# query -
|
|
169
|
+
#
|
|
170
|
+
# Returns
|
|
171
|
+
def self.lookup(query)
|
|
172
|
+
get "/lookup/#{API_VERSION}/", :query => query
|
|
173
|
+
end
|
|
174
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: spotify-metadata
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Paul Brickfeld
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-01-25 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: httparty
|
|
16
|
+
requirement: &2152099480 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 0.8.1
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *2152099480
|
|
25
|
+
description: Search Spotify's Metadata API using Ruby.
|
|
26
|
+
email: theboss@nearupon.com
|
|
27
|
+
executables: []
|
|
28
|
+
extensions: []
|
|
29
|
+
extra_rdoc_files: []
|
|
30
|
+
files:
|
|
31
|
+
- lib/spotify.rb
|
|
32
|
+
homepage: https://github.com/britishtea/spotify
|
|
33
|
+
licenses: []
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
none: false
|
|
40
|
+
requirements:
|
|
41
|
+
- - ! '>='
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
|
+
none: false
|
|
46
|
+
requirements:
|
|
47
|
+
- - ! '>='
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
requirements: []
|
|
51
|
+
rubyforge_project:
|
|
52
|
+
rubygems_version: 1.8.13
|
|
53
|
+
signing_key:
|
|
54
|
+
specification_version: 3
|
|
55
|
+
summary: Search Spotify's Metadata API using Ruby.
|
|
56
|
+
test_files: []
|