iqdb-client 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 11360f6265de2f8c156bdc7560635e7cea698f034db53f9caf33e36fe6b04dbe
4
+ data.tar.gz: a37d86679df2f07b1dde44bb0359e0770f9d51765862509e49c9c6f13b56fd8b
5
+ SHA512:
6
+ metadata.gz: 5af89a03c25301b3bff4fb24ae8fe315e814af17ffbc8ed6c597e0d8744c8ec5ee3ce7388b64f94c9f05d35b959dd5c1ba9597caa7aaa94a4deb22dd91ba725f
7
+ data.tar.gz: 358fcc8ceb3613a69d0f57a5b404c6b791c23ca28a5748805bedaff867387e446c0a22939007d1f11e2047c48545931bfb8596bd7bc7606f2f2b22d8d2a3bb02
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in iqdb-client.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'faraday'
9
+ gem 'nokogiri'
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ iqdb-client (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ faraday (1.0.1)
11
+ multipart-post (>= 1.2, < 3)
12
+ mini_portile2 (2.4.0)
13
+ multipart-post (2.1.1)
14
+ nokogiri (1.10.5)
15
+ mini_portile2 (~> 2.4.0)
16
+ rake (12.3.2)
17
+ rspec (3.9.0)
18
+ rspec-core (~> 3.9.0)
19
+ rspec-expectations (~> 3.9.0)
20
+ rspec-mocks (~> 3.9.0)
21
+ rspec-core (3.9.3)
22
+ rspec-support (~> 3.9.3)
23
+ rspec-expectations (3.9.2)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.9.0)
26
+ rspec-mocks (3.9.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.9.0)
29
+ rspec-support (3.9.3)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ faraday
36
+ iqdb-client!
37
+ nokogiri
38
+ rake (~> 12.0)
39
+ rspec (~> 3.0)
40
+
41
+ BUNDLED WITH
42
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Дмитриев Михаил Евгеньевич
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.
@@ -0,0 +1,52 @@
1
+ # Iqdb::Client
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/iqdb/client`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'iqdb-client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install iqdb-client
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+
25
+ require 'iqdb/client'
26
+
27
+ iqdb_search = Iqdb::Client.new(service: Iqdb::Client::OFFICIAL_2D)
28
+ result = iqdb_search.from_link('https://iqdb.org/konachan/2/2/e/22e339719f511b89310291f2d5582ca1.jpg')
29
+
30
+ if result.success?
31
+ puts "\n BEST MATCH:\n #{result.best_match.info}"
32
+ puts "\n ADDITIONAL MATCHES:\n #{result.additional_matches.map(&:info).join("\n")}"
33
+ else
34
+ puts "\n SUCCESS: FALSE"
35
+ puts "\n POSSIBLE MATCHES:\n #{result.possible_matches.map(&:info).join("\n")}"
36
+ end
37
+ ```
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Odebe/iqdb-client.
48
+
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "iqdb/client"
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(__FILE__)
@@ -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
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/iqdb/client/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "iqdb-client"
5
+ spec.version = Iqdb::Client::VERSION
6
+ spec.authors = ["Mihail 'Odebe' Dmitriev"]
7
+ spec.email = ["derpiranha@gmail.com"]
8
+
9
+ spec.summary = %q{IQDB adapter}
10
+ spec.homepage = "https://github.com/Odebe/iqdb-client"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/Odebe/iqdb-client"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+
26
+ spec.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,26 @@
1
+ require_relative "./client/version.rb"
2
+ require_relative "./client/engine.rb"
3
+ require_relative "./client/parser/response.rb"
4
+ require_relative "./client/parser/result.rb"
5
+ require_relative "./client/parser/alt.rb"
6
+
7
+ require 'faraday'
8
+ require 'nokogiri'
9
+
10
+ module Iqdb
11
+ module Client
12
+ OFFICIAL_2D = 'https://www.iqdb.org/'.freeze
13
+ OFFICIAL_3D = 'https://3d.iqdb.org/'.freeze
14
+
15
+ def self.new(service: OFFICIAL_2D)
16
+ faraday = Faraday.new(service) do |f|
17
+ f.use Faraday::Response::RaiseError
18
+ f.request :multipart
19
+ end
20
+
21
+ Engine.new(http: faraday)
22
+ end
23
+ end
24
+ end
25
+
26
+
@@ -0,0 +1,34 @@
1
+ module Iqdb
2
+ module Client
3
+ class Engine
4
+ attr_reader :service_adapter
5
+
6
+ def initialize(http:)
7
+ @service_adapter = http
8
+ end
9
+
10
+ def from_link(link)
11
+ Parser::Response.new(send(url: link))
12
+ end
13
+
14
+ # def from_file(io_path)
15
+ # response_body = send(file: Faraday::UploadIO.new(io_path, 'image/jpeg'))
16
+ # Parser::Response.new(response_body)
17
+ # end
18
+
19
+ private
20
+
21
+ def send(params)
22
+ # TODO: settings
23
+ req_body = params # .merge(service: Array.new(13, 1))
24
+
25
+ response = service_adapter.post('/') do |req|
26
+ req.headers['Content-Type'] = 'multipart/form-data'
27
+ req.body = req_body
28
+ end
29
+
30
+ response.body
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ module Iqdb
2
+ module Client
3
+ module Parser
4
+ class Alt
5
+ attr_reader :str
6
+
7
+ def initialize(str)
8
+ @str = str
9
+ end
10
+
11
+ def tags
12
+ @tags ||= str.scan(/Tags:(.+)$/).then { |m| m.empty? ? [] : m[0][0].split(' ') }
13
+ end
14
+
15
+ def rating
16
+ @rating ||= str.scan(/Rating:\s(\w)\s/).then { |m| m.empty? ? '' : m[0][0] }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ module Iqdb
2
+ module Client
3
+ module Parser
4
+ class Response
5
+ def initialize(html_text)
6
+ @page = Nokogiri::HTML.parse(html_text)
7
+ end
8
+
9
+ def success?
10
+ @success ||= find_page_by_text('No relevant matches').first.nil?
11
+ end
12
+
13
+ def best_match
14
+ @best_match ||= Result.new(find_page_by_text('Best match').first)
15
+ end
16
+
17
+ def additional_matches
18
+ @additional_matches ||= find_page_by_text('Additional match').map { |match| Result.new(match) }
19
+ end
20
+
21
+ def possible_matches
22
+ @possible_matches ||= find_page_by_text('Possible match').map { |match| Result.new(match) }
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :page
28
+
29
+ def find_page_by_text(text)
30
+ pages.xpath(".//table[.//th[text() = \"#{text}\"]]")
31
+ end
32
+
33
+ def pages
34
+ @pages ||= @page.xpath('//div[@id="pages"]')
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,25 @@
1
+ module Iqdb
2
+ module Client
3
+ module Parser
4
+ class Result
5
+ attr_reader :nokogiri_obj
6
+
7
+ def initialize(nokogiri_obj)
8
+ @nokogiri_obj = nokogiri_obj
9
+ end
10
+
11
+ def link
12
+ @link ||= @nokogiri_obj.xpath('.//a[1]/@href').text
13
+ end
14
+
15
+ def alt
16
+ @alt ||= Alt.new(@nokogiri_obj.xpath('.//td[@class="image"]//img[1]/@alt').text)
17
+ end
18
+
19
+ def info
20
+ { link: link, tags: alt.tags, rating: alt.rating }
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Iqdb
2
+ module Client
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iqdb-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mihail 'Odebe' Dmitriev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - derpiranha@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - iqdb-client.gemspec
31
+ - lib/iqdb/client.rb
32
+ - lib/iqdb/client/engine.rb
33
+ - lib/iqdb/client/parser/alt.rb
34
+ - lib/iqdb/client/parser/response.rb
35
+ - lib/iqdb/client/parser/result.rb
36
+ - lib/iqdb/client/version.rb
37
+ homepage: https://github.com/Odebe/iqdb-client
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/Odebe/iqdb-client
42
+ source_code_uri: https://github.com/Odebe/iqdb-client
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.3.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.0.3
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: IQDB adapter
62
+ test_files: []