Class: Nicovideo::Thumbnail

Inherits:
Object
  • Object
show all
Defined in:
lib/nicovideo/thumbnail.rb

Instance Method Summary collapse

Constructor Details

#initialize(proxy_url = nil) ⇒ Thumbnail

Returns a new instance of Thumbnail.



7
8
9
# File 'lib/nicovideo/thumbnail.rb', line 7

def initialize(proxy_url = nil)
  @proxy_url =  proxy_url
end

Instance Method Details

#get(video_id, wait_sec = 10, retry_max = 2) ⇒ Object



11
12
13
14
15
# File 'lib/nicovideo/thumbnail.rb', line 11

def get(video_id, wait_sec = 10, retry_max = 2)
	root = get_response(video_id, wait_sec, retry_max)
	
	get_elements(root.elements["thumb"])
end

#get_elements(parent) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nicovideo/thumbnail.rb', line 36

def get_elements(parent)
	thumbnail_info = ThumbInfo.new

	parent.each_element do |element|
		if element.name == 'tags' then
        thumbnail_info.tags[element.attributes['domain']] = []
        element.each_element do |child|
          thumbnail_info.tags[element.attributes['domain']] << child.text
        end
			next
	  end
		thumbnail_info[element.name] = element.text
	end
		thumbnail_info
end

#get_response(video_id, wait_sec, retry_max) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nicovideo/thumbnail.rb', line 17

def get_response(video_id, wait_sec, retry_max)
	retry_count = 0
    begin
      body = timeout(wait_sec) do
        open("http://ext.nicovideo.jp/api/getthumbinfo/#{video_id}", :proxy => @proxy_url) do |f|
          f.read
        end
      end

      root = REXML::Document.new(body).root
      raise ::Errno::ENOENT::new(video_id) unless root.attributes.get_attribute('status').value == 'ok'
      root
    rescue TimeoutError => e
      raise e if retry_count >= retry_max
      retry_count += 1
      retry
    end
end