Class: Niconico::Video

Inherits:
Object
  • Object
show all
Includes:
Deferrable
Defined in:
lib/niconico/video.rb

Defined Under Namespace

Classes: NotFound, VideoUnavailableError

Instance Method Summary collapse

Methods included from Deferrable

#fetched?, included

Constructor Details

#initialize(parent, video_id, defer = nil) ⇒ Video

Returns a new instance of Video.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/niconico/video.rb', line 19

def initialize(parent, video_id, defer=nil)
  @parent = parent
  @agent = parent.agent
  @fetched = false
  @thread_id = @id = video_id
  @page = nil
  @url = "#{Niconico::URL[:watch]}#{@id}"

  if defer
    preload_deffered_values(defer)
  else
    get()
  end
end

Instance Method Details

#add_to_mylist(mylist_id, description = '') ⇒ Object



132
133
134
# File 'lib/niconico/video.rb', line 132

def add_to_mylist(mylist_id, description='')
  @parent.nico_api.mylist_add(mylist_id, :video, @id, description)
end

#available?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/niconico/video.rb', line 83

def available?
  !!video_url
end

#economy?Boolean

Returns:

  • (Boolean)


34
# File 'lib/niconico/video.rb', line 34

def economy?; @eco; end

#get(options = {}) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/niconico/video.rb', line 36

def get(options = {})
  begin
    @page = @agent.get(@url)
  rescue Mechanize::ResponseCodeError => e
    raise NotFound, "#{@id} not found" if e.message == "404 => Net::HTTPNotFound"
    raise e
  end

  if /^so/ =~ @id
    sleep 5
    @thread_id = @agent.get("#{Niconico::URL[:watch]}#{@id}").uri.path.sub(/^\/watch\//,"")
  end
  additional_params = nil
  if /^nm/ === @id && (!options.key?(:as3) || options[:as3])
    additional_params = "&as3=1"
  end
  getflv = Hash[@agent.get_file("#{Niconico::URL[:getflv]}?v=#{@thread_id}#{additional_params}").scan(/([^&]+)=([^&]+)/).map{|(k,v)| [k.to_sym,CGI.unescape(v)] }]

  if api_data_node = @page.at("#watchAPIDataContainer")
    @api_data = JSON.parse(api_data_node.text())
    video_detail = @api_data["videoDetail"]
    @title ||= video_detail["title"] if video_detail["title"]
    @description ||= video_detail["description"] if video_detail["description"]
    @tags  ||= video_detail["tagList"].map{|e| e["tag"]}
  end

  t = @page.at("#videoTitle")
  @title ||= t.inner_text unless t.nil?
  d = @page.at("div#videoComment>div.videoDescription")
  @description ||= d.inner_html unless d.nil?

  @video_url = getflv[:url]
  if @video_url
    @eco = !(/low$/ =~ @video_url).nil?
    @type = case @video_url.match(/^http:\/\/(.+\.)?nicovideo\.jp\/smile\?(.+?)=.*$/).to_a[2]
            when 'm'; :mp4
            when 's'; :swf
            else;     :flv
            end
  end
  @tags ||= @page.search("#video_tags a[rel=tag]").map(&:inner_text)
  @mylist_comment ||= nil

  @fetched = true
  @page
end

#get_videoObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/niconico/video.rb', line 87

def get_video
  raise VideoUnavailableError unless available?
  unless block_given?
      @agent.get_file(video_url)
  else
    cookies = video_cookies.map(&:to_s).join(';')
    uri = URI(video_url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.request_get(uri.request_uri, 'Cookie' => cookies) do |res|
      res.read_body do |body|
        yield body
      end
    end
  end
end

#get_video_by_otherObject



103
104
105
106
107
108
# File 'lib/niconico/video.rb', line 103

def get_video_by_other
  raise VideoUnavailableError unless available?
  warn "WARN: Niconico::Video#get_video_by_other is deprecated. use Video#video_cookie_jar or video_cookie_jar_file, and video_cookies with video_url instead. (Called by #{caller[0]})"
  {cookie: @agent.cookie_jar.cookies(URI.parse(@video_url)),
   url: video_url}
end

#inspectObject



136
137
138
# File 'lib/niconico/video.rb', line 136

def inspect
  "#<Niconico::Video: #{@id}.#{@type} \"#{@title}\"#{@eco ? " low":""}#{(fetched? && !@video_url) ? ' (unavailable)' : ''}#{fetched? ? '' : ' (defered)'}>"
end


115
116
117
118
119
120
121
122
# File 'lib/niconico/video.rb', line 115

def video_cookie_jar
  raise VideoUnavailableError unless available?
  video_cookies.map { |cookie|
    [cookie.domain, "TRUE", cookie.path,
     cookie.secure.inspect.upcase, cookie.expires.to_i,
     cookie.name, cookie.value].join("\t")
  }.join("\n")
end


124
125
126
127
128
129
130
# File 'lib/niconico/video.rb', line 124

def video_cookie_jar_file
  raise VideoUnavailableError unless available?
  Tempfile.new("niconico_cookie_jar_#{self.id}").tap do |io|
    io.puts(video_cookie_jar)
    io.flush
  end
end

#video_cookiesObject



110
111
112
113
# File 'lib/niconico/video.rb', line 110

def video_cookies
  return nil unless available?
  @agent.cookie_jar.cookies(URI.parse(video_url))
end