Class: DeathByCaptcha::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/deathbycaptcha/client.rb

Overview

DeathByCaptcha::Client is a common interface inherited by DBC clients like DeathByCaptcha::Client::HTTP and DeathByCaptcha::Client::Socket.

Direct Known Subclasses

HTTP, Socket

Defined Under Namespace

Classes: HTTP, Socket

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, options = {}) ⇒ DeathByCaptcha::Client

Create a DeathByCaptcha client.

Parameters:

  • username (String)

    Username of the DeathByCaptcha account.

  • password (String)

    Password of the DeathByCaptcha account.

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :timeout (Integer) — default: 60

    Seconds before giving up.

  • :polling (Integer) — default: 5

    Seconds for polling the solution.

  • :hostname (String) — default: 'api.dbcapi.me'

    Custom API hostname.



50
51
52
53
54
55
56
# File 'lib/deathbycaptcha/client.rb', line 50

def initialize(username, password, options = {})
  self.username   = username
  self.password   = password
  self.timeout    = options[:timeout] || 60
  self.polling    = options[:polling] || 5
  self.hostname   = options[:hostname] || 'api.dbcapi.me'
end

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



15
16
17
# File 'lib/deathbycaptcha/client.rb', line 15

def hostname
  @hostname
end

#passwordObject

Returns the value of attribute password.



15
16
17
# File 'lib/deathbycaptcha/client.rb', line 15

def password
  @password
end

#pollingObject

Returns the value of attribute polling.



15
16
17
# File 'lib/deathbycaptcha/client.rb', line 15

def polling
  @polling
end

#timeoutObject

Returns the value of attribute timeout.



15
16
17
# File 'lib/deathbycaptcha/client.rb', line 15

def timeout
  @timeout
end

#usernameObject

Returns the value of attribute username.



15
16
17
# File 'lib/deathbycaptcha/client.rb', line 15

def username
  @username
end

Class Method Details

.create(username, password, connection = :http, options = {}) ⇒ DeathByCaptcha::Client

Create a DeathByCaptcha API client

Parameters:

  • username (String)

    Username of the DeathByCaptcha account.

  • password (String)

    Password of the DeathByCaptcha account.

  • connection (Symbol) (defaults to: :http)

    Connection type (:socket, :http)

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :timeout (Integer) — default: 60

    Seconds before giving up.

  • :polling (Integer) — default: 5

    Seconds for polling the solution.

Returns:



28
29
30
31
32
33
34
35
36
37
# File 'lib/deathbycaptcha/client.rb', line 28

def self.create(username, password, connection = :http, options = {})
  case connection
  when :socket
    DeathByCaptcha::Client::Socket.new(username, password, options)
  when :http
    DeathByCaptcha::Client::HTTP.new(username, password, options)
  else
    raise DeathByCaptcha::InvalidClientConnection
  end
end

Instance Method Details

#captcha(captcha_id) ⇒ DeathByCaptcha::Captcha

Retrieve information from an uploaded captcha.

Parameters:

  • captcha_id (Integer)

    Numeric ID of the captcha.

Returns:

Raises:

  • (NotImplementedError)


185
186
187
# File 'lib/deathbycaptcha/client.rb', line 185

def captcha(captcha_id)
  raise NotImplementedError
end

#decode(options = {}) ⇒ DeathByCaptcha::Captcha

Decode the text from an image (i.e. solve a captcha).

Parameters:

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :url (String)

    URL of the image to be decoded.

  • :path (String)

    File path of the image to be decoded.

  • :file (File)

    File instance with image to be decoded.

  • :raw (String)

    Binary content of the image to be decoded.

  • :raw64 (String)

    Binary content encoded in base64 of the image to be decoded.

Returns:



69
70
71
72
73
# File 'lib/deathbycaptcha/client.rb', line 69

def decode(options = {})
  decode!(options)
rescue DeathByCaptcha::Error
  DeathByCaptcha::Captcha.new
end

#decode!(options = {}) ⇒ DeathByCaptcha::Captcha

Decode the text from an image (i.e. solve a captcha).

Parameters:

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :url (String)

    URL of the image to be decoded.

  • :path (String)

    File path of the image to be decoded.

  • :file (File)

    File instance with image to be decoded.

  • :raw (String)

    Binary content of the image to be decoded.

  • :raw64 (String)

    Binary content encoded in base64 of the image to be decoded.

  • :type (String)

    CAPTCHA type.

  • :token_params (String)

    Parameters for token APIs (reCAPTCHA / hCaptcha).

Returns:

Raises:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/deathbycaptcha/client.rb', line 88

