Class: Niconico::Video

Inherits:
Fabric
  • Object
show all
Includes:
Helper
Defined in:
lib/nv/niconico/video.rb

Instance Attribute Summary

Attributes inherited from Fabric

#agent

Instance Method Summary collapse

Methods included from Helper

#mylist?

Methods inherited from Fabric

#sign_in, #signed_in?

Constructor Details

#initialize(ptr, agent = nil) ⇒ Video

Returns a new instance of Video.



5
6
7
8
9
10
11
# File 'lib/nv/niconico/video.rb', line 5

def initialize(ptr, agent=nil)
  super(agent)

  @thumb = nil
  @flv   = nil
  @id    = normalize(ptr) # pvid | thumb_cached
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Combined parameter fetcher



43
44
45
# File 'lib/nv/niconico/video.rb', line 43

def method_missing(method, *args)
  thumb[method] || flv[method] || raise(NoMethodError, method)
end

Instance Method Details

#download(output = ".") ⇒ Object



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
# File 'lib/nv/niconico/video.rb', line 13

def download(output=".")
  escapedTitle = thumb.title.gsub(/\//, "")
  filename = "#{escapedTitle} - [#{thumb.video_id}].#{thumb.extension}"
  filepath = File.join(output, filename)

  Dir.mkdir(output) unless Dir.exist? output

  progress_bar = nil
  File.open(filepath, 'wb') do |fp|
    open(flv.url, 'rb',
      'Cookie' => flv.history_cookies,
      :content_length_proc => lambda{ |content_length|
        if content_length
          progress_bar = ProgressBar.create(:total => content_length)
        end
      },
      :progress_proc => lambda{ |transferred_bytes|
        if progress_bar
          progress_bar.progress = transferred_bytes
        else
          puts "#{transferred_bytes} / Total size is unknown"
        end
      }
    ) do |f|
      fp.print f.read
    end
  end
end