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

#escape_string, #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



129
130
131
# File 'lib/nv/niconico/video.rb', line 129

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

def download(output=".")
  escaped_title  = escape_string(thumb.title)
  escaped_output = escape_string(output)

  filename = sprintf OUTPUT_NAME, {:title => escaped_title, :id => thumb.video_id, :extension => thumb.extension}
  filepath = File.join(escaped_output, filename)
  filepath_nvdownload = "#{filepath}.nvdownload"

  # Return when video file is already exist
  if File.exist? filepath
    File.delete filepath_nvdownload if File.exist? filepath_nvdownload
    return
  end

  # Create output dir
  Dir.mkdir escaped_output unless Dir.exist? escaped_output

  # Define request headers
  options = {
    'Cookie' => flv.history_cookies
  }
  if File.exist? filepath_nvdownload
    options['Range'] = "bytes=#{File.size(filepath_nvdownload)}-"
  end

  # Download video
  progress_bar = nil
  url = URI.parse(flv.url)
  begin
    Net::HTTP.start(url.host, url.port) do |http|
      header = http.request_head("#{url.path}?#{url.query}", options)
      progress_bar = ProgressBar.create(:total => header['content-length'].to_i)
      transferred_bytes = 0
      request = Net::HTTP::Get.new(url, options)
      http.request request do |response|
        open(filepath_nvdownload, 'ab') do |io|
          response.read_body do |chunk|
            transferred_bytes += chunk.size
            if progress_bar
              progress_bar.progress = transferred_bytes
            else
              puts "#{transferred_bytes} / Total size is unknown"
            end

            io.write chunk
          end
        end
      end
    end
  rescue => e
    puts "Failed download: #{e}"
    return
  end

  # Rename .nvdownload to real file
  File.rename(filepath_nvdownload, filepath)
end

#download_comments(output = ".") ⇒ Object

POST msg.nicovideo.jp/53/api/ <packet><thread thread=“1345476375” version=“20090904” user_id=“1501297” scores=“1” nicoru=“1” with_global=“1”/><thread_leaves thread=“1345476375” user_id=“1501297” scores=“1” nicoru=“1”>0-14:100,1000</thread_leaves></packet> <packet>

<thread thread="1345476375"
        version="20090904"
        waybackkey="1417346808.E9d0LUF9gvFvt3Rrf5TP91Pa0LA"
        when="1417346804"
        user_id="1501297"
        scores="1"
        nicoru="1"
/>
<thread_leaves
        thread="1345476375"
        waybackkey="1417346808.E9d0LUF9gvFvt3Rrf5TP91Pa0LA"
        when="1417346804" # 起点
        user_id="1501297"
        res_before="8541"
        scores="1"
        nicoru="1" >0-14:100,1000</thread_leaves>

</packet> <chat thread=“1345476375” no=“8540” vpos=“55658” date=“1417346426” mail=“184” user_id=“tzaiW5hp-SvJG6UGkEa0kELQd3w” anonymity=“1” leaf=“9”>白いレースのハンカチかな?</chat> <chat thread=“1345476375” no=“8539” vpos=“41534” date=“1417233687” mail=“184” user_id=“1p_4U9fr-YvliRaUl3E6XGEavp4” premium=“1” anonymity=“1” leaf=“6”>くっそwwww</chat>



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

def download_comments(output=".")
  escaped_title  = escape_string(thumb.title)
  escaped_output = escape_string(output)

  filename = sprintf OUTPUT_NAME, {:title => escaped_title, :id => thumb.video_id, :extension => 'comments'}
  filepath = File.join(escaped_output, filename)

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

  url = URI.parse(flv.ms)

  thread_id = flv.thread_id
  length = (flv.l.to_i / 60).round
  res = Net::HTTP.new(url.host).start do |http|
    req = Net::HTTP::Post.new(url.path, {'Cookie' => flv.history_cookies})
    req.body = %|<packet><thread thread="#{thread_id}" version="20090904" scores="1" nicoru="1" with_global="1"/><thread_leaves thread="#{thread_id}" scores="1" nicoru="1">0-#{length}:10</thread_leaves></packet>|
    http.request(req)
  end

  open(filepath, 'w') do |f|
    f.write res.body
  end

  # TODO: Comment parser
  # doc = REXML::Document.new(res.body)
  # chats = doc.elements.to_a('/packet/chat')
  # chats.each do |chat|
  #   puts chat.attribute('vpos')
  #   puts chat.text
  # end
end