niconico_search 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/niconico_search.rb +1 -52
- data/lib/niconico_search/client.rb +55 -0
- data/lib/niconico_search/result.rb +1 -1
- data/lib/niconico_search/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa687b5c0420ee74eaa40ac580a70e5b04639a01
|
4
|
+
data.tar.gz: 89204dc79c4ef00a0d1e0ab61e5c6bff569a6f9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44984d241f827a5db9381b7383d60b9b8ba493f33b4847f74ea938e3f87a5f3aca03d9d4d9a9a29712be151ca476fb783c9f749ff1e6589c4958973a590c7e9d
|
7
|
+
data.tar.gz: 5114a132d5ecf08114681c69db3bd9fcfbafad6f1a6f8df16a7fe5ee5a1352c724b3b5b9dddde955772731de4d9948f1251d40e1dfacbee32fb2e462c97b9e86
|
data/README.md
CHANGED
@@ -22,7 +22,7 @@ Or install it yourself as:
|
|
22
22
|
## Usage
|
23
23
|
|
24
24
|
```ruby
|
25
|
-
client = NiconicoSearch.new("your_application_name")
|
25
|
+
client = NiconicoSearch::Client.new("your_application_name")
|
26
26
|
results = nico.search(
|
27
27
|
query: keyword,
|
28
28
|
targets: [:title, :description, :tags],
|
data/lib/niconico_search.rb
CHANGED
@@ -2,55 +2,4 @@
|
|
2
2
|
|
3
3
|
require "niconico_search/version"
|
4
4
|
require "niconico_search/result"
|
5
|
-
require "
|
6
|
-
require "open-uri"
|
7
|
-
|
8
|
-
class ResponseError < StandardError; end
|
9
|
-
|
10
|
-
class NiconicoSearch
|
11
|
-
def initialize(app = nil)
|
12
|
-
@app = app
|
13
|
-
end
|
14
|
-
|
15
|
-
def search(query:, targets:, options: {})
|
16
|
-
fail ArgumentError, "query is required" if query.nil?
|
17
|
-
fail ArgumentError, "target is required" if targets.nil? || targets.length <= 0
|
18
|
-
|
19
|
-
uri = URI.parse("http://api.search.nicovideo.jp/api/v2/snapshot/video/contents/search")
|
20
|
-
uri.query = URI.encode_www_form(build_query(query: query, targets: targets, options: options))
|
21
|
-
res = uri.open(header).read
|
22
|
-
response = JSON.parse(res).deep_symbolize_keys
|
23
|
-
fail ::ResponseError, "HTTP status error #{response[:meta][:status]}" if response[:meta][:status] != 200
|
24
|
-
response[:data].map { |r| NiconicoSearch::Result.new(r) }
|
25
|
-
end
|
26
|
-
|
27
|
-
def build_query(query:, targets:, options:)
|
28
|
-
options[:q] = query
|
29
|
-
options[:targets] = targets.join(",")
|
30
|
-
options = default_options.merge(options)
|
31
|
-
options[:fields] = options[:fields].join(",")
|
32
|
-
options
|
33
|
-
end
|
34
|
-
|
35
|
-
def default_options
|
36
|
-
{
|
37
|
-
fields: [
|
38
|
-
:contentId,
|
39
|
-
:title,
|
40
|
-
:description,
|
41
|
-
:tags,
|
42
|
-
:viewCounter
|
43
|
-
],
|
44
|
-
_sort: "-viewCounter",
|
45
|
-
_context: @app
|
46
|
-
}
|
47
|
-
end
|
48
|
-
|
49
|
-
def header
|
50
|
-
{ "User-Agent" => @app }
|
51
|
-
end
|
52
|
-
|
53
|
-
def parse_targets(targets)
|
54
|
-
targets.join(",")
|
55
|
-
end
|
56
|
-
end
|
5
|
+
require "niconico_search/client"
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# -*- frozen_string_literal: true -*-
|
2
|
+
require "uri"
|
3
|
+
require "open-uri"
|
4
|
+
|
5
|
+
module NiconicoSearch
|
6
|
+
class ResponseError < StandardError; end
|
7
|
+
|
8
|
+
class Client
|
9
|
+
def initialize(app = nil)
|
10
|
+
@app = app
|
11
|
+
end
|
12
|
+
|
13
|
+
def search(query:, targets:, options: {})
|
14
|
+
fail ArgumentError, "query is required" if query.nil?
|
15
|
+
fail ArgumentError, "target is required" if targets.nil? || targets.length <= 0
|
16
|
+
|
17
|
+
uri = URI.parse("http://api.search.nicovideo.jp/api/v2/snapshot/video/contents/search")
|
18
|
+
uri.query = URI.encode_www_form(build_query(query: query, targets: targets, options: options))
|
19
|
+
res = uri.open(header).read
|
20
|
+
response = JSON.parse(res).deep_symbolize_keys
|
21
|
+
fail ::ResponseError, "HTTP status error #{response[:meta][:status]}" if response[:meta][:status] != 200
|
22
|
+
response[:data].map { |r| NiconicoSearch::Result.new(r) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def build_query(query:, targets:, options:)
|
26
|
+
options[:q] = query
|
27
|
+
options[:targets] = targets.join(",")
|
28
|
+
options = default_options.merge(options)
|
29
|
+
options[:fields] = options[:fields].join(",")
|
30
|
+
options
|
31
|
+
end
|
32
|
+
|
33
|
+
def default_options
|
34
|
+
{
|
35
|
+
fields: [
|
36
|
+
:contentId,
|
37
|
+
:title,
|
38
|
+
:description,
|
39
|
+
:tags,
|
40
|
+
:viewCounter
|
41
|
+
],
|
42
|
+
_sort: "-viewCounter",
|
43
|
+
_context: @app
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def header
|
48
|
+
{ "User-Agent" => @app }
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse_targets(targets)
|
52
|
+
targets.join(",")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: niconico_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- h3poteto
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- lib/niconico_search.rb
|
73
|
+
- lib/niconico_search/client.rb
|
73
74
|
- lib/niconico_search/result.rb
|
74
75
|
- lib/niconico_search/version.rb
|
75
76
|
- niconico_search.gemspec
|