nicosa 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d1a62e5d3c72a6cc3642bc53fec7b48041d7acf1
4
+ data.tar.gz: 313bcb9e09ab2191de8421e0ffcf327fc37ad636
5
+ SHA512:
6
+ metadata.gz: da582bd85101ced5b40c97167eafc7cbca2fd9079223b07150b65f4a59ca2f71bfe0b2d0b05f403a99a111f9342b9cf610f7a71ef5ff602e3671b84de06598e2
7
+ data.tar.gz: e144e93e34909d0720d4511d83843f11c0c8e9d52f04e5a6600bf57607dbbdf355fe2f83061671bdb5e8cf1370c9ab783115cf32e22c2273d73a900af2066da8
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/.rubocop.yml ADDED
@@ -0,0 +1,31 @@
1
+ AllCops:
2
+ Exclude:
3
+ - tmp/**/*
4
+ - vendor/**/*
5
+
6
+ SpecialGlobalVars:
7
+ Enabled: false
8
+
9
+ AsciiComments:
10
+ Enabled: false
11
+
12
+ ClassLength:
13
+ Max: 200
14
+
15
+ CyclomaticComplexity:
16
+ Enabled: false
17
+
18
+ Documentation:
19
+ Enabled: false
20
+
21
+ LineLength:
22
+ Max: 120
23
+ Exclude:
24
+ - spec/**/*
25
+
26
+ MethodLength:
27
+ Max: 30
28
+
29
+ TrailingCommaInLiteral:
30
+ EnforcedStyleForMultiline: comma
31
+
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.1
5
+ - 2.2
6
+ - 2.3.0
7
+ - ruby-head
8
+ before_install:
9
+ - gem install bundler
10
+ script:
11
+ - bundle exec rubocop
12
+ - bundle exec rake
13
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nicosa.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Nicosa
2
+
3
+ [![Build Status](https://travis-ci.org/rutan/nicosa.svg?branch=master)](https://travis-ci.org/rutan/nicosa)
4
+
5
+ niconico search API client.
6
+
7
+ - [コンテンツ検索APIドキュメント](http://search.nicovideo.jp/docs/api/search.html)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'nicosa'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install nicosa
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require 'nicosa'
29
+
30
+ client = Nicosa::Client.new(service: :video, user_agent: 'TODO: Your Address')
31
+ # or
32
+ client = Nicosa::Services::Video.new(user_agent: 'TODO: Your Address')
33
+
34
+ response = client.search('keyword') # => Nicosa::Response
35
+ response.each do |n|
36
+ puts n.title
37
+ puts n.url
38
+ end
39
+ ```
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rutan/nicosa.
44
+
45
+ ## License
46
+ MIT
47
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'nicosa'
5
+ require 'pry'
6
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/nicosa.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'nicosa/version'
2
+ require 'nicosa/client'
3
+ require 'nicosa/errors'
4
+ require 'nicosa/response'
5
+
6
+ require 'nicosa/services/video'
7
+ require 'nicosa/services/live'
8
+ require 'nicosa/services/illust'
9
+ require 'nicosa/services/manga'
10
+ require 'nicosa/services/book'
11
+ require 'nicosa/services/channel'
12
+ require 'nicosa/services/channel_article'
13
+ require 'nicosa/services/news'
@@ -0,0 +1,54 @@
1
+ require 'faraday'
2
+ require 'nicosa/errors'
3
+ require 'nicosa/response'
4
+
5
+ module Nicosa
6
+ class Client
7
+ DEFAULT_ENDPOINT = 'http://api.search.nicovideo.jp'.freeze
8
+
9
+ def initialize(service: :video, user_agent: nil, endpoint: nil)
10
+ @service = service
11
+ @user_agent = user_agent
12
+ @endpoint = endpoint || DEFAULT_ENDPOINT
13
+ raise ArgumentError, 'undefined user_agent' unless @user_agent
14
+ end
15
+
16
+ def search(keyword, params = {})
17
+ params = default_params.merge(params)
18
+ params[:q] = keyword
19
+ params[:_context] = @user_agent
20
+
21
+ raw = client.get do |req|
22
+ req.url "/api/v2/#{@service}/contents/search", params
23
+ end
24
+ response = Nicosa::Response.new(raw)
25
+
26
+ case response.status
27
+ when 200
28
+ response
29
+ when 400
30
+ Nicosa::Errors::Invalid.new(response)
31
+ else
32
+ Nicosa::Errors::Exception.new(response)
33
+ end
34
+ end
35
+
36
+ def client
37
+ @client ||= Faraday.new(url: @endpoint) do |faraday|
38
+ faraday.request :url_encoded
39
+ faraday.adapter Faraday.default_adapter
40
+ faraday.headers[:user_agent] = @user_agent
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def default_params
47
+ {
48
+ targets: 'title',
49
+ fields: 'contentId',
50
+ _sort: '-startTime',
51
+ }
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,12 @@
1
+ module Nicosa
2
+ module Errors
3
+ class Exception < StandardError
4
+ def initialize(response)
5
+ @response = response
6
+ super(@response.error_message.to_s)
7
+ end
8
+ attr_reader :response
9
+ end
10
+ class Invalid < Exception; end
11
+ end
12
+ end
@@ -0,0 +1,55 @@
1
+ require 'json'
2
+ require 'hashie'
3
+ require 'faraday'
4
+
5
+ module Nicosa
6
+ class Response
7
+ include Enumerable
8
+
9
+ def initialize(response)
10
+ @original = response
11
+ end
12
+
13
+ def status
14
+ @original.status
15
+ end
16
+
17
+ def response
18
+ @response ||=
19
+ begin
20
+ resp = Hashie::Mash.new(JSON.parse(@original.body))
21
+ (resp.data || []).each do |n|
22
+ n.url = guess_url(n['contentId'])
23
+ end
24
+ resp
25
+ end
26
+ end
27
+
28
+ def meta
29
+ response.meta || {}
30
+ end
31
+
32
+ def error_message
33
+ meta['errorMessage']
34
+ end
35
+
36
+ def data
37
+ response.data || []
38
+ end
39
+
40
+ def each
41
+ data.each { |n| yield n }
42
+ end
43
+
44
+ private
45
+
46
+ def guess_url(content_id)
47
+ case content_id.sub(/\d+\z/, '')
48
+ when '_comic'
49
+ "http://seiga.nicovideo.jp/comic/#{content_id.sub('_comic', '')}"
50
+ else
51
+ "http://nico.ms/#{content_id}"
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,19 @@
1
+ module Nicosa
2
+ module Services
3
+ class Book < Nicosa::Client
4
+ def initialize(user_agent: nil)
5
+ super(service: :book, user_agent: user_agent)
6
+ end
7
+
8
+ private
9
+
10
+ def default_params
11
+ {
12
+ targets: 'title,description,tags',
13
+ fields: 'contentId,title,description,tags,viewCounter,mylistCounter,commentCounter,startTime,thumbnailUrl',
14
+ _sort: '-startTime',
15
+ }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Nicosa
2
+ module Services
3
+ class Channel < Nicosa::Client
4
+ def initialize(user_agent: nil)
5
+ super(service: :channel, user_agent: user_agent)
6
+ end
7
+
8
+ private
9
+
10
+ def default_params
11
+ {
12
+ targets: 'title,description,tags',
13
+ fields: 'contentId,title,description,tags,startTime,thumbnailUrl',
14
+ _sort: '-startTime',
15
+ }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ module Nicosa
2
+ module Services
3
+ class ChannelArticle < Nicosa::Client
4
+ def initialize(user_agent: nil)
5
+ super(service: :channelarticle, user_agent: user_agent)
6
+ end
7
+
8
+ private
9
+
10
+ def default_params
11
+ {
12
+ targets: 'title,description,tags',
13
+ fields: 'contentId,title,description,tags,mylistCounter,commentCounter,startTime,thumbnailUrl',
14
+ _sort: '-startTime',
15
+ }
16
+ end
17
+ end
18
+
19
+ class Blomaga < ChannelArticle; end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ module Nicosa
2
+ module Services
3
+ class Illust < Nicosa::Client
4
+ def initialize(user_agent: nil)
5
+ super(service: :illust, user_agent: user_agent)
6
+ end
7
+
8
+ private
9
+
10
+ def default_params
11
+ {
12
+ targets: 'title,description,tags',
13
+ fields: %w(
14
+ contentId
15
+ title
16
+ description
17
+ tags
18
+ categoryTags
19
+ viewCounter
20
+ mylistCounter
21
+ commentCounter
22
+ startTime
23
+ thumbnailUrl
24
+ ).join(','),
25
+ _sort: '-startTime',
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,32 @@
1
+ module Nicosa
2
+ module Services
3
+ class Live < Nicosa::Client
4
+ def initialize(user_agent: nil)
5
+ super(service: :live, user_agent: user_agent)
6
+ end
7
+
8
+ private
9
+
10
+ def default_params
11
+ {
12
+ targets: 'title,description,tags',
13
+ fields: %w(
14
+ contentId
15
+ title
16
+ description
17
+ tags
18
+ categoryTags
19
+ viewCounter
20
+ commentCounter
21
+ startTime
22
+ thumbnailUrl
23
+ communityIcon
24
+ scoreTimeshiftReserved
25
+ liveStatus
26
+ ).join(','),
27
+ _sort: '-startTime',
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ module Nicosa
2
+ module Services
3
+ class Manga < Nicosa::Client
4
+ def initialize(user_agent: nil)
5
+ super(service: :manga, user_agent: user_agent)
6
+ end
7
+
8
+ private
9
+
10
+ def default_params
11
+ {
12
+ targets: 'title,description,tags',
13
+ fields: %w(
14
+ contentId
15
+ title
16
+ description
17
+ tags
18
+ categoryTags
19
+ viewCounter
20
+ mylistCounter
21
+ commentCounter
22
+ startTime
23
+ thumbnailUrl
24
+ ).join(','),
25
+ _sort: '-startTime',
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ module Nicosa
2
+ module Services
3
+ class News < Nicosa::Client
4
+ def initialize(user_agent: nil)
5
+ super(service: :news, user_agent: user_agent)
6
+ end
7
+
8
+ private
9
+
10
+ def default_params
11
+ {
12
+ targets: 'title,tags',
13
+ fields: 'contentId,title,description,tags,commentCounter,startTime',
14
+ _sort: '-startTime',
15
+ }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ module Nicosa
2
+ module Services
3
+ class Video < Nicosa::Client
4
+ def initialize(user_agent: nil)
5
+ super(service: :video, user_agent: user_agent)
6
+ end
7
+
8
+ private
9
+
10
+ def default_params
11
+ {
12
+ targets: 'title,description,tags',
13
+ fields: %w(
14
+ contentId
15
+ title
16
+ description
17
+ tags
18
+ categoryTags
19
+ viewCounter
20
+ mylistCounter
21
+ commentCounter
22
+ startTime
23
+ thumbnailUrl
24
+ ).join(','),
25
+ _sort: '-startTime',
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Nicosa
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/nicosa.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 'nicosa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'nicosa'
8
+ spec.version = Nicosa::VERSION
9
+ spec.authors = ['ru_shalm']
10
+ spec.email = ['ru_shalm@hazimu.com']
11
+ spec.summary = 'niconico search api client'
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
14
+ spec.bindir = 'exe'
15
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_dependency 'faraday', '~> 0.9'
19
+ spec.add_dependency 'hashie', '~> 3.4'
20
+ spec.add_development_dependency 'bundler', '~> 1.12'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'pry'
23
+ spec.add_development_dependency 'rubocop', '~> 0.40.0'
24
+ spec.add_development_dependency 'test-unit', '~> 3.1.8'
25
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nicosa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ru_shalm
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.40.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.40.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: test-unit
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.1.8
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.1.8
111
+ description:
112
+ email:
113
+ - ru_shalm@hazimu.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rubocop.yml"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - lib/nicosa.rb
127
+ - lib/nicosa/client.rb
128
+ - lib/nicosa/errors.rb
129
+ - lib/nicosa/response.rb
130
+ - lib/nicosa/services/book.rb
131
+ - lib/nicosa/services/channel.rb
132
+ - lib/nicosa/services/channel_article.rb
133
+ - lib/nicosa/services/illust.rb
134
+ - lib/nicosa/services/live.rb
135
+ - lib/nicosa/services/manga.rb
136
+ - lib/nicosa/services/news.rb
137
+ - lib/nicosa/services/video.rb
138
+ - lib/nicosa/version.rb
139
+ - nicosa.gemspec
140
+ homepage:
141
+ licenses: []
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.5.1
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: niconico search api client
163
+ test_files: []