def decode!(options = {})
  started_at = Time.now

  # Do not load image data for CAPTCHA types other than "image".
  raw64 = nil
  if ![4, 5, 6, 7].include?(options[:type])
    raw64 = load_captcha(options)
    raise DeathByCaptcha::InvalidCaptcha if raw64.to_s.empty?
  end

  decoded_captcha = self.upload(options.merge(raw64: raw64))

  while decoded_captcha.text.to_s.empty?
    sleep(self.polling)
    decoded_captcha = self.captcha(decoded_captcha.id)
    raise DeathByCaptcha::Timeout if (Time.now - started_at) > self.timeout
  end

  raise DeathByCaptcha::IncorrectSolution if !decoded_captcha.is_correct

  decoded_captcha
end

#decode_fun_captcha(options = {}) ⇒ Object



165
166
167
168
169
# File 'lib/deathbycaptcha/client.rb', line 165

def decode_fun_captcha(options = {})
  decode_fun_captcha!(options)
rescue DeathByCaptcha::Error
  DeathByCaptcha::Captcha.new
end

#decode_fun_captcha!(options = {}) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/deathbycaptcha/client.rb', line 171

def decode_fun_captcha!(options = {})
  options = {
    type:              6, # FunCaptcha
    funcaptcha_params: options.slice(:publickey, :pageurl, :proxy, :proxytype),
  }
  decode!(options)
end

#decode_h_captcha(options = {}) ⇒ Object



151
152
153
154
155
# File 'lib/deathbycaptcha/client.rb', line 151

def decode_h_captcha(options = {})
  decode_recaptcha_h_captcha!(options)
rescue DeathByCaptcha::Error
  DeathByCaptcha::Captcha.new
end

#decode_h_captcha!(options = {}) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/deathbycaptcha/client.rb', line 157

def decode_h_captcha!(options = {})
  options = {
    type:            7, # hCaptcha
    hcaptcha_params: options.slice(:sitekey, :pageurl, :proxy, :proxytype),
  }
  decode!(options)
end

#decode_image(options = {}) ⇒ Object



111
112
113
114
115
# File 'lib/deathbycaptcha/client.rb', line 111

def decode_image(options = {})
  decode!(options)
rescue DeathByCaptcha::Error
  DeathByCaptcha::Captcha.new
end

#decode_image!(options = {}) ⇒ Object



117
118
119
# File 'lib/deathbycaptcha/client.rb', line 117

def decode_image!(options = {})
  decode!(options.slice(:url, :path, :file, :raw, :raw64))
end

#decode_recaptcha_v2(options = {}) ⇒ Object



121
122
123
124
125
# File 'lib/deathbycaptcha/client.rb', line 121

def decode_recaptcha_v2(options = {})
  decode_recaptcha_v2!(options)
rescue DeathByCaptcha::Error
  DeathByCaptcha::Captcha.new
end

#decode_recaptcha_v2!(options = {}) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/deathbycaptcha/client.rb', line 127

def decode_recaptcha_v2!(options = {})
  options = {
    type:         4, # reCAPTCHA v2
    token_params: options.slice(:googlekey, :pageurl, :proxy, :proxytype),
  }
  decode!(options)
end

#decode_recaptcha_v3(options = {}) ⇒ Object



135
136
137
138
139
# File 'lib/deathbycaptcha/client.rb', line 135

def decode_recaptcha_v3(options = {})
  decode_recaptcha_v3!(options)
rescue DeathByCaptcha::Error
  DeathByCaptcha::Captcha.new
end

#decode_recaptcha_v3!(options = {}) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/deathbycaptcha/client.rb', line 141

def decode_recaptcha_v3!(options = {})
  options = {
    type:         5, # reCAPTCHA v3
    token_params: {
      min_score: 0.3,
    }.merge(options.slice(:googlekey, :pageurl, :action, :min_score, :proxy, :proxytype)),
  }
  decode!(options)
end

#report!(captcha_id) ⇒ DeathByCaptcha::Captcha

Report incorrectly solved captcha for refund.

Parameters:

  • captcha_id (Integer)

    Numeric ID of the captcha.

Returns:

Raises:

  • (NotImplementedError)


195
196
197
# File 'lib/deathbycaptcha/client.rb', line 195

def report!(captcha_id)
  raise NotImplementedError
end

#statusDeathByCaptcha::ServerStatus

Retrieve DeathByCaptcha server status.

Returns:

Raises:

  • (NotImplementedError)


211
212
213
# File 'lib/deathbycaptcha/client.rb', line 211

def status
  raise NotImplementedError
end

#upload(raw64) ⇒ DeathByCaptcha::Captcha

Upload a captcha to DeathByCaptcha.

This method will not return the solution. It’s only useful if you want to implement your own “decode” function.

Returns:

Raises:

  • (NotImplementedError)


222
223
224
# File 'lib/deathbycaptcha/client.rb', line 222

def upload(raw64)
  raise NotImplementedError
end

#userDeathByCaptcha::User

Retrieve your user information (which has the current credit balance).

Returns:

Raises:

  • (NotImplementedError)


203
204
205
# File 'lib/deathbycaptcha/client.rb', line 203

def user
  raise NotImplementedError
end