nicovideo 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/nicovideo.rb CHANGED
@@ -3,6 +3,7 @@ $:.unshift File.dirname(__FILE__)
3
3
  require 'nicovideo/mechanize-ext'
4
4
  require 'nicovideo/base'
5
5
  require 'nicovideo/video'
6
+ require 'nicovideo/videopage'
6
7
  require 'nicovideo/comments'
7
8
  #require 'nicovideo/tags'
8
9
  #require 'nicovideo/ichiba'
@@ -1,6 +1,3 @@
1
- require 'cgi'
2
- require 'kconv'
3
-
4
1
  module Nicovideo
5
2
 
6
3
  class NicovideoArgError < StandardError ; end
@@ -8,8 +5,6 @@ module Nicovideo
8
5
  class NicovideoNotFoundError < StandardError ; end
9
6
 
10
7
  class Base
11
- NV_DEBUG_LEVEL = 0
12
- TITLE_NICONICO = 'ニコニコ動画\(RC\)‐'.toutf8
13
8
 
14
9
  def initialize mail=nil, password=nil
15
10
  @mail = mail
@@ -18,9 +13,7 @@ module Nicovideo
18
13
  @agent = WWW::Mechanize.new()
19
14
 
20
15
  # for parameters current video
21
- @video_id = nil
22
- @params = nil
23
- @page = nil
16
+ @vp = nil
24
17
  self
25
18
  end
26
19
 
@@ -31,8 +24,8 @@ module Nicovideo
31
24
  account = {'mail' => @mail, 'password' => @password }
32
25
 
33
26
  begin
34
- @page = @agent.post('https://secure.nicovideo.jp/secure/login?site=niconico', account)
35
- puts_debug @page.title
27
+ @agent.post('https://secure.nicovideo.jp/secure/login?site=niconico', account)
28
+ # TODO: check login success or failure
36
29
  @logged_in = true
37
30
  rescue
38
31
  @logged_in = false
@@ -43,96 +36,44 @@ module Nicovideo
43
36
  end
44
37
 
45
38
  def watch(video_id)
46
- @video_id = video_id
39
+ @vp = get_videopage(video_id)
47
40
  if block_given?
48
- yield self
41
+ yield @vp
49
42
  end
50
43
  end
51
44
 
52
- def logged_in?() @logged_in end
53
- def comments(num=500) self.get_comments(@video_id, num) end
54
- def flv() self.get_flv @video_id end
55
- def title() self.get_title @video_id end
56
- def tags() self.get_tags @video_id end
45
+ def logged_in?() @logged_in end
57
46
 
58
- def get_tags video_id
59
- page = get_page(video_id)
60
- div = page.parser.search("div#video_tags")
61
- tags = div.to_html.scan(/<a href=\"tag\/[\w\%]+?\">(.+?)<\/a>/ou).inject([]) {|arr, v|
62
- puts_debug v[0]
63
- arr << v[0]
64
- }
65
- tags
47
+ def get_tags(video_id)
48
+ get_videopage(video_id).tags
66
49
  end
67
50
 
