niconico 1.4.2 → 1.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f18309964d6f29767de3ae7b97e2fb729cb128a8
4
- data.tar.gz: a435cf5c3044951a6068df200298a062a2f88b1e
3
+ metadata.gz: f3206a2fa1c511105d47e732cdfc0a112c324e96
4
+ data.tar.gz: 393d00222c2354bcd57fca25e24b6f863bfbc100
5
5
  SHA512:
6
- metadata.gz: 1e561eea62e13e2f9d91ebc2da2250d943fd4e9162d7c26a931c1518bc3e2f907f7528f4103fb5092568a5be5a37957c7c2a69979fb6e8712828d35454f7daa7
7
- data.tar.gz: b0e9642cc454d46d145206c73b4da59980a76ca22afaf2de505847da4f2e8f4cfb0317b76434506c9f7d3ad4a9af1f4bb6ef577a417c4cf368355010734039cf
6
+ metadata.gz: df5ee49a049b077e2bc7fe61597d003cdd3dfa12663e4ea118acc1e9f32bc74b0eb4274f226071493d39ba6ae1d9a46abc7789e446ae16780c0b46ad94e1b7fd
7
+ data.tar.gz: f3ec9f17c59f053471c8cf34ae7874b3216f569824d09259e9ef8849a0af6dd56362aea5d46962eb811974109b1fd8981bddb9d4c3bff5ada07939891adee54d
@@ -14,19 +14,26 @@ class Niconico
14
14
 
15
15
  TEST_VIDEO_ID = "sm9"
16
16
 
17
- attr_reader :agent, :logined
17
+ attr_reader :agent
18
+
19
+ def logged_in?; @logged_in; end
20
+ alias logined logged_in?
18
21
 
19
22
  def initialize(*args)
20
23
  case args.size
21
24
  when 2
22
25
  @mail, @pass = args
23
26
  when 1
24
- @token = args.first
27
+ if args.first.kind_of?(Hash)
28
+ @mail, @pass, @token = args.first.values_at(:mail, :password, :token)
29
+ else
30
+ @token = args.first
31
+ end
25
32
  else
26
33
  raise ArgumentError, "wrong number of arguments (#{args.size} for 1..2)"
27
34
  end
28
35
 
29
- @logined = false
36
+ @logged_in = false
30
37
 
31
38
  @agent = Mechanize.new.tap do |agent|
32
39
  agent.user_agent = "Niconico.gem (#{Niconico::VERSION}, https://github.com/sorah/niconico)"
@@ -42,19 +49,32 @@ class Niconico
42
49
  end
43
50
 
44
51
  def login(force=false)
45
- return false if !force && @logined
46
-
47
- if @token
52
+ return false if !force && @logged_in
53
+
54
+ if @token && @mail && @pass
55
+ begin
56
+ login_with_token
57
+ rescue LoginError
58
+ login_with_email
59
+ end
60
+ elsif @token
48
61
  login_with_token
49
62
  elsif @mail && @pass
50
63
  login_with_email
51
64
  else
52
- raise 'huh? (may be bug)'
65
+ raise ArgumentError, 'Insufficient options for logging in (token or/and pair of mail and password required)'
53
66
  end
54
67
  end
55
68
 
56
69
  def inspect
57
- "#<Niconico: #{@mail || '(token)'}, #{@logined ? "" : "not "}logined>"
70
+ "#<Niconico: #{@mail || '(token)'}, #{@logged_in ? "" : "not "}logged in>"
71
+ end
72
+
73
+ def token
74
+ return @token if @token
75
+ login unless logged_in?
76
+
77
+ @token = agent.cookie_jar.each('https://www.nicovideo.jp').find{|_| _.name == 'user_session' }.value
58
78
  end
59
79
 
60
80
  class LoginError < StandardError; end
@@ -65,7 +85,8 @@ class Niconico
65
85
  page = @agent.post(URL[:login], 'mail' => @mail, 'password' => @pass)
66
86
 
67
87
  raise LoginError, "Failed to login (x-niconico-authflag is 0)" if page.header["x-niconico-authflag"] == '0'
68
- @logined = true
88
+ @token = nil
89
+ @logged_in = true
69
90
  end
70
91
 
71
92
  def login_with_token
@@ -79,7 +100,7 @@ class Niconico
79
100
  page = @agent.get(URL[:top])
80
101
  raise LoginError, "Failed to login (x-niconico-authflag is 0)" if page.header["x-niconico-authflag"] == '0'
81
102
 
82
- @logined = true
103
+ @logged_in = true
83
104
  end
84
105
 
85
106
  end
@@ -6,7 +6,7 @@ require 'niconico/video'
6
6
 
7
7
  class Niconico
8
8
  def channel_videos(ch)
9
- login unless @logined
9
+ login unless logged_in?
10
10
 
11
11
  rss = Nokogiri::XML(open("http://ch.nicovideo.jp/#{ch}/video?rss=2.0", &:read))
12
12
 
@@ -3,7 +3,7 @@ require 'json'
3
3
 
4
4
  class Niconico
5
5
  def mylist(i)
6
- login unless @logined
6
+ login unless logged_in?
7
7
 
8
8
  page = @agent.get(url = "http://www.nicovideo.jp/mylist/#{i.to_s.sub(/^mylist\//,"")}")
9
9
  #require 'ir_b'; ir b
@@ -8,7 +8,7 @@ class Niconico
8
8
  # options[:method] -> :fav, :view, :comment, :mylist
9
9
  # (or :all) (or :res)
10
10
  def ranking(category = 'all', options={})
11
- login unless @logined
11
+ login unless logged_in?
12
12
 
13
13
  span = options[:span] || :daily
14
14
  span = :hourly if span == :hour
@@ -1,3 +1,3 @@
1
1
  class Niconico
2
- VERSION = "1.4.2"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -4,7 +4,7 @@ require 'niconico/deferrable'
4
4
 
5
5
  class Niconico
6
6
  def video(video_id)
7
- login unless @logined
7
+ login unless logged_in?
8
8
  Video.new(self, video_id)
9
9
  end
10
10
 
@@ -91,10 +91,33 @@ class Niconico
91
91
 
92
92
  def get_video_by_other
93
93
  raise VideoUnavailableError unless available?
94
+ 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]})"
94
95
  {cookie: @agent.cookie_jar.cookies(URI.parse(@video_url)),
95
96
  url: video_url}
96
97
  end
97
98
 
99
+ def video_cookies
100
+ return nil unless available?
101
+ @agent.cookie_jar.cookies(URI.parse(video_url))
102
+ end
103
+
104
+ def video_cookie_jar
105
+ raise VideoUnavailableError unless available?
106
+ video_cookies.map { |cookie|
107
+ [cookie.domain, "TRUE", cookie.path,
108
+ cookie.secure.inspect.upcase, cookie.expires.to_i,
109
+ cookie.name, cookie.value].join("\t")
110
+ }.join("\n")
111
+ end
112
+
113
+ def video_cookie_jar_file
114
+ raise VideoUnavailableError unless available?
115
+ Tempfile.new("niconico_cookie_jar_#{self.id}").tap do |io|
116
+ io.puts(video_cookie_jar)
117
+ io.flush
118
+ end
119
+ end
120
+
98
121
  def inspect
99
122
  "#<Niconico::Video: #{@id}.#{@type} \"#{@title}\"#{@eco ? " low":""}#{(fetched? && !@video_url) ? ' (unavailable)' : ''}#{fetched? ? '' : ' (defered)'}>"
100
123
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: niconico
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shota Fukumori (sora_h)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-09 00:00:00.000000000 Z
11
+ date: 2014-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize