Class: Decaptcha::Provider::DeathByCaptcha::Socket

Inherits:
Base
  • Object
show all
Defined in:
lib/decaptcha/provider/death_by_captcha/socket.rb

Constant Summary collapse

API_SERVER_HOST =
'deathbycaptcha.com'
API_SERVER_FIRST_PORT =
8123
API_SERVER_LAST_PORT =
8130
API_VERSION =
'DBC/Ruby v3.0'
SOFTWARE_VENDOR_ID =
0
DEFAULT_TIMEOUT =
60
POLLS_COUNT =
4
POLLS_PERIOD =
15
POLLS_INTERVAL =
5

Instance Attribute Summary

Attributes inherited from Base

#login, #password, #timeout

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods inherited from Base

#mime_type

Constructor Details

This class inherits a constructor from Decaptcha::Provider::DeathByCaptcha::Base

Instance Method Details

#build_response(data) ⇒ Object


132
133
134
135
136
137
138
139
140
# File 'lib/decaptcha/provider/death_by_captcha/socket.rb', line 132

def build_response(data)
  response = Decaptcha::Response.new
  response.id      = data['captcha']
  response.text    = data['text']
  response.invalid = !data['is_correct']
  response.banned  = data['banned']
  response.balance = data['balance']
  response
end

#compile_params(additional_params) ⇒ Object


142
143
144
145
146
147
148
149
150
151
152
# File 'lib/decaptcha/provider/death_by_captcha/socket.rb', line 142

def compile_params(additional_params)
  params = {
     :username    => @login, 
     :password    => Digest::SHA1.hexdigest(@password),
     :is_hashed   => 1,
     :swid        => SOFTWARE_VENDOR_ID,
     :version     => API_VERSION
   }

  params.merge additional_params
end

#delete(response_obj) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/decaptcha/provider/death_by_captcha/socket.rb', line 70

def delete(response_obj)
  params = {
    :username    => @login, 
    :password    => Digest::SHA1.hexdigest(@password),
    :is_hashed   => 1,
    :swid        => SOFTWARE_VENDOR_ID,
    :version     => API_VERSION
  }

  delete_url = "#{response_obj.location}/remove"

  RestClient.post delete_url, params, :accept => :json do |response, request, result|
    if response.code == 200
      response_obj.deleted = true
    else
      raise DeletionError
    end
  end

  response_obj
end

#invalid(response_obj) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/decaptcha/provider/death_by_captcha/socket.rb', line 92

def invalid(response_obj)
  params = {
    :username    => @login, 
    :password    => Digest::SHA1.hexdigest(@password),
    :is_hashed   => 1,
    :swid        => SOFTWARE_VENDOR_ID,
    :version     => API_VERSION
  }

  invalidation_url = "#{response_obj.location}/report"

  RestClient.post invalidation_url, params, :accept => :json do |response, request, result|
    if response.code == 200
      response_obj.invalid = true
    else
      raise InvalidationError, response
    end
  end

  response_obj
end

#make_request(options) ⇒ Object


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/decaptcha/provider/death_by_captcha/socket.rb', line 114

def make_request(options)
  host = TCPSocket.getaddress API_SERVER_HOST
  port = API_SERVER_FIRST_PORT + rand(API_SERVER_LAST_PORT - API_SERVER_FIRST_PORT + 1)
  
  connection = TCPSocket.new(host, port)
  connection.write compile_params(options).to_json
  
  buffer = ""
  
  while data = connection.read(1000)
    buffer = buffer + data
  end
  
  connection.close
  
  JSON.parse buffer rescue nil
end

#result(response_obj) ⇒ Object


59
60
61
62
63
64
65
66
67
68
# File 'lib/decaptcha/provider/death_by_captcha/socket.rb', line 59

def result(response_obj)
  options = { :cmd => 'get_text', :captcha => response_obj.id }
  data = make_request(options)

  if data.nil? || data.empty?
    raise ResultError
  end

  build_response(data)
end

#upload(args = {}) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/decaptcha/provider/death_by_captcha/socket.rb', line 21

def upload(args = {})
  filename = args[:filename]
  
  options = { :cmd => 'upload',
              :captcha => Base64::encode64(File.read(filename)) }
  
  data = make_request(options)
  
  if data.nil? || data.empty?
    raise UploadError
  end

  build_response(data)
end

#upload_and_poll(args = {}) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/decaptcha/provider/death_by_captcha/socket.rb', line 36

def upload_and_poll(args = {})
  user_timeout = args[:timeout] || DEFAULT_TIMEOUT
  response = upload(args)
  kill_time = Time.now + user_timeout

  response = ''
  while (Time.now < kill_time) do
    response = result(response)
    
    if response.text
      break
    elsif response.invalid
      raise InvalidationError, response
    else
      sleep POLLS_PERIOD
    end
  end
  
  # remove...

  response
end