nicovideo 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/nicovideo/thumbnail.rb +49 -0
- data/lib/nicovideo/version.rb +1 -1
- metadata +3 -2
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'timeout'
|
3
|
+
require 'rexml/document'
|
4
|
+
|
5
|
+
module Nicovideo
|
6
|
+
class Thumbnail
|
7
|
+
def initialize(proxy_url = nil)
|
8
|
+
@proxy_url = proxy_url
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(video_id, wait_sec = 10, retry_max = 2)
|
12
|
+
root = get_response(video_id, wait_sec, retry_max)
|
13
|
+
|
14
|
+
get_elements(root.elements["thumb"])
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_response(video_id, wait_sec, retry_max)
|
18
|
+
retry_count = 0
|
19
|
+
begin
|
20
|
+
body = timeout(wait_sec) do
|
21
|
+
open("http://www.nicovideo.jp/api/getthumbinfo/#{video_id}", :proxy => @proxy_url) do |f|
|
22
|
+
f.read
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
root = REXML::Document.new(body).root
|
27
|
+
raise ::Errno::ENOENT::new(video_id) unless root.attributes.get_attribute('status').value == 'ok'
|
28
|
+
root
|
29
|
+
rescue TimeoutError => e
|
30
|
+
raise e if retry_count >= retry_max
|
31
|
+
retry_count += 1
|
32
|
+
retry
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_elements(parent)
|
37
|
+
thumbnail_info = {}
|
38
|
+
|
39
|
+
parent.each_element do |element|
|
40
|
+
if element.has_elements? then
|
41
|
+
thumbnail_info[element.name] = element.texts # doesn't support recursive xml.
|
42
|
+
next
|
43
|
+
end
|
44
|
+
thumbnail_info[element.name] = element.text
|
45
|
+
end
|
46
|
+
thumbnail_info
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/nicovideo/version.rb
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: nicovideo
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2008-07-
|
6
|
+
version: 0.1.6
|
7
|
+
date: 2008-07-23 00:00:00 +09:00
|
8
8
|
summary: utils for nicovideo
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- lib/nicovideo/ranking.rb
|
47
47
|
- lib/nicovideo/search.rb
|
48
48
|
- lib/nicovideo/tagsearch.rb
|
49
|
+
- lib/nicovideo/thumbnail.rb
|
49
50
|
- lib/nicovideo/newarrival.rb
|
50
51
|
- lib/nicovideo/random.rb
|
51
52
|
- test/test_helper.rb
|