-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify.rb
63 lines (56 loc) · 1.66 KB
/
spotify.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env ruby
require 'rubygems'
require 'httparty'
require 'rest_client'
require 'json'
require 'pp'
class Spotify
include HTTParty
base_uri 'localhost:3333'
def self.artists(name)
get("/artists", :query => {:name => name})
end
def self.tracks(name, artist=nil)
results = get("/tracks", :query => {:name => name, :artist => artist})
raise results.inspect if results["status"] != "OK"
results["result"].first
end
def self.albums(name)
results = get("/albums", :query => {:name => name, :artist => artist})
raise results.inspect if results["status"] != "OK"
results["result"]
end
def self.playlists(name=nil)
results = get("/playlists")
raise results.inspect if results["status"] != "OK"
if name.nil?
results["result"]
else
# puts results.inspect
p = results["result"]["playlists"].select{|e| e["name"].downcase == name.downcase}
return nil if p.empty?
return p
end
end
def self.update_playlist(id, tracks=nil, name=nil)
puts "replacing tracks in #{id}"
data = {}
data["tracks"] = tracks unless tracks.nil?
data["name"] = name unless name.nil?
resp = put("/playlists/#{id}", :body => data.to_json)
puts resp.inspect
end
def self.create_playlist(name)
puts "creating a new playlist"
# this post throws error with httparty - must investigate
resp = RestClient.post("http://#{base_uri}/playlists", :name => name)
if resp.code == 201
location = resp.headers[:location]
puts "201 created (#{location})"
spotify_id = location[location.rindex('/')+1..-1]
return spotify_id
else
raise resp.to_s
end
end
end