nicovideo 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog ADDED
@@ -0,0 +1,7 @@
1
+ 2008-02-02 version 0.0.4
2
+
3
+ * added functions to get mylist and openlist
4
+ * auto login (ref: http://d.hatena.ne.jp/zorio/20080122/1201018583)
5
+ * added published_at method to VideoPage
6
+ * did unit tests
7
+
data/lib/nicovideo.rb CHANGED
@@ -1,10 +1,13 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
+ require 'rubygems'
3
4
  require 'nicovideo/mechanize-ext'
4
5
  require 'nicovideo/base'
5
- require 'nicovideo/video'
6
+ require 'nicovideo/page'
6
7
  require 'nicovideo/videopage'
7
8
  require 'nicovideo/comments'
9
+ require 'nicovideo/mylist'
10
+ require 'nicovideo/openlist'
8
11
  #require 'nicovideo/tags'
9
12
  #require 'nicovideo/ichiba'
10
13
  #require 'nicovideo/rank'
@@ -1,49 +1,83 @@
1
1
  module Nicovideo
2
2
 
3
- class NicovideoArgError < StandardError ; end
4
- class NicovideoLoginError < StandardError ; end
5
- class NicovideoNotFoundError < StandardError ; end
3
+ class ArgError < StandardError ; end
4
+ class LoginError < StandardError ; end
5
+ class NotFound < StandardError ; end
6
+ class Forbidden < StandardError ; end
7
+ # class NotOpened < StandardError ; end
8
+ # class OpenListNotFound < StandardError ; end
9
+ # class MyListNotFound < StandardError ; end
6
10
 
7
11
  class Base
8
12
 
9
- def initialize mail=nil, password=nil
13
+ def initialize mail=nil, password=nil, auto_login=true
10
14
  @mail = mail
11
15
  @password = password
12
- @logged_in = false
13
16
  @agent = WWW::Mechanize.new()
14
-
17
+ agent_init(auto_login)
18
+ @agent.set_account(@mail, @password)
19
+
15
20
  # for parameters current video
16
21
  @vp = nil
17
22
  self
18
23
  end
19
-
20
- def login mail=nil, password=nil
21
- if !@logged_in
22
- @mail = mail unless @mail
23
- @password = password unless @password
24
- account = {'mail' => @mail, 'password' => @password }
25
-
26
- begin
27
- @agent.post('https://secure.nicovideo.jp/secure/login?site=niconico', account)
28
- # TODO: check login success or failure
29
- @logged_in = true
30
- rescue
31
- @logged_in = false
32
- raise NicovideoLoginError
24
+
25
+ def agent_init auto_login=true
26
+ @agent.instance_eval do
27
+ alias raw_get get
28
+ alias raw_post post
29
+
30
+ def set_account(mail, password) @mail=mail; @password=password end
31
+ def authenticated?(page)
32
+ page.header['x-niconico-authflag'] != '0'
33
+ end
34
+
35
+ def login
36
+ raise ArgError unless (@mail && @password)
37
+ account = {'mail' => @mail, 'password' => @password }
38
+ res = raw_post('https://secure.nicovideo.jp/secure/login?site=niconico', account)
39
+ raise LoginError unless authenticated?(res)
40
+ end
41
+ end
42
+
43
+ if auto_login
44
+ @agent.instance_eval do
45
+ @wait_time = 3
46
+ def get(*args) try(:raw_post, *args) end
47
+ def post(*args) try(:raw_post, *args) end
48
+
49
+ def try(name, *args)
50
+ page = method(name).call(*args)
51
+ unless authenticated?(page)
52
+ self.login
53
+ sleep @wait_time
54
+ page = method(name).call(*args)
55
+ raise LoginError unless authenticated?(page)
56
+ end
57
+ page
58
+ end
33
59
  end
34
60
  end
61
+
62
+ end
63
+
64
+ def login mail=nil, password=nil
65
+ @mail ||= mail
66
+ @password ||= password
67
+ @agent.set_account(@mail, @password)
68
+ @agent.login
35
69
  self
36
70
  end
37
71
 
38
72
  def watch(video_id)
39
- @vp = get_videopage(video_id)
73
+ videopage = get_videopage(video_id)
74
+ @vp = videopage
40
75
  if block_given?
41
- yield @vp
76
+ yield videopage
42
77
  end
78
+ videopage
43
79
  end
44
80
 
45
- def logged_in?() @logged_in end
46
-
47
81
  def get_tags(video_id)
48
82
  get_videopage(video_id).tags
49
83
  end
@@ -64,10 +98,16 @@ module Nicovideo
64
98
  get_videopage(video_id).comments(num)
65
99
  end
66
100
 
101
+ def mylist(mylist_id)
102
+ MyList.new(@agent, mylist_id)
103
+ end
104
+
105
+ def openlist(video_id)
106
+ OpenList.new(@agent, video_id)
107
+ end
108
+
67
109
  private
68
110
  def get_videopage(video_id)
69
- self.login unless @logged_in
70
-
71
111
  if @vp.nil? || video_id != @vp.video_id
72
112
  @vp = VideoPage.new(@agent, video_id)
73
113
  end
@@ -0,0 +1,41 @@
1
+ require 'kconv'
2
+
3
+
4
+ require 'rss/2.0'
5
+ require 'rss/1.0'
6
+
7
+ module Nicovideo
8
+ class MyList < Page
9
+ NICO_MYLIST = 'マイリスト'
10
+
11
+ def initialize agent, mylist_id
12
+ super(agent)
13
+ @mylist_id = mylist_id
14
+ @raw_url = BASE_URL + '/mylist/' + @mylist_id
15
+ @url = BASE_URL + '/mylist/' + @mylist_id + '?rss=2.0'
16
+
17
+ params = ["title", "user", "description", "videos", "rss"]
18
+ self.register_getter params
19
+ end
20
+
21
+ attr_reader :myliset_id
22
+
23
+ def id() @mylist_id end
24
+ def url() @raw_url end
25
+
26
+ private
27
+ def parse(page)
28
+ @rss = RSS::Parser.parse(page.body)
29
+ @title = rss.channel.title.sub(/#{BASE_TITLE+NICO_MYLIST} /, '')
30
+ @user = rss.channel.managingEditor
31
+ @description = rss.channel.description
32
+
33
+ @videos = rss.items.collect {|i|
34
+ vp = VideoPage.new(@agent, i.link.sub(/^.*watch\/(\w+)$/, '\1'))
35
+ vp.title = i.title
36
+ vp
37
+ }
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,88 @@
1
+ require 'kconv'
2
+
3
+ module Nicovideo
4
+
5
+ # This class doesn't access NICONICO DOUGA when an instance created.
6
+ # At the first time you call this instance method, this accesses NICONICO
7
+ class OpenList < Page
8
+ include Enumerable
9
+
10
+ def initialize agent, video_id, pagenum=1, sort='c', order='d'
11
+ super(agent)
12
+ @video_id = video_id
13
+ @pagenum = pagenum
14
+ @sort = sort
15
+ @order = order
16
+ @url = url()
17
+
18
+ params = ["mylists", "total_size", "has_next?", "has_prev?"]
19
+ self.register_getter params
20
+ end
21
+
22
+ attr_reader :pagenum
23
+
24
+ def id() @video_id end
25
+
26
+ # call whenever pagenum changed
27
+ def url
28
+ @url = BASE_URL + '/openlist/' + @video_id + "?page=#{@pagenum}&sort=#{@sort}&order=#{@order}"
29
+ end
30
+
31
+ def each
32
+ @mylists.each {|ml|
33
+ yield ml
34
+ }
35
+ end
36
+
37
+ def to_a() mylists end
38
+
39
+ def pagenum=(pagenum)
40
+ if @pagenum != pagenum
41
+ @pagenum = pagenum
42
+ get_page(self.url, true)
43
+ end
44
+ @pagenum
45
+ end
46
+
47
+ def page=(pagenum)
48
+ self.pagenum = pagenum
49
+ end
50
+
51
+ def next
52
+ self.pagenum = @pagenum + 1
53
+ end
54
+
55
+ def prev
56
+ self.pagenum = @pagenum - 1
57
+ end
58
+
59
+ private
60
+ def parse(page)
61
+ if page.body =~ /<strong>#{@video_id}<\/strong> を含む公開マイリストはありません。/
62
+ @not_found = true
63
+ raise NotFound
64
+ end
65
+
66
+ @total_size = page.search('form[@name="sort"]//td[@class="TXT12"]//strong').first.inner_html.to_i
67
+ if (page/'a//img[@src="http://res.nicovideo.jp/img/common/pager_next_on.gif"]').size > 0
68
+ @has_next = true
69
+ else
70
+ @has_next = false
71
+ end
72
+
73
+ if (page/'a//img[@src="http://res.nicovideo.jp/img/common/pager_back_on.gif"]').size > 0
74
+ @has_prev = true
75
+ else
76
+ @has_prev = false
77
+ end
78
+
79
+ scanpattern = /<a href=\"#{BASE_URL}\/mylist\/(\d+)\">(.+?)<\/a>/ou
80
+ listrefs = page.parser.to_html.scan(scanpattern)
81
+ @mylists = listrefs.inject([]) {|arr, v| # v[0]: mylist id, v[1]: mylist title
82
+ ml = MyList.new(@agent, v[0])
83
+ ml.title = v[1]
84
+ arr << ml
85
+ }
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,91 @@
1
+ require 'kconv'
2
+
3
+ module Nicovideo
4
+
5
+ class Page
6
+ NV_DEBUG_LEVEL = 0
7
+
8
+ BASE_URL = 'http://www.nicovideo.jp'
9
+ BASE_TITLE = 'ニコニコ動画\(RC\)‐'.toutf8
10
+
11
+ def initialize agent
12
+ @agent = agent
13
+ @page = nil
14
+ @title = nil
15
+
16
+ @not_found = false
17
+ end
18
+
19
+ public
20
+ def exists?()
21
+ begin
22
+ @page = @page || get_page
23
+ return true
24
+ rescue
25
+ return false
26
+ end
27
+ end
28
+
29
+ def html()
30
+ page = @page || get_page
31
+ return nil unless page
32
+ page.parser.to_html
33
+ end
34
+
35
+ def title=(title)
36
+ @title = title
37
+ end
38
+
39
+ protected
40
+ def register_getter(params)
41
+ params.each {|p|
42
+ p_noq = p.sub(/\?$/,'')
43
+ eval <<E
44
+ @#{p_noq} = nil
45
+ def #{p}
46
+ if @#{p_noq}.nil?
47
+ @page ||= get_page(@url)
48
+ end
49
+ @#{p_noq}
50
+ end
51
+ E
52
+ }
53
+ end
54
+
55
+ def parse page
56
+ # to be extended
57
+ end
58
+
59
+ def get_page url, force=false
60
+ return @page if (@page && !force)
61
+ raise NotFound if @not_found
62
+
63
+ puts_info 'getting html page : url = ' + url
64
+ begin
65
+ page = @agent.get(url)
66
+ puts_debug page.header
67
+ puts_debug page.body
68
+
69
+ parse(page)
70
+ @page = page
71
+ rescue WWW::Mechanize::ResponseCodeError => e
72
+ rc = e.response_code
73
+ puts_info rc
74
+ if rc == "404" || rc == "410"
75
+ @not_found = true
76
+ raise NotFound
77
+ elsif rc == "403"
78
+ raise Forbidden
79
+ else
80
+ raise e
81
+ end
82
+ end
83
+ @page
84
+ end
85
+
86
+ def puts_error str ; puts str if (NV_DEBUG_LEVEL >= 1) ; end
87
+ def puts_info str ; puts str if (NV_DEBUG_LEVEL >= 2) ; end
88
+ def puts_debug str ; puts str if (NV_DEBUG_LEVEL >= 3) ; end
89
+ end
90
+
91
+ end
@@ -2,7 +2,7 @@ module Nicovideo #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 3
5
+ TINY = 4
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -2,36 +2,25 @@ require 'kconv'
2
2
  require 'cgi'
3
3
 
4
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
5
+ class VideoPage < Page
6
+ def initialize agent, video_id
7
+ super(agent)
11
8
  @video_id = video_id
12
9
  @params = nil
13
- @page = nil
10
+ @url = BASE_URL + '/watch/' + @video_id
11
+ register_getter ["title", "tags", "published_at"]
14
12
  end
15
13
 
16
- attr_reader :video_id
17
-
18
- def exists?()
19
- begin
20
- @page = @page || get_page
21
- return true
22
- rescue
23
- return false
24
- end
25
- end
14
+ attr_reader :video_id, :url
26
15
 
27
- def id() @video_id end
16
+ def id() @video_id end
28
17
 
29
18
  def comments(num=500)
30
19
  puts_info 'getting comment xml : id = ' + @video_id
31
20
  begin
32
- @params = @params || get_params
21
+ @params = get_params unless @params
33
22
  ms = @params['ms']
34
- raise unless ms
23
+ raise ArgError unless ms
35
24
 
36
25
  thread_id = @params['thread_id']
37
26
  body = %!<thread res_from="-#{num}" version="20061206" thread="#{thread_id}" />!
@@ -46,36 +35,46 @@ module Nicovideo
46
35
 
47
36
  def video()
48
37
  begin
49
- @params = @params || get_params
38
+ @params ||= get_params
50
39
  video_url = CGI.unescape(@params['url'])
51
40
  video_flv = @agent.get_file(video_url)
52
41
  video_flv
53
42
  end
54
43
  end
55
44
 
56
- def tags()
57
- @page = @page || get_page
58
- div = @page.parser.search("div#video_tags")
59
- tags = div.to_html.scan(/<a href=\"tag\/[\w\%]+?\">(.+?)<\/a>/ou).inject([]) {|arr, v|
60
- puts_debug v[0]
61
- arr << v[0]
62
- }
63
- tags
45
+ def title=(title)
46
+ @title = title
64
47
  end
65
48
 
66
- def title()
67
- @page = @page || get_page
68
- title = @page.title
69
- title.toutf8.sub(/^#{TITLE_NICONICO}/ou, '')
49
+ def openlist(page=1)
50
+ OpenList.new(@agent, @video_id)
70
51
  end
71
52
 
72
53
  private
54
+ def parse(page)
55
+ # title
56
+ @title = page.title.toutf8.sub(/^#{BASE_TITLE}/ou, '')
57
+
58
+ # tags
59
+ div = page.parser.search("div#video_tags")
60
+ @tags = div.to_html.scan(/<a href=\"tag\/[\w\%]+?\">(.+?)<\/a>/ou).inject([]) {|arr, v|
61
+ puts_debug v[0]
62
+ arr << v[0]
63
+ }
64
+
65
+ # published_at
66
+ str = page.search("div[@id='WATCHHEADER']//p[@class='TXT12']/strong")[0].inner_text
67
+ tm = str.scan(/\d+/)
68
+ @published_at = Time.mktime(*tm)
69
+ end
70
+
73
71
  def get_params
72
+ raise NotFound if @not_found
74
73
  begin
75
74
  unless @params
76
75
  puts_info 'getting params : id = ' + @video_id
77
- @page = @page || get_page
78
- content = @agent.get_file('http://www.nicovideo.jp/api/getflv?v=' + @video_id)
76
+ @page = get_videopage unless @page
77
+ content = @agent.get_file(BASE_URL + '/api/getflv?v=' + @video_id)
79
78
  puts_debug content
80
79
  @params = content.scan(/([^&]+)=([^&]*)/).inject({}){|h, v| h[v[0]] = v[1]; h}
81
80
  else
@@ -83,23 +82,9 @@ module Nicovideo
83
82
  end
84
83
  @params
85
84
  rescue
86
- raise NicovideoNotFoundError
85
+ @not_found = true
86
+ raise NotFound
87
87
  end
88
88
  end
89
-
90
- def get_page
91
- if @video_id
92
- puts_info 'getting HTML page : id = ' + @video_id
93
- begin
94
- @agent.get('http://www.nicovideo.jp/watch/' + @video_id)
95
- rescue
96
- raise NicovideoNotFoundError
97
- end
98
- end
99
- end
100
-
101
- def puts_error str ; puts str if (NV_DEBUG_LEVEL >= 1) ; end
102
- def puts_info str ; puts str if (NV_DEBUG_LEVEL >= 2) ; end
103
- def puts_debug str ; puts str if (NV_DEBUG_LEVEL >= 3) ; end
104
89
  end
105
90
  end
@@ -9,8 +9,11 @@ account = YAML.load_file(ENV['HOME'] + '/.nicovideo/account.yml')
9
9
  mail = account['mail']
10
10
  password = account['password']
11
11
 
12
- # you must login to Nicovideo
13
- nv = Nicovideo.new(mail, password).login
12
+ # create instance
13
+ nv = Nicovideo.new(mail, password)
14
+
15
+ # login to Nicovideo (you don't need to login explicitly at v 0.0.4 or later)
16
+ nv.login
14
17
 
15
18
  # get videos and comments
16
19
  video_ids.each {|video_id|
@@ -9,8 +9,11 @@ account = YAML.load_file(ENV['HOME'] + '/.nicovideo/account.yml')
9
9
  mail = account['mail']
10
10
  password = account['password']
11
11
 
12
- # you must login to Nicovideo
13
- nv = Nicovideo.new(mail, password).login
12
+ # create instance
13
+ nv = Nicovideo.new(mail, password)
14
+
15
+ # login to Nicovideo (you don't need to login explicitly at v 0.0.4 or later)
16
+ nv.login
14
17
 
15
18
  # get videos and comments
16
19
  video_ids.each {|video_id|
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require 'nicovideo'
3
+ require 'yaml'
4
+
5
+ mylist_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
+ # create instance
13
+ nv = Nicovideo.new(mail, password)
14
+
15
+ # login to Nicovideo (you don't need to login explicitly at v 0.0.4 or later)
16
+ nv.login
17
+
18
+ # get mylist
19
+ mylist_ids.each {|mylist_id|
20
+
21
+ ml = nv.mylist(mylist_id)
22
+ # method 'id' and 'mylist_id' return mylist ID(string).
23
+ puts 'mylist id = ' + ml.id
24
+
25
+ # method 'title', 'user' and 'description' return string.
26
+ puts 'title = ' + ml.title
27
+ puts 'user = ' + ml.user
28
+ puts 'description = ' + ml.description
29
+
30
+ # method 'videos' returns array of VideoPage.
31
+ videos = ml.videos
32
+ videos.each {|v|
33
+ puts v.id
34
+ }
35
+
36
+ sleep 3
37
+ }
@@ -0,0 +1,35 @@
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
+ # create instance
13
+ nv = Nicovideo.new(mail, password)
14
+
15
+ # login to Nicovideo (you don't need to login explicitly at v 0.0.4 or later)
16
+ nv.login
17
+
18
+ # get openlist
19
+ video_ids.each {|video_id|
20
+
21
+ ol = nv.openlist(video_id)
22
+ # method 'id' and 'video_id' return video ID(string).
23
+ puts 'video id = ' + ol.id
24
+
25
+ # method 'total_size' returns Fixnum.
26
+ puts 'total_size = ' + ol.total_size.to_s
27
+
28
+ # method 'mylists' returns array of MyList.
29
+ begin
30
+ mls = ol.mylists
31
+ mls.each {|ml| puts ml.id + ':' + ml.title }
32
+ sleep 1
33
+ end while (ol.has_next? && ol.next)
34
+
35
+ }
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestNicovideoLogin < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_login_OK
9
+ valid_account = YAML.load_file(ENV['HOME'] + '/.nicovideo/account.yml')
10
+ nv = Nicovideo.new(valid_account['mail'], valid_account['password'])
11
+
12
+ assert_nothing_raised { nv.login }
13
+
14
+ sleep 1
15
+ end
16
+
17
+ def test_login_NG
18
+ invalid_account = {'mail' => 'invalid@account.com', 'password' => 'invalid' }
19
+ nv = Nicovideo.new(invalid_account['mail'], invalid_account['password'])
20
+
21
+ assert_raise(Nicovideo::LoginError) {
22
+ nv.login
23
+ }
24
+
25
+ sleep 1
26
+ end
27
+
28
+ def test_auto_login
29
+ valid_account = YAML.load_file(ENV['HOME'] + '/.nicovideo/account.yml')
30
+ nv = Nicovideo.new(valid_account['mail'], valid_account['password'])
31
+
32
+ assert_nothing_raised {
33
+ nv.watch('sm500873') {|v|
34
+ puts v.title
35
+ }
36
+ }
37
+ end
38
+ end
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestNicovideoMyList < Test::Unit::TestCase
4
+
5
+ def setup
6
+ account = YAML.load_file(ENV['HOME'] + '/.nicovideo/account.yml')
7
+ @nv = Nicovideo.new(account['mail'], account['password'])
8
+ @nv.login
9
+
10
+ @mid_valid = '3982404' # 公開マイリスト
11
+ @mid_invalid = 'smfdsaf' # IDが間違っている
12
+ @mid_notopened = '3825220' # 非公開
13
+ end
14
+
15
+ def test_mylist_valid
16
+ ml = nil
17
+ assert_nothing_raised {
18
+ ml = @nv.mylist(@mid_valid)
19
+ }
20
+ assert_instance_of(Nicovideo::MyList, ml)
21
+ puts ml.id
22
+ puts ml.url
23
+ puts ml.title
24
+ puts ml.description
25
+ puts ml.videos.size
26
+ ml.videos.each {|v|
27
+ puts v.id + ' ' + v.title
28
+ }
29
+ assert_equal(@mid_valid, ml.id)
30
+ assert_equal("http://www.nicovideo.jp/mylist/"+@mid_valid, ml.url)
31
+ assert_instance_of(String, ml.title)
32
+ assert_instance_of(String, ml.description)
33
+ assert_instance_of(Array, ml.videos)
34
+
35
+ ml.title = "test"
36
+ assert_equal("test", ml.title)
37
+
38
+ sleep 1
39
+ end
40
+
41
+ def test_mylist_invalid
42
+ ml = nil
43
+ assert_nothing_raised {
44
+ ml = @nv.mylist(@mid_invalid)
45
+ }
46
+
47
+ assert_equal(@mid_invalid, ml.id)
48
+ assert_equal("http://www.nicovideo.jp/mylist/"+@mid_invalid, ml.url)
49
+
50
+ assert_raise(Nicovideo::NotFound) { ml.title }
51
+ assert_raise(Nicovideo::NotFound) { ml.description }
52
+ assert_raise(Nicovideo::NotFound) { ml.videos }
53
+
54
+ sleep 1
55
+ end
56
+
57
+ def test_mylist_notopened
58
+ ml = nil
59
+ assert_nothing_raised {
60
+ ml = @nv.mylist(@mid_notopened)
61
+ }
62
+
63
+ assert_equal(@mid_notopened, ml.id)
64
+ assert_equal("http://www.nicovideo.jp/mylist/"+@mid_notopened, ml.url)
65
+
66
+ assert_raise(Nicovideo::Forbidden) { ml.title }
67
+ assert_raise(Nicovideo::Forbidden) { ml.description }
68
+ assert_raise(Nicovideo::Forbidden) { ml.videos }
69
+
70
+ sleep 1
71
+ end
72
+ end
@@ -1,11 +1,11 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
-
3
- class TestNicovideo < Test::Unit::TestCase
4
-
5
- def setup
6
- end
7
-
8
- def test_truth
9
- assert true
10
- end
11
- end
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestNicovideo < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestNicovideoOpenList < Test::Unit::TestCase
4
+
5
+ def setup
6
+ account = YAML.load_file(ENV['HOME'] + '/.nicovideo/account.yml')
7
+ @nv = Nicovideo.new(account['mail'], account['password'])
8
+ @nv.login
9
+
10
+ @vid_valid = 'sm500873' # 公開マイリストが存在(単一ページ)
11
+ @vid_valid2 = 'sm500873' # 公開マイリストが存在(複数ページ)
12
+ @vid_invalid = 'smfdsafd' # IDがおかしい(動画が存在しない)
13
+ @vid_notfound = 'sm500875' # 公開マイリストが存在しない
14
+ end
15
+
16
+ def test_openlist_valid
17
+ ol = nil
18
+ assert_nothing_raised {
19
+ ol = @nv.openlist(@vid_valid)
20
+ }
21
+ assert_instance_of(Nicovideo::OpenList, ol)
22
+ assert_instance_of(Array, ol.mylists)
23
+ assert_instance_of(Array, ol.to_a)
24
+ assert_instance_of(Fixnum, ol.total_size)
25
+ assert_instance_of(Fixnum, ol.pagenum)
26
+ assert(ol.has_next?)
27
+ assert(!ol.has_prev?)
28
+
29
+ assert_nothing_raised {
30
+ ol.pagenum = 2
31
+ }
32
+
33
+ assert_equal(ol.pagenum, 2)
34
+ assert(ol.has_next?)
35
+ assert(ol.has_prev?)
36
+
37
+ assert_nothing_raised {
38
+ ol.next
39
+ }
40
+
41
+ assert_equal(ol.pagenum, 3)
42
+
43
+ sleep 1
44
+ end
45
+
46
+ def test_openlist_valid2
47
+ ol = nil
48
+
49
+ assert_nothing_raised {
50
+ @nv.watch(@vid_valid) {|v|
51
+ ol = v.openlist
52
+ }
53
+ }
54
+
55
+ assert_instance_of(Nicovideo::OpenList, ol)
56
+ assert_instance_of(Array, ol.mylists)
57
+ assert_instance_of(Array, ol.to_a)
58
+ assert_instance_of(Fixnum, ol.total_size)
59
+ assert_instance_of(Fixnum, ol.pagenum)
60
+ assert(ol.has_next?)
61
+ assert(!ol.has_prev?)
62
+
63
+ sleep 1
64
+ end
65
+
66
+ def test_openlist_invalid
67
+ ol = nil
68
+ assert_nothing_raised {
69
+ ol = @nv.openlist(@vid_invalid)
70
+ }
71
+
72
+ assert_instance_of(Nicovideo::OpenList, ol)
73
+ assert_raise(Nicovideo::NotFound) { ol.mylists }
74
+ assert_raise(Nicovideo::NotFound) { ol.to_a }
75
+ assert_raise(Nicovideo::NotFound) { ol.total_size }
76
+ assert_raise(Nicovideo::NotFound) { ol.has_next? }
77
+ assert_raise(Nicovideo::NotFound) { ol.has_prev? }
78
+ assert_raise(Nicovideo::NotFound) { ol.next }
79
+ assert_raise(Nicovideo::NotFound) { ol.prev }
80
+
81
+ sleep 1
82
+ end
83
+
84
+ def test_openlist_notfound
85
+ ol = nil
86
+ assert_nothing_raised {
87
+ ol = @nv.openlist(@vid_notfound)
88
+ }
89
+
90
+ assert_instance_of(Nicovideo::OpenList, ol)
91
+ assert_raise(Nicovideo::NotFound) { ol.mylists }
92
+ assert_raise(Nicovideo::NotFound) { ol.to_a }
93
+ assert_raise(Nicovideo::NotFound) { ol.total_size }
94
+ assert_raise(Nicovideo::NotFound) { ol.has_next? }
95
+ assert_raise(Nicovideo::NotFound) { ol.has_prev? }
96
+ assert_raise(Nicovideo::NotFound) { ol.next }
97
+ assert_raise(Nicovideo::NotFound) { ol.prev }
98
+
99
+ sleep 1
100
+ end
101
+ end
File without changes
@@ -0,0 +1,79 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestNicovideoVideoPage < Test::Unit::TestCase
4
+
5
+ def setup
6
+ account = YAML.load_file(ENV['HOME'] + '/.nicovideo/account.yml')
7
+ @nv = Nicovideo.new(account['mail'], account['password'])
8
+ @nv.login
9
+
10
+ @vid_valid = 'sm500873' # 閲覧可能(組曲)
11
+ @vid_invalid = 'smfdsafd' # IDが間違っている
12
+ @vid_notfound = 'sm500875' # 削除済み
13
+ end
14
+
15
+ # TODO: add test cases of openlist and mylist
16
+ def test_watch_valid
17
+ vp = nil
18
+ assert_nothing_raised {
19
+ vp = @nv.watch(@vid_valid)
20
+ }
21
+ assert_instance_of(Nicovideo::VideoPage, vp)
22
+ assert_instance_of(Array, vp.tags)
23
+ assert_instance_of(String, vp.title)
24
+ assert_instance_of(Nicovideo::Comments, vp.comments)
25
+ # assert_instance_of(String, vp.flv)
26
+ # assert_instance_of(String, vp.video)
27
+
28
+ sleep 1
29
+ end
30
+
31
+ def test_watch_valid2
32
+ vp = nil
33
+ assert_nothing_raised {
34
+ @nv.watch(@vid_valid) {|v|
35
+ assert_instance_of(Nicovideo::VideoPage, v)
36
+ assert_instance_of(Array, v.tags)
37
+ assert_instance_of(String, v.title)
38
+ assert_instance_of(Nicovideo::Comments, v.comments)
39
+ # assert_instance_of(String, v.flv)
40
+ # assert_instance_of(String, v.video)
41
+ }
42
+ }
43
+
44
+ sleep 1
45
+ end
46
+
47
+ # watchは、VideoPageインスタンスを返すのみなので例外発生なし
48
+ # VideoPageインスタンスのメソッドを実行したときに例外発生
49
+ def test_watch_invalid
50
+ vp = nil
51
+ assert_nothing_raised {
52
+ vp = @nv.watch(@vid_invalid)
53
+ }
54
+ assert_instance_of(Nicovideo::VideoPage, vp)
55
+ assert_raise(Nicovideo::NotFound) { vp.tags }
56
+ assert_raise(Nicovideo::NotFound) { vp.title }
57
+ assert_raise(Nicovideo::NotFound) { vp.comments }
58
+ assert_raise(Nicovideo::NotFound) { vp.flv }
59
+ assert_raise(Nicovideo::NotFound) { vp.video }
60
+
61
+ sleep 1
62
+ end
63
+
64
+ # watchは、VideoPageインスタンスを返すのみなので例外発生なし
65
+ def test_watch_notfound
66
+ vp = nil
67
+ assert_nothing_raised {
68
+ vp = @nv.watch(@vid_notfound)
69
+ }
70
+ assert_instance_of(Nicovideo::VideoPage, vp)
71
+ assert_raise(Nicovideo::NotFound) { vp.tags }
72
+ assert_raise(Nicovideo::NotFound) { vp.title }
73
+ assert_raise(Nicovideo::NotFound) { vp.comments }
74
+ assert_raise(Nicovideo::NotFound) { vp.flv }
75
+ assert_raise(Nicovideo::NotFound) { vp.video }
76
+
77
+ sleep 1
78
+ end
79
+ end
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.3
7
- date: 2008-01-09 00:00:00 +09:00
6
+ version: 0.0.4
7
+ date: 2008-02-02 00:00:00 +09:00
8
8
  summary: utils for nicovideo
9
9
  require_paths:
10
10
  - lib
@@ -30,6 +30,7 @@ authors:
30
30
  - emergent
31
31
  files:
32
32
  - README.txt
33
+ - ChangeLog
33
34
  - Rakefile
34
35
  - config/hoe.rb
35
36
  - config/requirements.rb
@@ -37,16 +38,29 @@ files:
37
38
  - lib/nicovideo/version.rb
38
39
  - lib/nicovideo/mechanize-ext.rb
39
40
  - lib/nicovideo/base.rb
41
+ - lib/nicovideo/page.rb
40
42
  - lib/nicovideo/videopage.rb
41
- - lib/nicovideo/video.rb
42
43
  - lib/nicovideo/comments.rb
44
+ - lib/nicovideo/mylist.rb
45
+ - lib/nicovideo/openlist.rb
43
46
  - test/test_helper.rb
44
47
  - test/test_nicovideo.rb
48
+ - test/test_login.rb
49
+ - test/test_videopage.rb
50
+ - test/test_mylist.rb
51
+ - test/test_openlist.rb
45
52
  - sample/nv_download.rb
46
53
  - sample/nv_download2.rb
54
+ - sample/nv_mylist.rb
55
+ - sample/nv_openlist.rb
47
56
  test_files:
57
+ - test/test_openlist.rb
58
+ - test/test_runner.rb
48
59
  - test/test_nicovideo.rb
60
+ - test/test_mylist.rb
61
+ - test/test_videopage.rb
49
62
  - test/test_helper.rb
63
+ - test/test_login.rb
50
64
  rdoc_options:
51
65
  - --main
52
66
  - README.txt
@@ -1,13 +0,0 @@
1
- # not used
2
- module Nicovideo
3
- class Video
4
- def initialize video_id, data
5
- @video_id = video_id
6
- @data = data
7
- end
8
-
9
- def id() @video_id end
10
- def flv() @data end
11
- def video() @data end
12
- end
13
- end