easy_captcha_solver 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/easy_captcha_solver.rb +32 -3
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b1234c024b893c57f76bff082f670d4b9e602b7b
4
- data.tar.gz: fa8c19e2088725784fbbbef9b2dbbaf479f6d636
3
+ metadata.gz: 2c0efaec755c121dae345e90395b94ad7502f222
4
+ data.tar.gz: 4b60d856f1c1db88b72e8a6cd2784806478ad18e
5
5
  SHA512:
6
- metadata.gz: 756f4241d26c040e380c9b05f7a6c94d5e2c5777ca72275356f247eec2eb0d373a254a17ace817987aa0ed39259ab50beadfd09989b5f758f9c0d57ec3962892
7
- data.tar.gz: 594942dc6956ae1736b69937b9399aec3ee4b57d1354798737ae821d9be7fd4ae211222d79786dc69793c2dc4d36072a926fce7a936ac8823cb4dec3b83d058f
6
+ metadata.gz: 0710d76863f1370abdd452a91717e783aa9b09a98a5f2733a1dd28126bffe4a354f638951fbd55b433577f3c282c452beba978eb6d1fdd48f0b4fc4c3ee7fc3e
7
+ data.tar.gz: bcceed3bc41f98006ef498bfb9687255985a6e6417005bf35fb7c32ab70c4ffc2167f4e0f0d35f4a094188d9f9f3b109811c897d5b8843efdd76513db57c2473
@@ -11,12 +11,22 @@ class EasyCaptchaSolver
11
11
  }
12
12
 
13
13
  @image = options[:image_path] if options[:image_path]
14
- @image = agent.get(options[:image_url]).body if options[:image_url]
14
+
15
+ # If URL, save a file instead of trying to solve the captcha from memory because of tesseract limitations with .png images
16
+ if options[:image_url]
17
+ image = agent.get(options[:image_url]).save! "./tmp_image"
18
+
19
+ # Guess image extension and rename tmp file
20
+ @image = "./tmp_image.#{get_image_extension(image)}"
21
+ File.rename( image, @image)
22
+ end
23
+
15
24
  unless @image
16
25
  throw Exception.new "A local image path or a image URL must be provided. Example: easy_c = EasyCaptcha.new( image_url: 'http://www.example.com/captcha_img.jpg')"
17
26
  end
18
27
 
19
- solve_captcha
28
+ solve_captcha ensure File.delete(@image) if options[:image_url]
29
+
20
30
  end
21
31
 
22
32
  private
@@ -27,8 +37,27 @@ class EasyCaptchaSolver
27
37
  e.language = :eng
28
38
  e.blacklist = '|'
29
39
  }
40
+
30
41
  @captcha = e.text_for(@image).strip # => 'ABC'
31
42
  end
32
43
  end
33
44
 
34
-
45
+ def get_image_extension(local_file_path)
46
+ png = Regexp.new("\x89PNG".force_encoding("binary"))
47
+ jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
48
+ jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
49
+ case IO.read(local_file_path, 10)
50
+ when /^GIF8/
51
+ 'gif'
52
+ when /^#{png}/
53
+ 'png'
54
+ when /^#{jpg}/
55
+ 'jpg'
56
+ when /^#{jpg2}/
57
+ 'jpg'
58
+ else
59
+ mime_type = `file #{local_file_path} --mime-type`.gsub("\n", '') # Works on linux and mac
60
+ raise UnprocessableEntity, "unknown file type" if !mime_type
61
+ mime_type.split(':')[1].split('/')[1].gsub('x-', '').gsub(/jpeg/, 'jpg').gsub(/text/, 'txt').gsub(/x-/, '')
62
+ end
63
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_captcha_solver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Rodriguez