nico_util 0.0.1 → 0.0.2

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: 084c27e82607a1983c6b88fc866c4f92acfc394b
4
- data.tar.gz: 8381db0d5857c45fdc99557bca9c33b4e839f54f
3
+ metadata.gz: 9a4d9ec0e1077cf48fb35c88da752f5fd87160e6
4
+ data.tar.gz: 1cb82c774e49de56cf11dba57f6d9de6ded01ac0
5
5
  SHA512:
6
- metadata.gz: 13db94083c011a4f6eb253b2883474f7dcdc1db5b37e071b21d814bbac7b300b32123c29a414655d22a91644560200831e4288b36aba9c05f3237512bc451cce
7
- data.tar.gz: 78a95c80a0c76f39c7c128c15c4a5cfb3af94618468a1473d00b5688f4d2a3ab92bd9d0ebda33b9d1a16d67a6e010e6f93197b69f9044683c2f5f6cbe9a2a27d
6
+ metadata.gz: 95f6dd28c0b30be41493abedede888195af6b482374b9bab87b951180ccb80894f746d0c2adcd888e2d401ce10ee6d7c1fad635b4b5cd50c97e4f65fc5aba522
7
+ data.tar.gz: 28dcf6b59eb9260199dab9a15888011e266203c4695c609418750df2915c2776d9aae7cd7c6fb6d2c8738aadb55e40889fd49336a0bfea1ad991d2b7e8e4f6ea
data/README.md CHANGED
@@ -7,6 +7,8 @@ Utility API for niconico (<http://www.nicovideo.jp>)
7
7
  * Sign in to niconico
8
8
  * Get comments of video
9
9
  * Get comments of live
10
+ * Get comments of illust
11
+ * Download a illust
10
12
 
11
13
  ## Install
12
14
 
@@ -36,7 +38,7 @@ pass = 'password'
36
38
  # login
37
39
  nico = NicoUtil::login email, pass
38
40
 
39
- # get and show comments of a video
41
+ # get comments of a video
40
42
  comments = nico.video('sm1097445').comments
41
43
  comments.each do |comment|
42
44
  p comment
@@ -53,6 +55,18 @@ nico.live('lv242616226').connect do |status, data|
53
55
  puts 'disconnect'
54
56
  end
55
57
  end
58
+
59
+ # get illust
60
+ illust = nico.illust('im3768140')
61
+
62
+ # get comments of a illust
63
+ p illust.comments
64
+
65
+ # get raw url of a illust
66
+ p illust.image_url
67
+
68
+ # download and save a illust
69
+ illust.save 'img.jpg'
56
70
  ```
57
71
 
58
72
  ## Contributing
data/lib/illust.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'json'
2
+
3
+ class NicoUtil::Niconico
4
+ def illust illust_id
5
+ Illust.new self, illust_id
6
+ end
7
+
8
+ class Illust
9
+ def initialize owner, illust_id
10
+ @owner = owner
11
+ if illust_id[0..1] == 'im'
12
+ @id = illust_id[2..-1]
13
+ else
14
+ @id = illust_id
15
+ end
16
+ end
17
+
18
+ def comments
19
+ return @comments if @comments
20
+
21
+ host = 'seiga.nicovideo.jp'
22
+ path = "/ajax/illust/comment/list?id=#{@id}&mode=all"
23
+
24
+ response = Net::HTTP.new(host).start do |http|
25
+ request = Net::HTTP::Get.new(path)
26
+ request['cookie'] = @owner.cookie
27
+ http.request(request)
28
+ end
29
+
30
+ comments = []
31
+ body = JSON.parse response.body.force_encoding('utf-8')
32
+
33
+ if body.key? 'errors'
34
+ raise InvalidIDError, body["errors"]
35
+ end
36
+
37
+ body["comment_list"].each do |comment|
38
+ comments << comment["text"]
39
+ end
40
+
41
+ @comments = comments
42
+ end
43
+
44
+ def image_url
45
+ return @image_url if @image_url
46
+
47
+ host = 'seiga.nicovideo.jp'
48
+ path = "/image/source?id=#{@id}"
49
+
50
+ response = Net::HTTP.new(host).start do |http|
51
+ request = Net::HTTP::Get.new(path)
52
+ request['cookie'] = @owner.cookie
53
+ http.request(request)
54
+ end
55
+
56
+ case response
57
+ when Net::HTTPRedirection
58
+ url = response['location']
59
+ @image_url = url.gsub 'lohas.nicoseiga.jp/o', 'lohas.nicoseiga.jp/priv'
60
+ end
61
+
62
+ @image_url
63
+ end
64
+
65
+ def download
66
+ Net::HTTP.get_response(URI.parse(image_url)).body
67
+ end
68
+
69
+ def save filename
70
+ #TODO: identify filetype [png, jpg, gif]
71
+ File.write(filename, download)
72
+ end
73
+ end
74
+ end
data/lib/live.rb CHANGED
@@ -7,6 +7,7 @@ class NicoUtil::Niconico
7
7
  def initialize owner, live_id
8
8
  @owner = owner
9
9
  @id = live_id
10
+ playerstatus
10
11
  end
11
12
 
12
13
  def playerstatus
@@ -24,6 +25,11 @@ class NicoUtil::Niconico
24
25
  playerstatus = {}
25
26
  body = response.body.force_encoding('utf-8')
26
27
  doc = REXML::Document.new(body).elements['getplayerstatus']
28
+
29
+ if doc.attributes["status"] == 'fail'
30
+ raise InvalidIDError, doc.elements['error/code'].text
31
+ end
32
+
27
33
  # stream = doc.elements['stream']
28
34
  # title = stream.elements['title'].text
29
35
  # desc = stream.elements['description'].text
@@ -1,3 +1,3 @@
1
1
  module NicoUtil
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/nico_util.rb CHANGED
@@ -3,12 +3,6 @@ require 'net/http'
3
3
  require 'openssl'
4
4
  require 'rexml/document'
5
5
 
6
- # ニコ動コメント取得
7
- # https://blog.nanoway.net/web/nicovideo-comment-api
8
-
9
- # ニコ生コメント取得
10
- # http://pita.s374.xrea.com/cms/75/
11
-
12
6
  module NicoUtil
13
7
  def self.login email, pass
14
8
  Niconico.new email, pass
@@ -16,6 +10,7 @@ module NicoUtil
16
10
 
17
11
  class Niconico
18
12
  class AuthenticationError < StandardError; end
13
+ class InvalidIDError < StandardError; end
19
14
 
20
15
  def initialize email, pass
21
16
  @cookie = nil
@@ -70,4 +65,5 @@ end
70
65
 
71
66
  require_relative 'video.rb'
72
67
  require_relative 'live.rb'
68
+ require_relative 'illust.rb'
73
69
 
data/lib/video.rb CHANGED
@@ -7,6 +7,7 @@ class NicoUtil::Niconico
7
7
  def initialize owner, video_id
8
8
  @owner = owner
9
9
  @id = video_id
10
+ flv_info
10
11
  end
11
12
 
12
13
  def flv_info
@@ -23,6 +24,10 @@ class NicoUtil::Niconico
23
24
  http.request(request)
24
25
  end
25
26
 
27
+ if response.body[0..4] == 'error'
28
+ raise InvalidIDError, 'Invalid video id'
29
+ end
30
+
26
31
  flv_info = {}
27
32
  response.body.split('&').each do |st|
28
33
  stt = st.split('=')
@@ -39,7 +44,7 @@ class NicoUtil::Niconico
39
44
  info = flv_info()
40
45
  uri = URI.parse info[:ms]
41
46
 
42
- response = Net::HTTP.new(uri.host).start { |http|
47
+ response = Net::HTTP.new(uri.host).start do |http|
43
48
  request = Net::HTTP::Post.new(uri.path)
44
49
  thread_id = info[:thread_id]
45
50
  version = '20061206' # 20090904
@@ -53,7 +58,7 @@ class NicoUtil::Niconico
53
58
  <thread thread='#{thread_id}' version='#{version}' res_from='-#{limit_max}' fork='#{frk}'/>
54
59
  </packet>"
55
60
  http.request(request)
56
- }
61
+ end
57
62
 
58
63
  comments = []
59
64
  xml = response.body.gsub('</chat>', "</chat>\n")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nico_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tatsuya Tanaka (tattn)
@@ -68,6 +68,7 @@ files:
68
68
  - Rakefile
69
69
  - bin/console
70
70
  - bin/setup
71
+ - lib/illust.rb
71
72
  - lib/live.rb
72
73
  - lib/nico_util.rb
73
74
  - lib/nico_util/version.rb