forked from tiegz/ruby-mtv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby-mtv.rb
186 lines (155 loc) · 5.43 KB
/
ruby-mtv.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
require 'rubygems'
require 'activesupport'
require 'open-uri'
module MTV
class Error < StandardError; end
class << self
attr_accessor :base_url, :host, :protocol
end
self.host = "api.mtvnservices.com/1"
self.protocol = "http"
self.base_url = "#{protocol}://#{host}"
class Base
def initialize(values={})
values.each { |k, v| send "#{k}=", v }
end
class << self
def request path
url = URI.escape "#{MTV.base_url}/#{path}"
puts "Requesting url #{url}"
open url
end
end
end
# This class represents a musical artist in MTV's database.
class Artist < Base
attr_accessor :name, :uid, :uri, :links, :updated
def initialize(values={})
super(values)
self.uid = uri.gsub(MTV.base_url + "/artist", '').delete '/' if uri
end
# artist = Artist.search('In Flames')[2]
# artist.browse => I Am Ghost, IB3, INXS, Ice Cube, ...
def browse
@browse ||= Artist.browse(uid.first)
end
# artist = Artist.search 'Frank Zappa'
# artist.browse => Amon Amarth, Amorphis, Arch Enemy, Arcturus, Borknagar, ...
def related
@related ||= Artist.related(self)
end
class << self
# Artist.browse 'x' => X, X-Clan, The X-Ecutioners, Xiren, Xscape, Xtreme, Xzibit
def browse(letter='a')
response = request "artist/browse/#{letter.to_s.first}/"
parse_many response
end
# Artist.find 'beck'
def find(name, options={})
return name if name.is_a? Artist
options.to_options!
name = options[:name] || name
response = request "artist/#{name}/"
parse_one response
end
# Artist.related 'qtip'
# Artist.related Artist.find('qtip')
def related(artist)
artist = find(artist)
response = request "artist/#{artist.uid}/related/"
parse_many response
end
# Artist.search 'beck', :max_results => 1, :start_index => 1
def search(term=nil, options={})
options.to_options!
params = {}
params[:'max-result'] = options[:max_result] || 1
params[:'start-index'] = options[:start_index]
term = options[:term] || term
params.reject! { |k,v| !v }
response = request "artist/search?#{term.to_query('term')}&#{params.to_param}"
parse_many response.read
end
protected
def parse_many(body)
entries = Hash.from_xml(body)['feed']['entry']
entries = [entries] unless entries.is_a? Array
entries.map { |entry|
instantiate entry
}.reject { |artist| artist.name.nil? || artist.name.empty? }
end
def parse_one(body)
entry = Hash.from_xml(body)['entry']
raise Error, "That artist not found!" if entry['author']['name'].nil? || entry['author']['name'].empty?
instantiate entry
end
def instantiate(entry={})
Artist.new(:name => (entry['author']['name'] rescue nil),
:uri => (entry['author']['uri'] rescue nil),
:links => (entry['link'].map { |link| link['href'] } rescue nil),
:updated => (entry['updated'].to_datetime rescue nil))
end
end
end
# This class represents a music video in MTV's database.
class Video < Base
attr_accessor :name, :uid, :uri, :links, :updated
def initialize(values={})
super(values)
# self.uid = uri.gsub(MTV.base_url + "/artist", '').delete '/' if uri
end
class << self
# Video.find 'beck'
def find(uid, options={})
return uid if uid.is_a? Artist
options.to_options!
uid = options[:uid] || uid
response = request "video/#{uid}/"
parse_one response
end
# http://api.mtvnservices.com/1/video/search/[parameters]
# Video.search 'beck', :max_results => 1, :start_index => 1
def search(term=nil, options={})
options.to_options!
params = {}
params[:'max-result'] = options[:max_result] || 1
params[:'start-index'] = options[:start_index]
term = options[:term] || term
params.reject! { |k,v| !v }
response = request "video/search?#{term.to_query('term')}&#{params.to_param}"
parse_many response.read
end
protected
def parse_many(body)
entries = Hash.from_xml(body)['feed']['entry']
entries = [entries] unless entries.is_a? Array
entries.map { |entry|
instantiate entry
}.reject { |video| video.name.nil? || video.name.empty? }
end
def parse_one(body)
entry = Hash.from_xml(body)['entry']
raise Error, "That artist not found!" if entry['author']['name'].nil? || entry['author']['name'].empty?
instantiate entry
end
def instantiate(entry={})
puts "the entry is #{entry.inspect}"
Video.new
# TODO finish Video stuff
# (:thumbnails => entry['thumbnail'],
# :player => entry['player'],
# :title => entry['title'].to_s,
# :published => entry['published'],
# :author => entry['author']['name'])
end
end
end
class Thumbnail
attr_accessor :url, :width, :height
def initialize(url, width, height)
self.url = url
self.width = width
self.height = height
end
end
end