Class: CaptchedToDeath::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*credentials) {|_self| ... } ⇒ Client

Sensible defaults that can be overriden by configuration block:

client = CaptchedToDeath::Client.new do |c|
  c.username = 'username'
  c.password = 'password'
  c.verbose  = true
end

or just:

client = CaptchedToDeath::Client.new('username','password')

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/captched_to_death/client.rb', line 20

def initialize(*credentials)
  @accept  = :json
  @verbose = false
  if credentials.size == 2
    @username = credentials[0].to_s
    @password = credentials[1].to_s
  end
  yield self if block_given?

  RestClient.log = Logger.new(STDOUT) if @verbose
end

Instance Attribute Details

#accept=(value) ⇒ Object (writeonly)

Sets the attribute accept

Parameters:

  • value

    the value to set the attribute accept to.



6
7
8
# File 'lib/captched_to_death/client.rb', line 6

def accept=(value)
  @accept = value
end

#password=(value) ⇒ Object (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



6
7
8
# File 'lib/captched_to_death/client.rb', line 6

def password=(value)
  @password = value
end

#username=(value) ⇒ Object (writeonly)

Sets the attribute username

Parameters:

  • value

    the value to set the attribute username to.



6
7
8
# File 'lib/captched_to_death/client.rb', line 6

def username=(value)
  @username = value
end

#verbose=(value) ⇒ Object (writeonly)

Sets the attribute verbose

Parameters:

  • value

    the value to set the attribute verbose to.



6
7
8
# File 'lib/captched_to_death/client.rb', line 6

def verbose=(value)
  @verbose = value
end

Instance Method Details

#balanceObject

User credit balance, includes account details.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/captched_to_death/client.rb', line 34

def balance
  fail RejectedError if empty_credentials?
  response = RestClient.post "#{API_URI}/user", {
    :username => @username,
    :password => @password
  }, :accept => @accept
  JSON.parse(response) 
rescue RestClient::Exception => e
  case e.http_code
  when 403
    fail RejectedError
  else
    fail ServiceError
  end
end

#captcha(captcha_id) ⇒ Object

Polls for uploaded CAPTCHA status. You don’t have to supply your Death by Captcha credentials this time. Please don’t poll for a CAPTCHA status more than once in a couple of seconds. This is considered abusive and might get you banned.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/captched_to_death/client.rb', line 55

def captcha(captcha_id)
  response = RestClient.get "#{API_URI}/captcha/#{captcha_id}", {:accept => @accept}
  JSON.parse(response)
rescue RestClient::Exception => e
  case e.http_code
  when 404
    fail NotFound 
  when 500
    fail ServiceError
  #503 (Service Temporarily Unavailable) when our service is overloaded (usually around 3:00–6:00 PM EST)
  when 503  
    # not sure 503 is ever sent, but retry if it is
    sleep Server.status['solved_in']
    retry
  else
    raise e
  end
end

#decode(challenge_url, referer = nil, agent = nil) ⇒ Object

Solving a CAPTCHA using Death by Captcha HTTP API requires performing at least two steps.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/captched_to_death/client.rb', line 76

def decode(challenge_url, referer=nil, agent=nil)
  fail RejectedError if empty_credentials?

  response = RestClient.post "#{API_URI}/captcha", {
    :username    => @username,
    :password    => @password,
    :captchafile => captcha_file(challenge_url,referer,agent)
  }, :accept => @accept
  resolved = JSON.parse(response) 
  fail ServiceError, resolved['error'] if resolved.include?('error')  # ie. {"status": 255, "error": "service-overload"}
  begin
    sleep Server.status['solved_in']
    resolved = captcha(resolved['captcha'])
  end while resolved['text'].empty?
  resolved
rescue RestClient::Exception => e
  case e.http_code
  #303 (See Other) CAPTCHA successfully uploaded: Location HTTP header will point to the status page
  when 303
    # RestClient: for result code 303 the redirection will be followed and the request transformed into a get
    # (...so it'll be returned as a 200)
  #403 (Forbidden) credentials were rejected, or you don't have enough credits
  when 403
    fail NoCreditError if balance
  #400 (Bad Request) if your request was not following the specification or not a valid image
  when 400
    fail RejectedError
  #500 (Internal Server Error)
  #503 (Service Temporarily Unavailable) when our service is overloaded (usually around 3:00–6:00 PM EST)
  when 500, 503  
    fail ServiceError, e.http_body
  else
    raise e
  end
end

#report(captcha_id) ⇒ Object

Reports incorrectly solved CAPTCHAs. If you think your CAPTCHA was solved incorrectly, report it to Death by Captcha to get your money back. You’ll get refunded if the CAPTCHA was uploaded less than an hour ago.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/captched_to_death/client.rb', line 116

def report(captcha_id)
  fail RejectedError if empty_credentials?
  response = RestClient.post "#{API_URI}/captcha/#{captcha_id}/report", {
    :username => @username,
    :password => @password,
  }, :accept => @accept
  JSON.parse(response) 
rescue RestClient::Exception => e
  case e.http_code
  when 403
    fail RejectedError
  else
    fail ServiceError
  end
end