waifu2x 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50e1df8ba5bb6c368d96d4b46dd58aa401912a20
4
- data.tar.gz: 2b05f77ed8f83831ae2874ecc6f101e60f1e0cd8
3
+ metadata.gz: bf8325f34e31f4bcb3fbed92ddf27eff4587c4ec
4
+ data.tar.gz: 162f69e37cdba67e0514fa823186692b590a2349
5
5
  SHA512:
6
- metadata.gz: f821376a0163a1c2ff02c1954b5758eda9d20e57449639fadbc764a2d3a1b05b2f8ca994a5a663965f5227964535797869817dd671e43ee3d0d2243f9e0b5cd4
7
- data.tar.gz: 818ec0ddf70ac893dac9b55dbc0f4326fe4587bd5f74f4c7a420a4cc38602a4bef89f15b3e4c97f0ed2e9ab6f9d62215911e5164d59185ffe73e3b7d19b834d8
6
+ metadata.gz: c4f21ede2d6db88f989eeeb985c2eda287f5641d856308d9aa9caadd536f735b20adfac6a38bb6188e94d2fafd81ff286b130f432895d6b509e9be2d69863ce0
7
+ data.tar.gz: cc509bdc1db7fb7e8ba624f85722fdfb300802fdba57a97e71895c7f1cd78aa6416fc9f7a146834dec4de813953e227e2df2d8530eeb77cb0d4abb60d15d27eb
@@ -10,14 +10,18 @@ optparse = OptionParser.new do |opts|
10
10
  opts.separator ''
11
11
  opts.separator 'Options:'
12
12
 
13
- opts.on_tail('-n', '--noise NOISE', 'Noise reduction (0 - none, 1 - low, 2 - high), default: 2') do |noise|
13
+ opts.on_tail('-n', '--noise NOISE', 'Noise reduction (0 - none, 1 - medium, 2 - high), default: 1') do |noise|
14
14
  options[:noise] = noise.to_i
15
15
  end
16
16
 
17
- opts.on_tail('-s', '--scale SCALE', 'Upscaling (0 - none, 1 - 1.6x, 2 - 2x), default: 0') do |scale|
17
+ opts.on_tail('-s', '--scale SCALE', 'Upscaling (1 - none, 2 - 2x), default: 2') do |scale|
18
18
  options[:scale] = scale.to_i
19
19
  end
20
20
 
21
+ opts.on_tail('-r', '--raw RAW', 'Return URL of the converted image (0 - no, 1 - yes), default: 0') do |raw|
22
+ options[:raw] = raw.to_i == 1
23
+ end
24
+
21
25
  opts.on_tail('-h', '--help', 'Prints this help message') do
22
26
  puts opts
23
27
  exit
@@ -56,4 +60,10 @@ end
56
60
  finish_time = Time.now.to_i
57
61
  duration = finish_time - start_time
58
62
 
59
- puts "Upload took ~#{duration} seconds. Created file #{waifu2x}"
63
+ print "Upload took ~#{duration} seconds. "
64
+
65
+ if options[:raw]
66
+ puts "Converted file URL: #{waifu2x}"
67
+ else
68
+ puts "Created file #{waifu2x}"
69
+ end
@@ -1,53 +1,90 @@
1
1
  require 'waifu2x/version'
2
2
  require 'waifu2x/errors'
3
+ require 'open-uri'
3
4
  require 'typhoeus'
4
5
 
5
6
  module Waifu2x
6
7
  USER_AGENT = "Waifu2x Ruby Gem #{Waifu2x::VERSION}".freeze
7
- API_ENDPOINT = 'http://waifu2x.udp.jp/api'.freeze
8
+ API_ENDPOINT = 'https://waifu2x.booru.pics'.freeze
8
9
 
9
- # Converts image from a local file or URL
10
+ # Converts image from a local file or remote URL
10
11
  #
11
12
  # @param [String] source Source of image to convert (url or filename)
12
- # @param [String, nil] output_file Name of output image file (with extension)
13
+ # @param [String, nil] output_file Filename of the converted image file (with extension)
13
14
  # @param [Hash] options
