nico_util 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 084c27e82607a1983c6b88fc866c4f92acfc394b
4
+ data.tar.gz: 8381db0d5857c45fdc99557bca9c33b4e839f54f
5
+ SHA512:
6
+ metadata.gz: 13db94083c011a4f6eb253b2883474f7dcdc1db5b37e071b21d814bbac7b300b32123c29a414655d22a91644560200831e4288b36aba9c05f3237512bc451cce
7
+ data.tar.gz: 78a95c80a0c76f39c7c128c15c4a5cfb3af94618468a1473d00b5688f4d2a3ab92bd9d0ebda33b9d1a16d67a6e010e6f93197b69f9044683c2f5f6cbe9a2a27d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nico_util.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Tatsuya Tanaka
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # NicoUtil
2
+
3
+ Utility API for niconico (<http://www.nicovideo.jp>)
4
+
5
+ ## Features
6
+
7
+ * Sign in to niconico
8
+ * Get comments of video
9
+ * Get comments of live
10
+
11
+ ## Install
12
+
13
+ Add the following line to Gemfile:
14
+
15
+ ```
16
+ gem 'nico_util'
17
+ ```
18
+
19
+ and run `bundle install` from your shell.
20
+
21
+ Or do the following:
22
+
23
+ ```
24
+ $ gem install nico_util
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require 'nico_util'
31
+
32
+ # write your account of niconico
33
+ email = 'niconico@example.com'
34
+ pass = 'password'
35
+
36
+ # login
37
+ nico = NicoUtil::login email, pass
38
+
39
+ # get and show comments of a video
40
+ comments = nico.video('sm1097445').comments
41
+ comments.each do |comment|
42
+ p comment
43
+ end
44
+
45
+ # connect to a live and show data
46
+ nico.live('lv242616226').connect do |status, data|
47
+ case status
48
+ when :comment
49
+ puts data
50
+ when :command
51
+ puts data
52
+ when :disconnect
53
+ puts 'disconnect'
54
+ end
55
+ end
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it!
61
+ 2. Create your feature branch: `git checkout -b my-new-feature`
62
+ 3. Commit your changes: `git commit -am 'Add some feature'`
63
+ 4. Push to the branch: `git push origin my-new-feature`
64
+ 5. Submit a pull request :D
65
+
66
+ ## License
67
+
68
+ NicoUtil is released under the MIT license. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nico_util"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/live.rb ADDED
@@ -0,0 +1,73 @@
1
+ class NicoUtil::Niconico
2
+ def live live_id
3
+ Live.new self, live_id
4
+ end
5
+
6
+ class Live
7
+ def initialize owner, live_id
8
+ @owner = owner
9
+ @id = live_id
10
+ end
11
+
12
+ def playerstatus
13
+ return @playerstatus if @playerstatus
14
+
15
+ host = 'live.nicovideo.jp'
16
+ path = "/api/getplayerstatus?v=#{@id}"
17
+
18
+ response = Net::HTTP.new(host).start do |http|
19
+ request = Net::HTTP::Get.new(path)
20
+ request['cookie'] = @owner.cookie
21
+ http.request(request)
22
+ end
23
+
24
+ playerstatus = {}
25
+ body = response.body.force_encoding('utf-8')
26
+ doc = REXML::Document.new(body).elements['getplayerstatus']
27
+ # stream = doc.elements['stream']
28
+ # title = stream.elements['title'].text
29
+ # desc = stream.elements['description'].text
30
+ ms = doc.elements['ms']
31
+ playerstatus[:addr] = ms.elements['addr'].text
32
+ playerstatus[:port] = ms.elements['port'].text
33
+ playerstatus[:thread_id] = ms.elements['thread'].text
34
+ @playerstatus = playerstatus
35
+ end
36
+
37
+ def connect
38
+ info = playerstatus()
39
+
40
+ TCPSocket.open(info[:addr], info[:port].to_i) do |sock|
41
+ ticket = "<thread thread=\"#{info[:thread_id]}\" version=\"20061206\" res_from=\"-1000\"/>\0"
42
+ sock.write(ticket)
43
+ # 最初の1回にいらないデータが来る
44
+ p sock.gets("\0")
45
+ while true
46
+ stream = sock.gets("\0")
47
+ if stream.index("\0") == stream.length - 1
48
+ stream = stream[0..-2]
49
+ end
50
+ if stream
51
+ # next if stream =~ /ifseetno/ # 一般会員を追い出した
52
+ # next if stream =~ /panel clear/ # 運営が表示したパネルの削除
53
+ # next if stream =~ /\/play/ # クルーズなどで動画を再生する
54
+ if stream =~ /chat/
55
+ stream = REXML::Document.new(stream).elements['chat']
56
+ if stream
57
+ if stream.text[0] == '/'
58
+ yield :command, stream.text
59
+ else
60
+ yield :comment, stream.text
61
+ end
62
+ end
63
+ elsif stream =~ /\/disconnect/
64
+ yield :disconnect, nil
65
+ break
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module NicoUtil
2
+ VERSION = "0.0.1"
3
+ end
data/lib/nico_util.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'openssl'
4
+ require 'rexml/document'
5
+
6
+ # ニコ動コメント取得
7
+ # https://blog.nanoway.net/web/nicovideo-comment-api
8
+
9
+ # ニコ生コメント取得
10
+ # http://pita.s374.xrea.com/cms/75/
11
+
12
+ module NicoUtil
13
+ def self.login email, pass
14
+ Niconico.new email, pass
15
+ end
16
+
17
+ class Niconico
18
+ class AuthenticationError < StandardError; end
19
+
20
+ def initialize email, pass
21
+ @cookie = nil
22
+ @email = email
23
+ @pass = pass
24
+
25
+ login
26
+ end
27
+
28
+ def cookie
29
+ login if @cookie == nil
30
+ @cookie
31
+ end
32
+
33
+ def login
34
+ host = 'secure.nicovideo.jp'
35
+ path = '/secure/login?site=niconico'
36
+ body = "mail=#{@email}&password=#{@pass}"
37
+
38
+ https = Net::HTTP.new host, 443
39
+ https.use_ssl = true
40
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
41
+ response = https.start do |https|
42
+ https.post path, body
43
+ end
44
+
45
+ @cookie = nil
46
+ response['set-cookie'].split('; ').each do |st|
47
+ if idx=st.index('user_session_')
48
+ @cookie = "user_session=#{st[idx..-1]}"
49
+ break
50
+ end
51
+ end
52
+
53
+ unless @cookie
54
+ puts 'Invalid email or password'
55
+ end
56
+ end
57
+
58
+ def logged_in?
59
+ @cookie != nil
60
+ end
61
+
62
+ def check_logged_in!
63
+ unless logged_in?
64
+ raise AuthenticationError, "You are not authorized..."
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+
71
+ require_relative 'video.rb'
72
+ require_relative 'live.rb'
73
+
data/lib/video.rb ADDED
@@ -0,0 +1,68 @@
1
+ class NicoUtil::Niconico
2
+ def video video_id
3
+ Video.new self, video_id
4
+ end
5
+
6
+ class Video
7
+ def initialize owner, video_id
8
+ @owner = owner
9
+ @id = video_id
10
+ end
11
+
12
+ def flv_info
13
+ return @flv_info if @flv_info
14
+
15
+ @owner.check_logged_in!
16
+
17
+ host = 'flapi.nicovideo.jp'
18
+ path = "/api/getflv/#{@id}"
19
+
20
+ response = Net::HTTP.new(host).start do |http|
21
+ request = Net::HTTP::Get.new(path)
22
+ request['cookie'] = @owner.cookie
23
+ http.request(request)
24
+ end
25
+
26
+ flv_info = {}
27
+ response.body.split('&').each do |st|
28
+ stt = st.split('=')
29
+ flv_info[stt[0].to_sym] = stt[1]
30
+ end
31
+ flv_info[:ms] = URI.unescape flv_info[:ms]
32
+
33
+ @flv_info = flv_info
34
+ end
35
+
36
+ def comments limit_max = 1000
37
+ return @comments if @comments && limit_max == 1000
38
+
39
+ info = flv_info()
40
+ uri = URI.parse info[:ms]
41
+
42
+ response = Net::HTTP.new(uri.host).start { |http|
43
+ request = Net::HTTP::Post.new(uri.path)
44
+ thread_id = info[:thread_id]
45
+ version = '20061206' # 20090904
46
+ language = '0' # 1 or 2
47
+ frk = 1 # 1:投稿者コメントあり
48
+ term = '0-30:100,1000' # 30分の内、1分間最大100コメ、直近で1000コメ
49
+ request.body =
50
+ "<packet>
51
+ <thread thread='#{thread_id}' version='#{version}' scores='1' nicoru='1' language='#{language}' with_global='1'/>
52
+ <thread_leaves thread='#{thread_id}' scores='1' nicoru='1' language='0'>#{term}</thread_leaves>
53
+ <thread thread='#{thread_id}' version='#{version}' res_from='-#{limit_max}' fork='#{frk}'/>
54
+ </packet>"
55
+ http.request(request)
56
+ }
57
+
58
+ comments = []
59
+ xml = response.body.gsub('</chat>', "</chat>\n")
60
+ xml.split("\n").each do |line|
61
+ line.gsub! /<.*?>/, ''
62
+ comments << line.force_encoding('utf-8') if line.length > 0
63
+ end
64
+
65
+ @comments = comments
66
+ end
67
+ end
68
+ end
data/nico_util.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nico_util/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nico_util"
8
+ spec.version = NicoUtil::VERSION
9
+ spec.authors = ["Tatsuya Tanaka (tattn)"]
10
+ spec.email = ["tatsuyars@yahoo.co.jp"]
11
+
12
+ spec.summary = %q{Utility APIs for niconico}
13
+ spec.description = %q{Utility APIs for niconico}
14
+ spec.homepage = "https://github.com/tattn/nico_util"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nico_util
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tatsuya Tanaka (tattn)
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Utility APIs for niconico
56
+ email:
57
+ - tatsuyars@yahoo.co.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/live.rb
72
+ - lib/nico_util.rb
73
+ - lib/nico_util/version.rb
74
+ - lib/video.rb
75
+ - nico_util.gemspec
76
+ homepage: https://github.com/tattn/nico_util
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.5.0
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Utility APIs for niconico
100
+ test_files: []