niconico 1.6.0 → 1.7.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 +4 -4
- data/lib/niconico.rb +10 -2
- data/lib/niconico/nico_api.rb +52 -0
- data/lib/niconico/version.rb +1 -1
- data/lib/niconico/video.rb +16 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e974da09ccd8ca51489f33e40cb8cee03492e8c
|
4
|
+
data.tar.gz: cd9f2c241c322cac652d47c82ba2461e9aae46ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83d692cf58272878c31836fcb8fd22cf76e11e063117a337d42aa7b62e9145bdf986d783b8fe611d4bcbd3e62c7c1cc088c17023ce5f2c0d93e325a59bdf37e8
|
7
|
+
data.tar.gz: e7527167a1871dbde1c74fe965b90a35456282be8171da7ce5faf1a52ed042d31b4ae028fbc744f84551f850ba32a923941a52fcd006f36e1edfed2bd4bb9837
|
data/lib/niconico.rb
CHANGED
@@ -9,7 +9,8 @@ class Niconico
|
|
9
9
|
top: 'http://www.nicovideo.jp/',
|
10
10
|
login: 'https://secure.nicovideo.jp/secure/login?site=niconico',
|
11
11
|
watch: 'http://www.nicovideo.jp/watch/',
|
12
|
-
getflv: 'http://flapi.nicovideo.jp/api/getflv'
|
12
|
+
getflv: 'http://flapi.nicovideo.jp/api/getflv',
|
13
|
+
my_mylist: 'http://www.nicovideo.jp/my/mylist'
|
13
14
|
}
|
14
15
|
|
15
16
|
attr_reader :agent
|
@@ -74,12 +75,18 @@ class Niconico
|
|
74
75
|
@token = agent.cookie_jar.each('https://www.nicovideo.jp').find{|_| _.name == 'user_session' }.value
|
75
76
|
end
|
76
77
|
|
78
|
+
def nico_api
|
79
|
+
return @nico_api if @nico_api
|
80
|
+
login unless logged_in?
|
81
|
+
@nico_api = NicoAPI.new(self)
|
82
|
+
end
|
83
|
+
|
77
84
|
class LoginError < StandardError; end
|
78
85
|
|
79
86
|
private
|
80
87
|
|
81
88
|
def login_with_email
|
82
|
-
page = @agent.post(URL[:login], '
|
89
|
+
page = @agent.post(URL[:login], 'mail_tel' => @mail, 'password' => @pass)
|
83
90
|
|
84
91
|
raise LoginError, "Failed to log in (x-niconico-authflag is 0)" if page.header["x-niconico-authflag"] == '0'
|
85
92
|
@token = nil
|
@@ -107,3 +114,4 @@ require 'niconico/mylist'
|
|
107
114
|
require 'niconico/ranking'
|
108
115
|
require 'niconico/channel'
|
109
116
|
require 'niconico/live'
|
117
|
+
require 'niconico/nico_api'
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
class Niconico
|
4
|
+
class NicoAPI
|
5
|
+
class ApiError < Exception; end
|
6
|
+
class AcquiringTokenError < Exception; end
|
7
|
+
|
8
|
+
MYLIST_ITEM_TYPES = {video: 0, seiga: 5}
|
9
|
+
|
10
|
+
def initialize(parent)
|
11
|
+
@parent = parent
|
12
|
+
end
|
13
|
+
|
14
|
+
def agent; @parent.agent; end
|
15
|
+
|
16
|
+
def token; @token ||= get_token; end
|
17
|
+
|
18
|
+
def get_token
|
19
|
+
page = agent.get(Niconico::URL[:my_mylist])
|
20
|
+
match = page.search("script").map(&:inner_text).grep(/\tNicoAPI\.token/) {|v| v.match(/\tNicoAPI\.token = "(.+)";\n/)}.first
|
21
|
+
if match
|
22
|
+
match[1]
|
23
|
+
else
|
24
|
+
raise AcquiringTokenError, "Couldn't find a token"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def mylist_add(group_id, item_type, item_id, description='')
|
29
|
+
!!post(
|
30
|
+
'/api/mylist/add',
|
31
|
+
{
|
32
|
+
group_id: group_id,
|
33
|
+
item_type: MYLIST_ITEM_TYPES[item_type],
|
34
|
+
item_id: item_id,
|
35
|
+
description: description,
|
36
|
+
token: token
|
37
|
+
}
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def post(path, params)
|
44
|
+
uri = URI.join(Niconico::URL[:top], path)
|
45
|
+
page = agent.post(uri, params)
|
46
|
+
json = JSON.parse(page.body)
|
47
|
+
raise ApiError, json unless json['status'] == 'ok'
|
48
|
+
json
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
data/lib/niconico/version.rb
CHANGED
data/lib/niconico/video.rb
CHANGED
@@ -86,7 +86,18 @@ class Niconico
|
|
86
86
|
|
87
87
|
def get_video
|
88
88
|
raise VideoUnavailableError unless available?
|
89
|
-
|
89
|
+
unless block_given?
|
90
|
+
@agent.get_file(video_url)
|
91
|
+
else
|
92
|
+
cookies = video_cookies.map(&:to_s).join(';')
|
93
|
+
uri = URI(video_url)
|
94
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
95
|
+
http.request_get(uri.request_uri, 'Cookie' => cookies) do |res|
|
96
|
+
res.read_body do |body|
|
97
|
+
yield body
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
90
101
|
end
|
91
102
|
|
92
103
|
def get_video_by_other
|
@@ -118,6 +129,10 @@ class Niconico
|
|
118
129
|
end
|
119
130
|
end
|
120
131
|
|
132
|
+
def add_to_mylist(mylist_id, description='')
|
133
|
+
@parent.nico_api.mylist_add(mylist_id, :video, @id, description)
|
134
|
+
end
|
135
|
+
|
121
136
|
def inspect
|
122
137
|
"#<Niconico::Video: #{@id}.#{@type} \"#{@title}\"#{@eco ? " low":""}#{(fetched? && !@video_url) ? ' (unavailable)' : ''}#{fetched? ? '' : ' (defered)'}>"
|
123
138
|
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
|
+
version: 1.7.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:
|
11
|
+
date: 2015-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mechanize
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- lib/niconico/live.rb
|
57
57
|
- lib/niconico/live/api.rb
|
58
58
|
- lib/niconico/mylist.rb
|
59
|
+
- lib/niconico/nico_api.rb
|
59
60
|
- lib/niconico/ranking.rb
|
60
61
|
- lib/niconico/version.rb
|
61
62
|
- lib/niconico/video.rb
|
@@ -79,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
80
|
version: '0'
|
80
81
|
requirements: []
|
81
82
|
rubyforge_project:
|
82
|
-
rubygems_version: 2.
|
83
|
+
rubygems_version: 2.4.1
|
83
84
|
signing_key:
|
84
85
|
specification_version: 4
|
85
86
|
summary: wrapper of Mechanize, optimized for nicovideo.
|