nicovideo 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,3 +1,9 @@
1
+ class NilClass
2
+ def empty?
3
+ true
4
+ end
5
+ end
6
+
1
7
  require 'config/requirements'
2
8
  require 'config/hoe' # setup Hoe + all gem configuration
3
9
 
@@ -62,7 +62,8 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
62
62
  p.changes = p.paragraphs_of("ChangeLog", 0..1).join("\\n\\n")
63
63
  #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
64
64
  p.extra_deps = [
65
- ['mechanize', '>=0.6.0']
65
+ ['mechanize', '>=0.9.2'],
66
+ ['json', '>=1.1.4']
66
67
  ]
67
68
  #p.spec_extras = {} # A hash of extra values to set in the gemspec.
68
69
  end
@@ -3,6 +3,7 @@ require 'kconv'
3
3
 
4
4
  require 'rss/2.0'
5
5
  require 'rss/1.0'
6
+ require 'json'
6
7
 
7
8
  module Nicovideo
8
9
  class MyList < Page
@@ -23,10 +24,45 @@ module Nicovideo
23
24
  def id() @mylist_id end
24
25
  def url() @raw_url end
25
26
 
27
+ def add(video_id)
28
+ video_page = VideoPage.new @agent, video_id
29
+
30
+ begin
31
+ add_result = @agent.post(video_page.url, {
32
+ :mylist => "add",
33
+ :mylistgroup_name => "",
34
+ :csrf_token => video_page.csrf_token,
35
+ :group_id => @mylist_id,
36
+ :ajax => "1"})
37
+
38
+ result_code = JSON.parse(add_result.body.sub(/^\(?(.*?)\)?$/, '\1'))
39
+
40
+ if result_code["result"] == "success" then
41
+ # added video isn't applied to rss immediately, so add video into list by hand.
42
+ page = @page || get_page(@url)
43
+ @videos << video_page
44
+ return self
45
+ end
46
+ raise ArgError if result_code["result"] == "duperror"
47
+ raise StandardError
48
+ rescue WWW::Mechanize::ResponseCodeError => e
49
+ rc = e.response_code
50
+ puts_info rc
51
+ if rc == "404" || rc == "410"
52
+ @not_found = true
53
+ raise NotFound
54
+ elsif rc == "403"
55
+ raise Forbidden
56
+ else
57
+ raise e
58
+ end
59
+ end
60
+ end
61
+
26
62
  private
27
63
  def parse(page)
28
64
  @rss = RSS::Parser.parse(page.body)
29
- @title = rss.channel.title.sub(/#{BASE_TITLE2+NICO_MYLIST} /, '')
65
+ @title = rss.channel.title.sub(/#{BASE_TITLE1}/,'').sub(/#{NICO_MYLIST} /, '')
30
66
  @user = rss.channel.managingEditor
31
67
  @description = rss.channel.description
32
68
 
@@ -61,24 +61,24 @@ module Nicovideo
61
61
 
62
62
  protected
63
63
  def parse(page)
64
- if page.body =~ /<strong>#{@video_id}<\/strong>を含む公開マイリストはありません。/
64
+ if page.body =~ /<strong.*?>#{@video_id}<\/strong> を登録している公開マイリストはありません。/
65
65
  @not_found = true
66
66
  raise NotFound
67
67
  end
68
68
 
69
- @total_size = page.search('//form[@name="sort"]//td[@class="TXT12"]//strong').first.inner_html.sub(/,/,'').to_i
69
+ @total_size = page.search('//div[@class="mb8p4"]//p[@class="TXT12"]//strong').first.inner_html.sub(/,/,'').to_i
70
70
 
71
71
  @has_next = false
72
72
  @has_prev = false
73
- respages = page/'//div[@class="mb16p4"]//p[@class="TXT12"]//a'
73
+ respages = page/'//div[@class="pagelink"]'
74
74
  puts_info respages.size
75
75
  respages.each {|r| puts_info r.inner_html }
76
76
  if respages.size > 0
77
77
  respages.each {|text|
78
- if text.inner_html =~ /前のページ/
78
+ if text.inner_html =~ /<a class="prevpage".*?>.*?戻る.*?<\/a>/
79
79
  @has_prev = true
80
80
  end
81
- if text.inner_html =~ /次のページ/
81
+ if text.inner_html =~ /<a class="nextpage".*?>.*?進む.*?<\/a>/
82
82
  @has_next = true
83
83
  end
84
84
  }
@@ -6,8 +6,8 @@ module Nicovideo
6
6
  NV_DEBUG_LEVEL = 0
7
7
 
8
8
  BASE_URL = 'http://www.nicovideo.jp'
9
- BASE_TITLE1 = '‐ニコニコ動画\(秋\)'.toutf8
10
- BASE_TITLE2 = 'ニコニコ動画\(秋\)‐'.toutf8
9
+ BASE_TITLE1 = '‐ニコニコ動画\(.*?\)'.toutf8
10
+ BASE_TITLE2 = 'ニコニコ動画\(.*?\)‐'.toutf8
11
11
 
12
12
  def initialize agent
13
13
  @agent = agent
@@ -68,19 +68,19 @@ module Nicovideo
68
68
  raise NotFound
69
69
  end
70
70
 
71
- @total_size = page.search('form[@name="sort"]//td[@class="TXT12"]//strong').first.inner_html.sub(/,/,'').to_i
71
+ @total_size = page.search('strong[@class="search_total"]').first.inner_html.sub(/,/,'').to_i
72
72
 
73
73
  @has_next = false
74
74
  @has_prev = false
75
- respages = page/'//div[@class="mb16p4"]//p[@class="TXT12"]//a'
75
+ respages = page/'//div[@class="pagelink"]'
76
76
  puts_info respages.size
77
77
  respages.each {|r| puts_info r.inner_html }
78
78
  if respages.size > 0
79
79
  respages.each {|text|
80
- if text.inner_html =~ /前のページ/
80
+ if text.inner_html =~ /<a class="prevpage".*?>.*?戻る.*?<\/a>/
81
81
  @has_prev = true
82
82
  end
83
- if text.inner_html =~ /次のページ/
83
+ if text.inner_html =~ /<a class="nextpage".*?>.*?進む.*?<\/a>/
84
84
  @has_next = true
85
85
  end
86
86
  }
@@ -18,7 +18,7 @@ module Nicovideo
18
18
  retry_count = 0
19
19
  begin
20
20
  body = timeout(wait_sec) do
21
- open("http://www.nicovideo.jp/api/getthumbinfo/#{video_id}", :proxy => @proxy_url) do |f|
21
+ open("http://ext.nicovideo.jp/api/getthumbinfo/#{video_id}", :proxy => @proxy_url) do |f|
22
22
  f.read
23
23
  end
24
24
  end
@@ -34,11 +34,14 @@ module Nicovideo
34
34
  end
35
35
 
36
36
  def get_elements(parent)
37
- thumbnail_info = {}
37
+ thumbnail_info = ThumbInfo.new
38
38
 
39
39
  parent.each_element do |element|
40
- if element.has_elements? then
41
- thumbnail_info[element.name] = element.texts # doesn't support recursive xml.
40
+ if element.name == 'tags' then
41
+ thumbnail_info.tags[element.attributes['domain']] = []
42
+ element.each_element do |child|
43
+ thumbnail_info.tags[element.attributes['domain']] << child.text
44
+ end
42
45
  next
43
46
  end
44
47
  thumbnail_info[element.name] = element.text
@@ -46,4 +49,20 @@ module Nicovideo
46
49
  thumbnail_info
47
50
  end
48
51
  end
52
+
53
+ class ThumbInfo < Hash
54
+ attr_accessor :tags
55
+ def initialize
56
+ @tags = {}
57
+ end
58
+
59
+ def has_tag?(tag)
60
+ @tag_hash ||= tag_flatten.inject({}) {|tag_hash, temp_tag| tag_hash[temp_tag] = temp_tag}
61
+ @tag_hash.has_key? tag
62
+ end
63
+
64
+ def tag_flatten
65
+ @tag_flatten ||= @tags.values.flatten
66
+ end
67
+ end
49
68
  end
@@ -2,7 +2,7 @@ module Nicovideo #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 7
5
+ TINY = 8
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -8,7 +8,7 @@ module Nicovideo
8
8
  @video_id = video_id
9
9
  @params = nil
10
10
  @url = BASE_URL + '/watch/' + @video_id
11
- register_getter ["title", "tags", "published_at"]
11
+ register_getter ["title", "tags", "published_at", "csrf_token"]
12
12
  end
13
13
 
14
14
  attr_reader :video_id, :url
@@ -86,6 +86,9 @@ module Nicovideo
86
86
  str = page.search("div[@id='WATCHHEADER']//p[@class='TXT12']/strong")[0].inner_text
87
87
  tm = str.scan(/\d+/)
88
88
  @published_at = Time.mktime(*tm)
89
+
90
+ # csrf_token
91
+ @csrf_token = page.search("form[@name='mylist_form']//input[@name='csrf_token']")[0]['value']
89
92
  end
90
93
 
91
94
  def get_params
@@ -10,6 +10,9 @@ class TestNicovideoMyList < Test::Unit::TestCase
10
10
  @mid_valid = '3982404' # 公開マイリスト
11
11
  @mid_invalid = 'smfdsaf' # IDが間違っている
12
12
  @mid_notopened = '3825220' # 非公開
13
+ #@mid_testers_own = '12149071' # テスト用アカウントに登録されているマイリストのID
14
+
15
+ @vid_valid = 'sm2407057' # 閲覧可能(短い動画)
13
16
  end
14
17
 
15
18
  def test_mylist_valid
@@ -69,4 +72,21 @@ class TestNicovideoMyList < Test::Unit::TestCase
69
72
 
70
73
  sleep 5
71
74
  end
75
+
76
+ def test_add
77
+ ml = nil
78
+ assert_nothing_raised {
79
+ ml = @nv.mylist(@mid_testers_own)
80
+ }
81
+
82
+ # FIXME removing video function is NOT implemented,
83
+ # so you MUST remove video from mylist before test.
84
+ assert_nothing_raised {
85
+ ml.add @vid_valid
86
+ }
87
+
88
+ assert_equal(@vid_valid, ml.videos.last.video_id)
89
+
90
+ sleep 5
91
+ end
72
92
  end
@@ -8,7 +8,8 @@ class TestNicovideoVideoPage < Test::Unit::TestCase
8
8
  @nv.login
9
9
 
10
10
  # @vid_valid = 'sm500873' # 閲覧可能(組曲)
11
- @vid_valid = 'sm2407057' # 閲覧可能(短い動画)
11
+ #@vid_valid = 'sm2407057' # 閲覧可能(短い動画)
12
+ @vid_valid = 'sm9' # 閲覧可能(短い動画)
12
13
  @vid_invalid = 'smfdsafd' # IDが間違っている
13
14
  @vid_notfound = 'sm500875' # 削除済み
14
15
  end
@@ -40,10 +41,12 @@ class TestNicovideoVideoPage < Test::Unit::TestCase
40
41
  assert_instance_of(String, vp.title)
41
42
  assert_instance_of(Time, vp.published_at)
42
43
  assert_instance_of(Nicovideo::Comments, vp.comments)
44
+ assert_instance_of(String, vp.csrf_token)
43
45
 
44
46
  puts vp.tags
45
47
  puts vp.title
46
48
  puts vp.published_at
49
+ puts vp.csrf_token
47
50
  # assert_nothing_raised { vp.flv }
48
51
  #assert_instance_of(String, vp.flv)
49
52
  #assert_instance_of(String, vp.video)
@@ -60,6 +63,7 @@ class TestNicovideoVideoPage < Test::Unit::TestCase
60
63
  assert_instance_of(Array, v.tags)
61
64
  assert_instance_of(String, v.title)
62
65
  assert_instance_of(Nicovideo::Comments, v.comments)
66
+ assert_instance_of(String, v.csrf_token)
63
67
  # assert_instance_of(String, v.flv)
64
68
  # assert_instance_of(String, v.video)
65
69
  }
@@ -81,6 +85,7 @@ class TestNicovideoVideoPage < Test::Unit::TestCase
81
85
  assert_raise(Nicovideo::NotFound) { vp.comments }
82
86
  assert_raise(Nicovideo::NotFound) { vp.flv }
83
87
  assert_raise(Nicovideo::NotFound) { vp.video }
88
+ assert_raise(Nicovideo::NotFound) { vp.csrf_token }
84
89
 
85
90
  sleep 1
86
91
  end
@@ -97,6 +102,7 @@ class TestNicovideoVideoPage < Test::Unit::TestCase
97
102
  assert_raise(Nicovideo::NotFound) { vp.comments }
98
103
  assert_raise(Nicovideo::NotFound) { vp.flv }
99
104
  assert_raise(Nicovideo::NotFound) { vp.video }
105
+ assert_raise(Nicovideo::NotFound) { vp.csrf_token }
100
106
 
101
107
  sleep 1
102
108
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nicovideo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - emergent
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-09 00:00:00 +09:00
12
+ date: 2009-05-01 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,17 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.6.0
23
+ version: 0.9.2
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.4
24
34
  version:
25
35
  - !ruby/object:Gem::Dependency
26
36
  name: hoe