14
- # @option options [Integer] :noise Noise reduction (0 - none, 1 - low, 2 - high), default: 2
15
- # @option options [Integer] :scale Upscaling (0 - none, 1 - 1.6x, 2 - 2x), default: 0
16
- # @option options [Boolean] :raw When true will return a converted image in raw data (binary)
17
- # @return [String] converted image in raw data (binary) or output filename
15
+ # @option options [Integer] :noise Noise reduction (0 - none, 1 - medium, 2 - high), default: 1
16
+ # @option options [Integer] :scale Upscaling (1 - none, 2 - 2x), default: 2
17
+ # @option options [Boolean] :raw When true will return URL of the converted image
18
+ # @return [String] converted image URL or filename
18
19
  def self.convert(source, output_file=nil, options={})
19
- noise = options[:noise] || 2
20
- scale = options[:scale] || 0
21
-
22
- validate_options! noise, scale
23
-
24
- if source.downcase.start_with?('http://', 'https://', 'ftp://')
25
- source_params = { url: source }
20
+ if source =~ /\A(https?|ftp):\/\//i
21
+ converted_url = handle_request(source, options)
26
22
  else
27
23
  begin
28
- source_params = { file: File.open(source) }
29
- rescue
24
+ converted_url = handle_request(File.open(source), options)
25
+ rescue Errno::ENOENT, Errno::EACCES
30
26
  raise InvalidImage, "Can't open image file: #{source}"
31
27
  end
32
28
  end
33
29
 
34
- response = Typhoeus.post(API_ENDPOINT,
35
- headers: { 'User-Agent' => USER_AGENT },
36
- body: { noise: noise, scale: scale }.merge(source_params)
37
- )
38
-
39
- raise InvalidImage, response.body if response.body.include?('ERROR')
40
- raise Waifu2x::ServerError, "Request to Waifu2x API failed with response code: #{response.code}" if response.code != 200
41
-
42
30
  if options[:raw]
43
- response.body
31
+ converted_url
44
32
  else
45
33
  output_file = output_filename(source) if output_file.to_s.strip.empty?
46
- File.write(output_file, response.body)
34
+ IO.copy_stream(open(converted_url), output_file)
47
35
  output_file
48
36
  end
49
37
  end
50
38
 
39
+ # Performs request to the Waifu2x API for image conversion
40
+ #
41
+ # @param [File, String] Image file or URL of an image to convert
42
+ # @param [Hash] options
43
+ # @option options [Integer] :noise Noise reduction (0 - none, 1 - medium, 2 - high), default: 1
44
+ # @option options [Integer] :scale Upscaling (1 - none, 2 - 2x), default: 2
45
+ # @return [String] converted image URL
46
+ def self.handle_request(file_or_url, options)
47
+ noise = options[:noise] || 1
48
+ scale = options[:scale] || 2
49
+
50
+ validate_options! noise, scale
51
+
52
+ default_request_options = {
53
+ followlocation: true,
54
+ headers: { 'User-Agent' => USER_AGENT }
55
+ }
56
+
57
+ request = if file_or_url.is_a?(File)
58
+ Typhoeus::Request.new(
59
+ "#{API_ENDPOINT}/Home/upload",
60
+ default_request_options.merge(
61
+ method: :post,
62
+ body: { denoise: noise, scale: scale, img: file_or_url }
63
+ )
64
+ )
65
+ else
66
+ Typhoeus::Request.new(
67
+ "#{API_ENDPOINT}/Home/fromlink",
68
+ default_request_options.merge(
69
+ method: :get,
70
+ params: { denoise: noise, scale: scale, url: file_or_url }
71
+ )
72
+ )
73
+ end
74
+ response = request.run
75
+
76
+ if response.body.include?('File not found') || response.body.include?('Image file is not of type')
77
+ raise InvalidImage, response.body
78
+ end
79
+
80
+ if response.code != 200
81
+ raise Waifu2x::ServerError, "Request to Waifu2x API failed with response code: #{response.code}"
82
+ end
83
+
84
+ hash = response.effective_url.match(/hash=(\S+)/)[1]
85
+ "#{API_ENDPOINT}/outfiles/#{hash}.jpg"
86
+ end
87
+
51
88
  # @private
52
89
  def self.output_filename(file)
53
90
  file_ext = File.extname(file)
@@ -1,3 +1,3 @@
1
1
  module Waifu2x
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waifu2x
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nihad Abbasov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-06 00:00:00.000000000 Z
11
+ date: 2017-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  requirements: []
122
122
  rubyforge_project:
123
- rubygems_version: 2.5.0
123
+ rubygems_version: 2.6.13
124
124
  signing_key:
125
125
  specification_version: 4
126
126
  summary: Ruby wrapper and CLI for waifu2x