68
- def get_title video_id
69
- page = get_page(video_id)
70
- title = page.title
71
- puts_info title.toutf8.sub(/^#{TITLE_NICONICO}/ou, '')
72
- title.toutf8.sub(/^#{TITLE_NICONICO}/ou, '')
51
+ def get_title(video_id)
52
+ get_videopage(video_id).title
73
53
  end
74
54
 
75
- def get_video video_id=nil
76
- puts_info 'getting video : id = ' + video_id
77
- self.get_flv video_id
55
+ def get_video(video_id)
56
+ self.get_flv(video_id)
78
57
  end
79
58
 
80
- def get_flv video_id=nil
81
- puts_info 'getting flv : id = ' + video_id if video_id
82
- begin
83
- params = get_params video_id
84
-
85
- video_url = CGI.unescape(params['url'])
86
- video_flv = @agent.get_file(video_url)
87
- Video.new(video_id, video_flv)
88
- end
59
+ def get_flv(video_id)
60
+ get_videopage(video_id).flv
89
61
  end
90
62
 
91
63
  def get_comments video_id, num=500
92
- puts_info 'getting comment xml : id = ' + video_id
93
- begin
94
- params = get_params video_id
95
- raise unless params['ms']
96
-
97
- thread_id = params['thread_id']
98
- body = %!<thread res_from="-#{num}" version="20061206" thread="#{thread_id}" />!
99
- post_url = CGI.unescape(params['ms'])
100
- comment_xml = @agent.post_data(post_url, body).body
101
- puts_debug comment_xml
102
- Comments.new(video_id, comment_xml)
103
- end
64
+ get_videopage(video_id).comments(num)
104
65
  end
105
66
 
106
67
  private
107
- def get_params video_id=nil
68
+ def get_videopage(video_id)
108
69
  self.login unless @logged_in
109
-
110
- begin
111
- if !((video_id == @video_id) && @params)
112
- puts_info 'getting params : id = ' + video_id
113
- @video_id = video_id
114
- page = get_page video_id
115
- content = @agent.get_file('http://www.nicovideo.jp/api/getflv?v=' + video_id)
116
- puts_debug content
117
- @params = content.scan(/([^&]+)=([^&]*)/).inject({}){|h, v| h[v[0]] = v[1]; h}
118
- else
119
- puts_info 'params are already in cache : id = ' + video_id
120
- end
121
- @params
122
- rescue
123
- raise NicovideoNotFoundError
124
- end
125
- end
126
70
 
127
- def get_page video_id
128
- puts_info 'getting HTML page : id = ' + video_id
129
- @agent.get('http://www.nicovideo.jp/watch/' + video_id)
71
+ if @vp.nil? || video_id != @vp.video_id
72
+ @vp = VideoPage.new(@agent, video_id)
73
+ end
74
+ @vp
130
75
  end
131
76
 
132
- def puts_error str ; puts str if (NV_DEBUG_LEVEL >= 1) ; end
133
- def puts_info str ; puts str if (NV_DEBUG_LEVEL >= 2) ; end
134
- def puts_debug str ; puts str if (NV_DEBUG_LEVEL >= 3) ; end
135
-
136
77
  end
137
78
 
138
79
  def Nicovideo.new(mail, password)
@@ -2,7 +2,7 @@ module Nicovideo #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -0,0 +1,94 @@
1
+ require 'kconv'
2
+ require 'cgi'
3
+
4
+ module Nicovideo
5
+ class VideoPage
6
+ NV_DEBUG_LEVEL = 0
7
+ TITLE_NICONICO = 'ニコニコ動画\(RC\)‐'.toutf8
8
+
9
+ def initialize agent, video_id=nil
10
+ @agent = agent
11
+ @video_id = video_id
12
+ @params = nil
13
+ @page = nil
14
+ end
15
+
16
+ attr_reader :video_id
17
+
18
+ def comments(num=500)
19
+ puts_info 'getting comment xml : id = ' + @video_id
20
+ begin
21
+ @params = @params || get_params
22
+ ms = @params['ms']
23
+ raise unless ms
24
+
25
+ thread_id = @params['thread_id']
26
+ body = %!<thread res_from="-#{num}" version="20061206" thread="#{thread_id}" />!
27
+ post_url = CGI.unescape(ms)
28
+ comment_xml = @agent.post_data(post_url, body).body
29
+ puts_debug comment_xml
30
+ Comments.new(@video_id, comment_xml)
31
+ end
32
+ end
33
+
34
+ def flv() return video() end
35
+
36
+ def video()
37
+ begin
38
+ @params = @params || get_params
39
+ video_url = CGI.unescape(@params['url'])
40
+ video_flv = @agent.get_file(video_url)
41
+ Video.new(@video_id, video_flv)
42
+ end
43
+ end
44
+
45
+ def tags()
46
+ @page = @page || get_page
47
+ div = @page.parser.search("div#video_tags")
48
+ tags = div.to_html.scan(/<a href=\"tag\/[\w\%]+?\">(.+?)<\/a>/ou).inject([]) {|arr, v|
49
+ puts_debug v[0]
50
+ arr << v[0]
51
+ }
52
+ tags
53
+ end
54
+
55
+ def title()
56
+ @page = @page || get_page
57
+ title = @page.title
58
+ title.toutf8.sub(/^#{TITLE_NICONICO}/ou, '')
59
+ end
60
+
61
+ private
62
+ def get_params
63
+ begin
64
+ unless @params
65
+ puts_info 'getting params : id = ' + @video_id
66
+ @page = @page || get_page
67
+ content = @agent.get_file('http://www.nicovideo.jp/api/getflv?v=' + @video_id)
68
+ puts_debug content
69
+ @params = content.scan(/([^&]+)=([^&]*)/).inject({}){|h, v| h[v[0]] = v[1]; h}
70
+ else
71
+ puts_info 'params have already gotten : id = ' + @video_id
72
+ end
73
+ @params
74
+ rescue
75
+ raise NicovideoNotFoundError
76
+ end
77
+ end
78
+
79
+ def get_page
80
+ if @video_id
81
+ puts_info 'getting HTML page : id = ' + @video_id
82
+ begin
83
+ @agent.get('http://www.nicovideo.jp/watch/' + @video_id)
84
+ rescue
85
+ raise NicovideoNotFoundError
86
+ end
87
+ end
88
+ end
89
+
90
+ def puts_error str ; puts str if (NV_DEBUG_LEVEL >= 1) ; end
91
+ def puts_info str ; puts str if (NV_DEBUG_LEVEL >= 2) ; end
92
+ def puts_debug str ; puts str if (NV_DEBUG_LEVEL >= 3) ; end
93
+ end
94
+ end
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'nicovideo'
3
+ require 'yaml'
4
+
5
+ video_ids = ARGV
6
+
7
+ # set account
8
+ account = YAML.load_file(ENV['HOME'] + '/.nicovideo/account.yml')
9
+ mail = account['mail']
10
+ password = account['password']
11
+
12
+ # you must login to Nicovideo
13
+ nv = Nicovideo.new(mail, password).login
14
+
15
+ # get videos and comments
16
+ video_ids.each {|video_id|
17
+
18
+ # the another way of nv_download
19
+ puts nv.get_title(video_id)
20
+ puts nv.get_tags(video_id).join(' ')
21
+ puts 'getting comments xml'
22
+ File.open("#{video_id}.xml", "wb") {|f|
23
+ f.write nv.get_comments(video_id, 100).to_xml
24
+ }
25
+ puts 'getting flv file'
26
+ File.open("#{video_id}.flv", "wb") {|f|
27
+ f.write nv.get_flv(video_id)
28
+ }
29
+
30
+ sleep 1
31
+ }
32
+
33
+
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.0.1
7
- date: 2008-01-06 00:00:00 +09:00
6
+ version: 0.0.2
7
+ date: 2008-01-07 00:00:00 +09:00
8
8
  summary: utils for nicovideo
9
9
  require_paths:
10
10
  - lib
@@ -37,11 +37,13 @@ files:
37
37
  - lib/nicovideo/version.rb
38
38
  - lib/nicovideo/mechanize-ext.rb
39
39
  - lib/nicovideo/base.rb
40
+ - lib/nicovideo/videopage.rb
40
41
  - lib/nicovideo/video.rb
41
42
  - lib/nicovideo/comments.rb
42
43
  - test/test_helper.rb
43
44
  - test/test_nicovideo.rb
44
45
  - sample/nv_download.rb
46
+ - sample/nv_download2.rb
45
47
  test_files:
46
48
  - test/test_nicovideo.rb
47
49
  - test/test_helper.rb