Truveo 1.0.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/bin/truveo_example.rb +50 -0
- data/lib/truveo.rb +498 -0
- data/test/test_api.rb +46 -0
- metadata +48 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Created by Adam Beguelin on 2007-04-23.
|
4
|
+
# Copyright (c) 2007. All rights reserved.
|
5
|
+
|
6
|
+
# simple example program using Truveo API
|
7
|
+
|
8
|
+
require 'truveo'
|
9
|
+
|
10
|
+
begin
|
11
|
+
|
12
|
+
t = Truveo.new('5jtbaln8c0m6um6pp') # <----- use your own appid here
|
13
|
+
|
14
|
+
# get_videos(query='', results=10, start=0, showRelatedItems=0,
|
15
|
+
# tagResults=10, channelResults=10, categoryResults=10, userResults=10,
|
16
|
+
# showAdult=0)
|
17
|
+
|
18
|
+
res = t.get_videos("funny")
|
19
|
+
|
20
|
+
puts "number of videos in index: " + res.total_results_available
|
21
|
+
puts "number of results returned from query: " + res.total_results_returned
|
22
|
+
|
23
|
+
# video_set is an array of videos in result set
|
24
|
+
puts "top videos"
|
25
|
+
res.video_set.each{|v|
|
26
|
+
puts "\t #{v['id']}: \t #{v['title']}"
|
27
|
+
}
|
28
|
+
|
29
|
+
puts "query '#{res.query}'"
|
30
|
+
|
31
|
+
puts "video[0] metadata"
|
32
|
+
res.video_set[0].each_pair { |key,value| puts "#{key}:\n\t#{value}\n"}
|
33
|
+
|
34
|
+
puts "\nTags for Madonna:"
|
35
|
+
res = t.get_related_tags(query = 'madonna', results = 10, start = 0)
|
36
|
+
res.tag_set.each_pair { |tag, val| puts "#{tag}: #{val}" }
|
37
|
+
|
38
|
+
puts "\nChannels for Madonna:"
|
39
|
+
res = t.get_related_channels(query = 'madonna', results = 10, start = 0)
|
40
|
+
res.channel_set.each_pair { |tag, val| puts "#{tag}: #{val}" }
|
41
|
+
|
42
|
+
puts "\nCategories for Madonna:"
|
43
|
+
res = t.get_related_categories(query = 'madonna', results = 10, start = 0)
|
44
|
+
res.category_set.each_pair { |tag, val| puts "#{tag}: #{val}" }
|
45
|
+
|
46
|
+
puts "\nUsers for Madonna:"
|
47
|
+
res = t.get_related_users(query = 'madonna', results = 10, start = 0)
|
48
|
+
res.user_set.each_pair { |tag, val| puts "#{tag}: #{val}" }
|
49
|
+
|
50
|
+
end
|
data/lib/truveo.rb
ADDED
@@ -0,0 +1,498 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# Created by Adam Beguelin on 2007-03-27.
|
5
|
+
# Truveo Video Search Ruby API version 3.
|
6
|
+
# Copyright (c) 2007. AOL LLC.
|
7
|
+
# All rights reserved.
|
8
|
+
#++
|
9
|
+
|
10
|
+
$VERBOSE = 0
|
11
|
+
|
12
|
+
require "net/http"
|
13
|
+
require "uri"
|
14
|
+
require "rexml/document"
|
15
|
+
include REXML
|
16
|
+
|
17
|
+
=begin rdoc
|
18
|
+
TruveoResponse objects are returned from Truveo methods get_videos(), get_related_categories(), get_related_channels(),
|
19
|
+
get_related_tags(), and get_related_users(). For example, the following line of code creates a new TruveoResponse object
|
20
|
+
as the result of a call to Truveo.get_videos().
|
21
|
+
|
22
|
+
res = t.get_videos("funny")
|
23
|
+
|
24
|
+
The video_set attribute is an array of the videos returned by the TruveoResponse.get_videos call.
|
25
|
+
|
26
|
+
res.video_set.each{|v| ... } # iterates through the videos, each video is a hash of the metadata for that video
|
27
|
+
|
28
|
+
The channel_set is a hash of the channels that match the query. The key is the name of the channel. The value is
|
29
|
+
the count of the number of videos in that channel that match the query.
|
30
|
+
|
31
|
+
res.channel_set.each_pair{|key,val| ... }
|
32
|
+
|
33
|
+
The tag_set, cateogry_set, and user_set members are similar to the channel_set member described above.
|
34
|
+
|
35
|
+
The TruveoResponse also implements the <tt>each</tt> method which supports iteration through the all the videos that can be returned by the get_videos()
|
36
|
+
query that created the TruveoResponse, up to 1,000 videos.
|
37
|
+
|
38
|
+
res = t.get_videos("funny")
|
39
|
+
res.each{|v| puts v['title']}
|
40
|
+
|
41
|
+
=end
|
42
|
+
|
43
|
+
class TruveoResponse
|
44
|
+
# Array of videos returned by the query.
|
45
|
+
# res.video_set # <-- array of the videos that matched query
|
46
|
+
attr :video_set, true
|
47
|
+
# Hash of channels and their related counts for your query
|
48
|
+
# res.channel_set # <-- hash of the matching channels
|
49
|
+
attr :channel_set, true
|
50
|
+
# Hash of categories and their related counts for your query
|
51
|
+
# res.category_set # <-- hash of the matching categories
|
52
|
+
attr :category_set, true
|
53
|
+
# Hash of tags and their related counts for your query
|
54
|
+
# res.tag_set # <-- hash of the matching tags
|
55
|
+
attr :tag_set, true
|
56
|
+
# Hash of users and their related counts for your query
|
57
|
+
# res.user_set # <-- hash of the matching users
|
58
|
+
attr :user_set, true
|
59
|
+
|
60
|
+
# String containing method, i.e., 'truveo.videos.getVideos'
|
61
|
+
# res.method
|
62
|
+
attr :method, true
|
63
|
+
# String containing the query used to create this response object
|
64
|
+
# res.query
|
65
|
+
attr :query, true
|
66
|
+
# String containging the sorter used to create this response
|
67
|
+
# res.sortby
|
68
|
+
attr :sortby, true
|
69
|
+
|
70
|
+
# String containing the query suggestion, if any
|
71
|
+
# res.query_suggestion
|
72
|
+
attr :query_suggestion, true
|
73
|
+
# String indicating the number of total results that matched the query
|
74
|
+
# res.total_results_available
|
75
|
+
attr :total_results_available, true
|
76
|
+
# String indicating the number of resutls returned in this result set. For get_videos() the following should be true: video_set.length == total_resutls_returned.to_i)
|
77
|
+
# res.total_results_returned
|
78
|
+
attr :total_results_returned, true
|
79
|
+
# String representing the position of the first Video in the entire set of matching videos.
|
80
|
+
# res.first_result_position
|
81
|
+
attr :first_result_position, true
|
82
|
+
# String containing the URL which will return an RSS feed for the set of videos returned in response to the submitted query.
|
83
|
+
# res.rss_url
|
84
|
+
attr :rss_url, true
|
85
|
+
# String containing a human-readable title for the set of videos returned in response to the submitted request.
|
86
|
+
# For example, this field would return a string such as "Most popular 'madonna' videos in Music on MTV" for the query 'madonna category:Music channel:MTV sort:mostPopular'.
|
87
|
+
# res.video_set_title
|
88
|
+
attr :video_set_title, true
|
89
|
+
|
90
|
+
# String indicating the number of channel results returned
|
91
|
+
# res.channel_set.length == res.channel_results_returned.to_i # <-- true
|
92
|
+
attr :channel_results_returned, true
|
93
|
+
# String indicating the number of category results returned
|
94
|
+
# res.category_set.length == res.cagegory_results_returned.to_i # <-- true
|
95
|
+
attr :category_results_returned, true
|
96
|
+
# String indicating the number of tag results returned
|
97
|
+
# res.tag_set.length == res.tag_results_returned.to_i # <-- true
|
98
|
+
attr :tag_results_returned, true
|
99
|
+
# String indicating the number of user results returned
|
100
|
+
# res.user_set.length == res.user_results_returned.to_i # <-- true
|
101
|
+
attr :user_results_returned, true
|
102
|
+
|
103
|
+
# String indicating the integer code for the error if one occured
|
104
|
+
attr :error_code, true
|
105
|
+
# String containing text of error code if one occured.
|
106
|
+
attr :error_text, true
|
107
|
+
|
108
|
+
# :stopdoc:
|
109
|
+
|
110
|
+
def initialize
|
111
|
+
@vidlist = Array.new
|
112
|
+
end
|
113
|
+
|
114
|
+
attr :sphinxquery, true # sqphinx query used
|
115
|
+
attr :sphinxfilters, true # sqphinx filters used
|
116
|
+
|
117
|
+
attr :params, true # parameters sent to truveo api call when TruveoResponse was created
|
118
|
+
attr :truveo, true # truveo object used to create the results, used for next_video
|
119
|
+
|
120
|
+
# copy over state for next_video call
|
121
|
+
def next_self(res)
|
122
|
+
self.video_set = res.video_set
|
123
|
+
self.channel_set = res.channel_set
|
124
|
+
self.category_set = res.category_set
|
125
|
+
self.tag_set = res.tag_set
|
126
|
+
self.user_set = res.user_set
|
127
|
+
|
128
|
+
self.query_suggestion = res.query_suggestion
|
129
|
+
self.total_results_available = res.total_results_available
|
130
|
+
self.total_results_returned = res.total_results_returned
|
131
|
+
self.first_result_position = res.first_result_position
|
132
|
+
self.rss_url = res.rss_url
|
133
|
+
self.video_set_title = res.video_set_title
|
134
|
+
|
135
|
+
self.channel_results_returned = res.channel_results_returned
|
136
|
+
self.category_results_returned = res.category_results_returned
|
137
|
+
self.tag_results_returned = res.tag_results_returned
|
138
|
+
self.user_results_returned = res.user_results_returned
|
139
|
+
|
140
|
+
self.sphinxquery = res.sphinxquery
|
141
|
+
self.sphinxfilters = res.sphinxfilters
|
142
|
+
end
|
143
|
+
|
144
|
+
# :startdoc:
|
145
|
+
|
146
|
+
=begin rdoc
|
147
|
+
|
148
|
+
Iterate through all the videos in the response. Each video is a hash where the key is the metadata field, like title,
|
149
|
+
and the value is the actual metadata. The videos are returned in whatever order was specified by the sorter, if any,
|
150
|
+
used in the query that created the TruveoResponse object.
|
151
|
+
|
152
|
+
The following goes through all the videos that match the query and prints the title. If more than one thousand
|
153
|
+
videos match the query, the each method will only iterate through the first thousand.
|
154
|
+
|
155
|
+
# create a Truveo object with my app id (apply for a free app id at http://developer.truveo.com/)
|
156
|
+
t = Truveo.new("appid")
|
157
|
+
res = t.get_videos("funny")
|
158
|
+
|
159
|
+
# print lots of titles
|
160
|
+
res.each{|vid| puts vid['title']}
|
161
|
+
|
162
|
+
Note that the each method will invoke another get_videos() method behind the scenes. These calls will count against your
|
163
|
+
daily limit. This means iterating through a thousand results using <tt>each</tt> will result in 100 calls to <tt>get_videos()</tt>
|
164
|
+
by default.
|
165
|
+
|
166
|
+
=end rdoc
|
167
|
+
|
168
|
+
def each # :yields: video
|
169
|
+
# if we've stored the results so fare
|
170
|
+
@vidlist.each { |v| yield v }
|
171
|
+
# get any more videos, storing the results for later calls to self.each
|
172
|
+
while v = next_video do
|
173
|
+
@vidlist << v
|
174
|
+
yield v
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
# :stopdoc:
|
179
|
+
|
180
|
+
# get the next video, assumes a query has already been made, @video_set, @params, and @next_start have been set in get_videos_hash()
|
181
|
+
def next_video
|
182
|
+
return nil if video_set.nil?
|
183
|
+
|
184
|
+
# get more video if video_set has been depleted
|
185
|
+
if video_set.length < 1
|
186
|
+
next_start = first_result_position.to_i + total_results_returned.to_i
|
187
|
+
# check for valid start, we never return more than 1000 results
|
188
|
+
return nil if next_start >= 1000
|
189
|
+
# try to get another video_set
|
190
|
+
self.params[:start] = next_start.to_s
|
191
|
+
res = truveo.get_videos_hash(params)
|
192
|
+
next_self(res)
|
193
|
+
return nil if video_set.nil? || video_set.length < 1
|
194
|
+
end
|
195
|
+
|
196
|
+
video_set.shift
|
197
|
+
end
|
198
|
+
# :startdoc:
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
=begin rdoc
|
203
|
+
|
204
|
+
The Truveo class implements a Ruby version of the Truveo API (see the {Truveo Developer Site}[http://developer.truveo.com] for details).
|
205
|
+
A Truveo object is initialized with an developer id, which is free and can be obtained from
|
206
|
+
the {Truveo Developer Site}[http://developer.truveo.com]. A developer id will allow you to call the Truveo API
|
207
|
+
up to 10,000 times per day.
|
208
|
+
|
209
|
+
Currently user functions, like ratings, are not implemented in the Ruby API.
|
210
|
+
|
211
|
+
=end
|
212
|
+
|
213
|
+
class Truveo
|
214
|
+
|
215
|
+
@@sorter = %w(sort:mostPopular sort:mostPopularNow sort:mostPopularThisWeek sort:mostPopularThisMonth sort:vrank sort:mostRecent sort:mostRelevant sort:topFavorites sort:highestRated)
|
216
|
+
@@filter = %w(days_old: bitrate: type:free type:reg type:sub type:rent type:buy runtime: quality:poor quality:fair quality:good quality:excellent format:win format:real format:qt format:flash format:hi-q site: file_size:) # XXXX add these, should there be two types? One that takes a comparison and one that doesn't?
|
217
|
+
@@modifier = %w(category: channel: tag: user: id: sim: title: description: artist: album: show: actor: director: writer: producer: distributor:)
|
218
|
+
|
219
|
+
# array of Truveo supported sorters such as <tt>sort:vrank</tt>
|
220
|
+
#
|
221
|
+
# Example:
|
222
|
+
# Truveo.sorter.each{|s| puts s } # print all the sorters
|
223
|
+
#
|
224
|
+
def Truveo.sorter
|
225
|
+
@@sorter
|
226
|
+
end
|
227
|
+
|
228
|
+
# array of Truveo supported filters such as <tt>type:free</tt>
|
229
|
+
#
|
230
|
+
# Example:
|
231
|
+
# Truveo.filter.each{|f| puts f } # print all the filters
|
232
|
+
#
|
233
|
+
def Truveo.filter
|
234
|
+
@@filter
|
235
|
+
end
|
236
|
+
|
237
|
+
# array of Truveo supported modifiers such as <tt>tag:</tt>
|
238
|
+
#
|
239
|
+
# Example:
|
240
|
+
# Truveo.modifier.each{|m| puts m } # print all the modifiers
|
241
|
+
#
|
242
|
+
def Truveo.modifier
|
243
|
+
@@modifier
|
244
|
+
end
|
245
|
+
|
246
|
+
# Create a new Truveo object for querying the Truveo video search engine.
|
247
|
+
# The appid is required. For your free appid go to {My API Account}[http://developer.searchvideo.com/APIMyAccount.php]
|
248
|
+
# where you easily sign up for your own appid, allowing you up to 10,000 Truveo queries per day.
|
249
|
+
# :call-seq: new(appid)
|
250
|
+
#
|
251
|
+
# Example:
|
252
|
+
# t = Truveo.new('my_appid') # create a new Truveo object with your app_id
|
253
|
+
#
|
254
|
+
def initialize(appid, site = 'xml.searchvideo.com', path = "/apiv3?", port=80)
|
255
|
+
@appid = appid
|
256
|
+
@site = site
|
257
|
+
@path = path
|
258
|
+
@port = port
|
259
|
+
end
|
260
|
+
|
261
|
+
# generic rest call
|
262
|
+
def rest(parms) # :nodoc:
|
263
|
+
call = String.new(@path)
|
264
|
+
parms.each{|k,v| call << "#{k}=#{v}&"}
|
265
|
+
call.chop!
|
266
|
+
call = URI.escape(call)
|
267
|
+
api_site = Net::HTTP.new(@site,@port)
|
268
|
+
# puts call
|
269
|
+
xml = REXML::Document.new(body = api_site.request_get(call).body)
|
270
|
+
return xml
|
271
|
+
rescue REXML::ParseException => e
|
272
|
+
puts "rest parse rescue: " # + e.to_s
|
273
|
+
puts "zzz>>> #{body}<<<zzz"
|
274
|
+
return nil
|
275
|
+
rescue Exception => e
|
276
|
+
puts "rest rescue: " + e.to_s.split("\n")[0]
|
277
|
+
pp e
|
278
|
+
puts "site, path, parms: #{@site}, #{@path}, :#{call}:"
|
279
|
+
return nil
|
280
|
+
end
|
281
|
+
|
282
|
+
# return the error as hash or nil if no error
|
283
|
+
def api_error(xml) #:nodoc:
|
284
|
+
if xml.nil?
|
285
|
+
res = TruveoResponse.new
|
286
|
+
res.error_code = '69'
|
287
|
+
res.error_text = 'bad xml'
|
288
|
+
return res
|
289
|
+
end
|
290
|
+
#<?xml version='1.0' encoding='UTF-8'?><Response><Error Code='14'>Access Denied: invalid appid.</Error></Response>
|
291
|
+
if elt = xml.elements['//Error']
|
292
|
+
res = TruveoResponse.new
|
293
|
+
res.error_code = elt.attributes["Code"]
|
294
|
+
res.error_text = elt.text
|
295
|
+
return res
|
296
|
+
end
|
297
|
+
nil
|
298
|
+
end
|
299
|
+
|
300
|
+
# convert an xml element to a string
|
301
|
+
def elt_text(xml,s) #:nodoc:
|
302
|
+
if elt = xml.elements[s]
|
303
|
+
return elt.text
|
304
|
+
end
|
305
|
+
return nil
|
306
|
+
end
|
307
|
+
|
308
|
+
# convert xml to hash
|
309
|
+
def xml_to_hash(xml) #:nodoc:
|
310
|
+
h = Hash.new
|
311
|
+
xml.elements.each{|e| h[e.name] = e.text }
|
312
|
+
h
|
313
|
+
end
|
314
|
+
|
315
|
+
# performs the specified query and returns the results in the form of a TruveoResponse object.
|
316
|
+
# query:: this is the search query to run. See {Building Search Queries}[http://developer.truveo.com/SearchQueryOverview.php] for details.
|
317
|
+
# results:: number of results to return, the default is 10, the maximum is 50.
|
318
|
+
# start:: where to start the result set being returned, defaults to 0. Can be used to page through results.
|
319
|
+
# showRelatedItems:: indicates if the tag set, category set, and user set should be returned. Values should be 0 or 1, the default is 0.
|
320
|
+
# tagResults:: number of tags requested in the tag_set response.
|
321
|
+
# channelResults:: number of channels requested in the channel_set response.
|
322
|
+
# categoryResults:: number of categories requested in the category_set response.
|
323
|
+
# userResults:: number of users requested in the user_set response.
|
324
|
+
# showAdult:: flag indicating whether or not adult content should be included in the result set.
|
325
|
+
# All of the result sizes above are maximums. A query may return fewer than the requested number of results if the requested
|
326
|
+
# number of results don't match.
|
327
|
+
#
|
328
|
+
# If the query string is empty, then the top results are returned in vrank order.
|
329
|
+
#
|
330
|
+
# If showRelatedItems is zero, the response will only includea a video_set. The tag_set, channel_set, category_set, and user_set
|
331
|
+
# parameters will be left out.
|
332
|
+
|
333
|
+
def get_videos(query='', results=10, start=0, showRelatedItems=0, tagResults=10, channelResults=10, categoryResults=10, userResults=10, showAdult=0)
|
334
|
+
params = Hash.new
|
335
|
+
params[:method] = 'truveo.videos.getVideos'
|
336
|
+
params[:query] = query
|
337
|
+
params[:showRelatedItems] = showRelatedItems
|
338
|
+
params[:tagResults] = tagResults
|
339
|
+
params[:channelResults] = channelResults
|
340
|
+
params[:categoryResults] = categoryResults
|
341
|
+
params[:userResults] = userResults
|
342
|
+
params[:showAdult] = showAdult
|
343
|
+
params[:appid] = @appid
|
344
|
+
params[:results] = results
|
345
|
+
params[:start] = start
|
346
|
+
|
347
|
+
get_videos_hash(params)
|
348
|
+
|
349
|
+
end
|
350
|
+
|
351
|
+
def get_videos_hash(params) #:nodoc:
|
352
|
+
|
353
|
+
|
354
|
+
xml = rest(params)
|
355
|
+
|
356
|
+
res = TruveoResponse.new
|
357
|
+
|
358
|
+
# save params for next_video call
|
359
|
+
res.params = params
|
360
|
+
res.truveo = self
|
361
|
+
|
362
|
+
# check for error codes
|
363
|
+
err = api_error(xml)
|
364
|
+
puts "get_videos(#{params[:query]}) returned error: #{err.error_code} #{err.error_text}" if $VERBOSE && !err.nil?
|
365
|
+
return err if !err.nil?
|
366
|
+
|
367
|
+
res.method = elt_text(xml,'//Response/method')
|
368
|
+
res.query = elt_text(xml,'//Response/query')
|
369
|
+
res.sortby = elt_text(xml,'//Response/sortby')
|
370
|
+
res.query_suggestion = elt_text(xml,'//Response/querySuggestion')
|
371
|
+
|
372
|
+
res.total_results_available = elt_text(xml, '//VideoSet/totalResultsAvailable')
|
373
|
+
res.total_results_returned = elt_text(xml, '//VideoSet/totalResultsReturned')
|
374
|
+
res.first_result_position = elt_text(xml, '//VideoSet/firstResultPosition')
|
375
|
+
|
376
|
+
res.rss_url = elt_text(xml, '//rssUrl')
|
377
|
+
res.video_set_title = elt_text(xml, '//VideoSet/title')
|
378
|
+
|
379
|
+
res.sphinxquery = elt_text(xml,'//Response/sphinxquery')
|
380
|
+
res.sphinxfilters = elt_text(xml,'//Response/sphinxfilters')
|
381
|
+
|
382
|
+
# store the video set
|
383
|
+
res.video_set = Array.new
|
384
|
+
@video_set = Array.new
|
385
|
+
xml.elements.each('//Video') {|v|
|
386
|
+
res.video_set << (tvid = xml_to_hash(v))
|
387
|
+
@video_set << tvid
|
388
|
+
}
|
389
|
+
|
390
|
+
@next_start = res.total_results_returned.to_i + res.first_result_position.to_i
|
391
|
+
|
392
|
+
if res.total_results_returned.to_i != res.video_set.length
|
393
|
+
puts "Warning: results mismatch: res.total_results_returned (#{res.total_results_returned}) != res.video_set.length (#{res.video_set.length})"
|
394
|
+
# puts xml.to_s
|
395
|
+
end
|
396
|
+
|
397
|
+
# the channel_set is a hash, one entry per channel. the key is the channel name, the value is the count.
|
398
|
+
res.channel_results_returned = elt_text(xml, '//ChannelSet/totalResultsReturned')
|
399
|
+
res.channel_set = set_hash(xml,'//Channel')
|
400
|
+
|
401
|
+
res.tag_results_returned = elt_text(xml, '//TagSet/totalResultsReturned')
|
402
|
+
res.tag_set = set_hash(xml,'//Tag')
|
403
|
+
|
404
|
+
res.category_results_returned = elt_text(xml, '//CategorySet/totalResultsReturned')
|
405
|
+
res.category_set = set_hash(xml, '//Category')
|
406
|
+
|
407
|
+
res.user_results_returned = elt_text(xml, '//UserSet/totalResultsReturned')
|
408
|
+
res.user_set = set_hash(xml, '//User')
|
409
|
+
|
410
|
+
res
|
411
|
+
|
412
|
+
end
|
413
|
+
|
414
|
+
# return the hash for the given set (category, channel, tag, or user)
|
415
|
+
def set_hash(xml,s) #:nodoc:
|
416
|
+
h = Hash.new
|
417
|
+
xml.elements.each(s){|v| h[v.elements['name'].text] = v.elements['count'].text.to_i}
|
418
|
+
h
|
419
|
+
end
|
420
|
+
|
421
|
+
|
422
|
+
# generic get_related for each of the get_related type calls
|
423
|
+
def get_related(type='Tags', query='', results=10, start=0) #:nodoc:
|
424
|
+
|
425
|
+
params = Hash.new
|
426
|
+
params[:method] = "truveo.videos.getRelated#{type}"
|
427
|
+
params[:query] = query
|
428
|
+
params[:appid] = @appid
|
429
|
+
params[:results] = results
|
430
|
+
params[:start] = start
|
431
|
+
|
432
|
+
xml = rest(params)
|
433
|
+
|
434
|
+
res = TruveoResponse.new
|
435
|
+
|
436
|
+
# check for error codes
|
437
|
+
err = api_error(xml)
|
438
|
+
return err if !err.nil?
|
439
|
+
|
440
|
+
res.method = elt_text(xml,'//Response/method')
|
441
|
+
res.query = elt_text(xml,'//Response/query')
|
442
|
+
res.sortby = elt_text(xml,'//Response/sortby')
|
443
|
+
res.query_suggestion = elt_text(xml,'//Response/querySuggestion')
|
444
|
+
|
445
|
+
res.sphinxquery = elt_text(xml,'//Response/sphinxquery')
|
446
|
+
res.sphinxfilters = elt_text(xml,'//Response/sphinxfilters')
|
447
|
+
|
448
|
+
res.total_results_returned = elt_text(xml, '//totalResultsReturned')
|
449
|
+
res.first_result_position = elt_text(xml, '//firstResultPosition')
|
450
|
+
|
451
|
+
res.send("#{singularize(type).downcase}_set=", set_hash(xml,"//#{singularize(type)}"))
|
452
|
+
|
453
|
+
res
|
454
|
+
|
455
|
+
end
|
456
|
+
|
457
|
+
# Return a hash of the tags and counts related to the query.
|
458
|
+
#
|
459
|
+
# The results and start parameters are used for paging through the result set.
|
460
|
+
#
|
461
|
+
# Example:
|
462
|
+
# t = Truveo.new('appid_goes_here')
|
463
|
+
# res = t.get_related_tags('funny')
|
464
|
+
# res.tag_set.each_pair{|k,v| puts "tag: #{k} count: #{v}" }
|
465
|
+
def get_related_tags(query='', results=10, start=0)
|
466
|
+
get_related('Tags', query, results, start)
|
467
|
+
end
|
468
|
+
|
469
|
+
# Return a hash of the channels and counts related to the query.
|
470
|
+
#
|
471
|
+
# The results and start parameters are used for paging through the result set.
|
472
|
+
def get_related_channels(query='', results=10, start=0)
|
473
|
+
get_related('Channels',query, results, start)
|
474
|
+
end
|
475
|
+
|
476
|
+
# Return a hash of the users and counts related to the query.
|
477
|
+
#
|
478
|
+
# The results and start parameters are used for paging through the result set.
|
479
|
+
def get_related_users(query='', results=10, start=0)
|
480
|
+
get_related('Users',query, results, start)
|
481
|
+
end
|
482
|
+
|
483
|
+
# Return a hash of the categories and counts related to the query.
|
484
|
+
#
|
485
|
+
# The results and start parameters are used for paging through the result set.
|
486
|
+
def get_related_categories(query='', results=10, start=0)
|
487
|
+
get_related('Categories',query, results, start)
|
488
|
+
end
|
489
|
+
|
490
|
+
def singularize(s) #:nodoc:
|
491
|
+
return s.chop if s =~ /(Tags|Channels|Users)/
|
492
|
+
return 'Category' if s =~ /Categories/
|
493
|
+
return s
|
494
|
+
end
|
495
|
+
|
496
|
+
private :singularize
|
497
|
+
|
498
|
+
end
|
data/test/test_api.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Created by Adam Beguelin on 2007-06-27.
|
4
|
+
# Copyright (c) 2007. All rights reserved.
|
5
|
+
#
|
6
|
+
# a few interesting tests just to sanity check things
|
7
|
+
# please add your appid to the Truveo.new call in setup()
|
8
|
+
|
9
|
+
require 'test/unit'
|
10
|
+
require 'truveo'
|
11
|
+
|
12
|
+
class TestTruveo < Test::Unit::TestCase
|
13
|
+
|
14
|
+
def setup
|
15
|
+
@t = Truveo.new('5jtbaln8c0m6um6pp') # <----- use your own appid here
|
16
|
+
# use the empty query with showRealtedItems on
|
17
|
+
@res = @t.get_videos(query='', results=50, start=0, showRelatedItems=1, tagResults=50, channelResults=50, categoryResults=50, userResults=50, showAdult=1)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_tag_hash
|
21
|
+
assert_equal('50', @res.tag_results_returned)
|
22
|
+
@res.tag_set.each{|k,v| assert(v>0, "Tag count < 1 for tag #{k} count #{v}")}
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_user_hash
|
26
|
+
assert_equal('50', @res.user_results_returned)
|
27
|
+
@res.user_set.each{|k,v| assert(v>0, "User count < 1 for user #{k} count #{v}")}
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_video_hash
|
31
|
+
assert_not_nil(@res.video_set)
|
32
|
+
assert_equal(50, @res.total_results_returned.to_i)
|
33
|
+
assert_equal(50, @res.video_set.length)
|
34
|
+
@res.video_set.each{|e| assert_not_nil(@res.video_set[0]['title']) }
|
35
|
+
@res.video_set.each{|e| assert_not_nil(@res.video_set[0]['description']) }
|
36
|
+
# vrank should be descending
|
37
|
+
rank = 1.0
|
38
|
+
@res.video_set.each{|e| assert(e['vRank'].to_f <= rank); rank = e['vRank'].to_f }
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_sorters
|
42
|
+
assert_equal(9,Truveo.sorter.size)
|
43
|
+
assert_equal(19,Truveo.filter.size)
|
44
|
+
assert_equal(16,Truveo.modifier.size)
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: Truveo
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2007-07-09 00:00:00 -07:00
|
8
|
+
summary: Ruby interface to Truveo
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: adam @nospam@ truveo.com
|
12
|
+
homepage: http://developer.truveo.com
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: truveo
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Adam Beguelin
|
31
|
+
files:
|
32
|
+
- lib/truveo.rb
|
33
|
+
- bin/truveo_example.rb
|
34
|
+
- test/test_api.rb
|
35
|
+
test_files:
|
36
|
+
- test/test_api.rb
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
dependencies: []
|
48
|
+
|