nicovideo 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,8 @@
1
+ 2008-02-24 version 0.1.0
2
+
3
+ * added functions to search, tagsearch, ranking
4
+ * added low?() method(ref: http://d.hatena.ne.jp/hayori/20080218/1203312604)
5
+
1
6
  2008-02-16 version 0.0.6
2
7
 
3
8
  * fixed bug of downloading video via mylist
@@ -10,6 +15,6 @@
10
15
  2008-02-02 version 0.0.4
11
16
 
12
17
  * added functions to get mylist and openlist
13
- * auto login (ref: http://d.hatena.ne.jp/zorio/20080122/1201018583)
18
+ * added auto login (ref: http://d.hatena.ne.jp/zorio/20080122/1201018583)
14
19
  * added published_at method to VideoPage
15
20
  * did unit tests
data/lib/nicovideo.rb CHANGED
@@ -8,8 +8,10 @@ require 'nicovideo/videopage'
8
8
  require 'nicovideo/comments'
9
9
  require 'nicovideo/mylist'
10
10
  require 'nicovideo/openlist'
11
+ require 'nicovideo/search'
12
+ require 'nicovideo/tagsearch'
13
+ require 'nicovideo/ranking'
11
14
  #require 'nicovideo/tags'
12
15
  #require 'nicovideo/ichiba'
13
- #require 'nicovideo/rank'
14
16
  #require 'nicovideo/feed'
15
17
 
@@ -105,14 +105,25 @@ module Nicovideo
105
105
  OpenList.new(@agent, video_id)
106
106
  end
107
107
 
108
- # not implemented
109
- def ranking options=nil
110
- Ranking.new(@agent, options)
108
+ # type : 'mylist', 'view' or 'res'
109
+ # span : 'daily', 'newarrival', 'weekly', 'monthly', 'total'
110
+ # category : 'all', 'music' ... and more
111
+ def ranking(type='mylist', span='daily', category='all', pagenum=nil)
112
+ Ranking.new(@agent, type, span, category, pagenum).to_a
111
113
  end
112
114
 
113
- # not implemented
114
- def search option=nil
115
- Search.new(@agent, options)
115
+ # keyword : search keyword
116
+ # sort : nil -> published date
117
+ # 'v' -> playback times
118
+ # 'n' -> commented date
119
+ # 'r' -> comment number
120
+ # 'm' -> mylist number
121
+ def search(keyword, sort=nil, order=nil, pagenum=1)
122
+ Search.new(@agent, keyword, sort, order, pagenum)
123
+ end
124
+
125
+ def tagsearch(keyword, sort=nil, order=nil, pagenum=1)
126
+ TagSearch.new(@agent, keyword, sort, order, pagenum)
116
127
  end
117
128
 
118
129
  private
@@ -29,12 +29,12 @@ module Nicovideo
29
29
  end
30
30
 
31
31
  def each
32
- @mylists.each {|ml|
32
+ self.mylists.each {|ml|
33
33
  yield ml
34
34
  }
35
35
  end
36
36
 
37
- def to_a() mylists end
37
+ def to_a() self.mylists end
38
38
 
39
39
  def pagenum=(pagenum)
40
40
  if @pagenum != pagenum
@@ -56,7 +56,7 @@ module Nicovideo
56
56
  self.pagenum = @pagenum - 1
57
57
  end
58
58
 
59
- private
59
+ protected
60
60
  def parse(page)
61
61
  if page.body =~ /<strong>#{@video_id}<\/strong> を含む公開マイリストはありません。/
62
62
  @not_found = true
@@ -60,7 +60,7 @@ E
60
60
  return @page if (@page && !force)
61
61
  raise NotFound if @not_found
62
62
 
63
- puts_info 'getting html page : url = ' + url
63
+ puts_info 'getting html page : url = ' + url.to_s
64
64
  begin
65
65
  page = @agent.get(url)
66
66
  puts_debug page.header
@@ -0,0 +1,37 @@
1
+ module Nicovideo
2
+ class Ranking < Page
3
+ def initialize agent, type='mylist', span='daily', category='all', pagenum=nil
4
+ super(agent)
5
+ @type = type
6
+ @category = category
7
+ @pagenum = pagenum
8
+ @url = url()
9
+ self.register_getter ["videos"]
10
+ end
11
+
12
+ # call whenever pagenum changed
13
+ def url
14
+ url = "#{BASE_URL}/ranking/#{@type}/#{@span}/#{@category}"
15
+ if @pagenum
16
+ url += '?page=' + pagenum.to_s
17
+ end
18
+ url
19
+ end
20
+
21
+ def to_a
22
+ videos()
23
+ end
24
+
25
+ protected
26
+ def parse(page)
27
+ ranking = page/'h3/a[@class=video]'
28
+ @videos = ranking.inject([]) {|arr,v| #
29
+ #puts v.attributes['href']
30
+ vp = VideoPage.new(@agent, v.attributes['href'].sub(/#{BASE_URL}\/watch\/(\w+)$/,'\1'))
31
+ vp.title = v.inner_html
32
+ arr << vp
33
+ }
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,86 @@
1
+ require 'cgi'
2
+
3
+ module Nicovideo
4
+ class Search < Page
5
+ include Enumerable
6
+
7
+ def initialize agent, keyword, sort=nil, order=nil, pagenum=1
8
+ super(agent)
9
+ @search_type = 'search'
10
+ @keyword = CGI.escape(CGI.escape(keyword)) # なぜか2回エスケープが必要?
11
+ @order = order
12
+
13
+ params = ["videos", "total_size", "has_next?", "has_prev?"]
14
+ self.register_getter params
15
+
16
+ @url = url()
17
+ end
18
+
19
+ def url
20
+ url = "#{BASE_URL}/#{@search_type}?ref=top&s=#{@keyword}"
21
+ url += '&sort=' + @sort if @sort
22
+ url += '&order=' + @order if @order
23
+ url
24
+ end
25
+
26
+ def each
27
+ self.videos.each {|v|
28
+ yield v
29
+ }
30
+ end
31
+
32
+ def to_a() self.videos end
33
+
34
+ def pagenum=(pagenum)
35
+ if @pagenum != pagenum
36
+ @pagenum = pagenum
37
+ get_page(self.url, true)
38
+ end
39
+ @pagenum
40
+ end
41
+
42
+ def page=(pagenum)
43
+ self.pagenum = pagenum
44
+ end
45
+
46
+ def next
47
+ self.pagenum = @pagenum + 1
48
+ end
49
+
50
+ def prev
51
+ self.pagenum = @pagenum - 1
52
+ end
53
+
54
+ protected
55
+ def parse(page)
56
+ if page.body =~ /<\/strong> を含む動画はありません。/
57
+ @not_found = true
58
+ raise NotFound
59
+ end
60
+
61
+ @total_size = page.search('form[@name="sort"]//td[@class="TXT12"]//strong').first.inner_html.to_i
62
+
63
+ if (page/'a//img[@src="http://res.nicovideo.jp/img/common/pager_next_on.gif"]').size > 0
64
+ @has_next = true
65
+ else
66
+ @has_next = false
67
+ end
68
+
69
+ if (page/'a//img[@src="http://res.nicovideo.jp/img/common/pager_back_on.gif"]').size > 0
70
+ @has_prev = true
71
+ else
72
+ @has_prev = false
73
+ end
74
+
75
+ result_xpath = page/'div[@class="thumb_R"]/p[@class="TXT12"]/a[@class="video"]'
76
+ puts result_xpath.size.to_s
77
+ @videos = result_xpath.inject([]) {|arr, v|
78
+ vp = VideoPage.new(@agent, v.attributes['href'].sub(/watch\/(\w+)$/,'\1'))
79
+ vp.title = v.inner_html
80
+ arr << vp
81
+ }
82
+
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,13 @@
1
+ require 'cgi'
2
+
3
+ module Nicovideo
4
+ class TagSearch < Search
5
+
6
+ def initialize agent, keyword, sort=nil, order=nil, pagenum=1
7
+ super(agent, keyword, sort, order, pagenum)
8
+ @search_type = 'tag'
9
+ @url = url()
10
+ end
11
+
12
+ end
13
+ end
@@ -1,8 +1,8 @@
1
1
  module Nicovideo #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 0
5
- TINY = 6
4
+ MINOR = 1
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -50,6 +50,12 @@ module Nicovideo
50
50
  OpenList.new(@agent, @video_id)
51
51
  end
52
52
 
53
+ def low?
54
+ @params ||= get_params
55
+ return true if CGI.unescape(@params['url']) =~ /low$/
56
+ return false
57
+ end
58
+
53
59
  private
54
60
  def parse(page)
55
61
  # title
@@ -13,6 +13,13 @@ class TestNicovideoOpenList < Test::Unit::TestCase
13
13
  @vid_notfound = 'sm500875' # 公開マイリストが存在しない
14
14
  end
15
15
 
16
+ def test_openlist_testing
17
+ result = @nv.openlist("sm500873")
18
+ result.each {|v|
19
+ puts v.title
20
+ }
21
+ end
22
+
16
23
  def test_openlist_valid
17
24
  ol = nil
18
25
  assert_nothing_raised {
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestNicovideoRanking < 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
+ end
10
+
11
+ def test_ranking_valid
12
+ rv = nil
13
+ assert_nothing_raised {
14
+ rv = @nv.ranking
15
+ }
16
+
17
+ rv.each {|v|
18
+ puts v.id + ':' + v.published_at.to_s + ':' + v.title
19
+ sleep 1
20
+ }
21
+
22
+ sleep 1
23
+ end
24
+
25
+ def test_ranking_invalid
26
+ end
27
+
28
+ def test_ranking_notopened
29
+ end
30
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestNicovideoSearch < 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
+ end
10
+
11
+ def test_search_testing0
12
+ result = @nv.search("aaa")
13
+ puts result.total_size
14
+ result.each {|v|
15
+ puts v.title
16
+ }
17
+ sleep 1
18
+ end
19
+
20
+ def test_search_testing1
21
+ result = @nv.search("ミク")
22
+ result.each {|v|
23
+ puts v.title
24
+ }
25
+ sleep 1
26
+ end
27
+
28
+ def test_search_testing2
29
+ result = @nv.search("ミク", 'n')
30
+ result.each {|v|
31
+ puts v.title
32
+ }
33
+ sleep 1
34
+ end
35
+
36
+ def test_search_testing3
37
+ result = @nv.search("ミク", 'n', 'd')
38
+ result.each {|v|
39
+ puts v.title
40
+ }
41
+ sleep 1
42
+ end
43
+
44
+
45
+ =begin
46
+ def test_search_valid
47
+ rv = nil
48
+ assert_nothing_raised {
49
+ rv = @nv.ranking
50
+ }
51
+
52
+ rv.each {|v|
53
+ puts v.id + ':' + v.published_at.to_s + ':' + v.title
54
+ sleep 1
55
+ }
56
+
57
+ sleep 1
58
+ end
59
+
60
+ def test_search_invalid
61
+ end
62
+
63
+ def test_search_notopened
64
+ end
65
+ =end
66
+ end
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestNicovideoTagSearch < 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
+ end
10
+
11
+ def test_tagsearch_testing0
12
+ result = @nv.tagsearch("aaa")
13
+ puts result.total_size
14
+ result.each {|v|
15
+ puts v.title
16
+ }
17
+ sleep 1
18
+ end
19
+
20
+ def test_tagsearch_testing1
21
+ result = @nv.tagsearch("ミク")
22
+ result.each {|v|
23
+ puts v.title
24
+ }
25
+ sleep 1
26
+ end
27
+
28
+ def test_tagsearch_testing1
29
+ result = @nv.tagsearch("ミク", 'v', 'a')
30
+ result.each {|v|
31
+ puts v.title
32
+ }
33
+ sleep 1
34
+ end
35
+
36
+ def test_search_testing2
37
+ result = @nv.tagsearch("ミク", 'n')
38
+ result.each {|v|
39
+ puts v.title
40
+ }
41
+ end
42
+
43
+
44
+ =begin
45
+ def test_search_valid
46
+ rv = nil
47
+ assert_nothing_raised {
48
+ rv = @nv.ranking
49
+ }
50
+
51
+ rv.each {|v|
52
+ puts v.id + ':' + v.published_at.to_s + ':' + v.title
53
+ sleep 1
54
+ }
55
+
56
+ sleep 1
57
+ end
58
+
59
+ def test_search_invalid
60
+ end
61
+
62
+ def test_search_notopened
63
+ end
64
+ =end
65
+ end
@@ -8,7 +8,7 @@ class TestNicovideoVideoPage < Test::Unit::TestCase
8
8
  @nv.login
9
9
 
10
10
  # @vid_valid = 'sm500873' # 閲覧可能(組曲)
11
- @vid_valid = 'sm1892625' # 閲覧可能(短い動画)
11
+ @vid_valid = 'sm2407057' # 閲覧可能(短い動画)
12
12
  @vid_invalid = 'smfdsafd' # IDが間違っている
13
13
  @vid_notfound = 'sm500875' # 削除済み
14
14
  end
@@ -20,6 +20,7 @@ class TestNicovideoVideoPage < Test::Unit::TestCase
20
20
  vp = @nv.watch(@vid_valid)
21
21
  }
22
22
  assert_nothing_raised { vp.flv }
23
+ sleep 5
23
24
  end
24
25
  =begin
25
26
  def test_watch_valid
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.6
7
- date: 2008-02-16 00:00:00 +09:00
6
+ version: 0.1.0
7
+ date: 2008-02-24 00:00:00 +09:00
8
8
  summary: utils for nicovideo
9
9
  require_paths:
10
10
  - lib
@@ -43,6 +43,9 @@ files:
43
43
  - lib/nicovideo/comments.rb
44
44
  - lib/nicovideo/mylist.rb
45
45
  - lib/nicovideo/openlist.rb
46
+ - lib/nicovideo/ranking.rb
47
+ - lib/nicovideo/search.rb
48
+ - lib/nicovideo/tagsearch.rb
46
49
  - test/test_helper.rb
47
50
  - test/runner.rb
48
51
  - test/test_nicovideo.rb
@@ -50,6 +53,9 @@ files:
50
53
  - test/test_videopage.rb
51
54
  - test/test_mylist.rb
52
55
  - test/test_openlist.rb
56
+ - test/test_ranking.rb
57
+ - test/test_search.rb
58
+ - test/test_tagsearch.rb
53
59
  - sample/nv_download.rb
54
60
  - sample/nv_download2.rb
55
61
  - sample/nv_mylist.rb
@@ -58,9 +64,12 @@ test_files:
58
64
  - test/test_openlist.rb
59
65
  - test/test_nicovideo.rb
60
66
  - test/test_mylist.rb
67
+ - test/test_ranking.rb
68
+ - test/test_tagsearch.rb
61
69
  - test/test_videopage.rb
62
70
  - test/test_helper.rb
63
71
  - test/test_login.rb
72
+ - test/test_search.rb
64
73
  rdoc_options:
65
74
  - --main
66
75
  - README.txt