Class: AdsPowerClient
- Inherits:
-
Object
- Object
- AdsPowerClient
- Defined in:
- lib/adspower-client.rb
Constant Summary collapse
- @@drivers =
control over the drivers created, in order to don’t create the same driver twice and don’t generate memory leaks. reference: github.com/leandrosardi/adspower-client/issues/4
{}
Instance Attribute Summary collapse
-
#adspower_default_browser_version ⇒ Object
reference: localapi-doc-en.adspower.com/ reference: localapi-doc-en.adspower.com/docs/Rdw7Iu.
-
#adspower_listener ⇒ Object
reference: localapi-doc-en.adspower.com/ reference: localapi-doc-en.adspower.com/docs/Rdw7Iu.
-
#key ⇒ Object
reference: localapi-doc-en.adspower.com/ reference: localapi-doc-en.adspower.com/docs/Rdw7Iu.
-
#port ⇒ Object
reference: localapi-doc-en.adspower.com/ reference: localapi-doc-en.adspower.com/docs/Rdw7Iu.
-
#server_log ⇒ Object
reference: localapi-doc-en.adspower.com/ reference: localapi-doc-en.adspower.com/docs/Rdw7Iu.
Instance Method Summary collapse
-
#check(id) ⇒ Object
send an GET request to “#url/status” and return if I get the json response[‘status’] == ‘Active’.
-
#create ⇒ Object
send a post request to “#url/api/v1/user/create” and return the response body.
- #delete(id) ⇒ Object
- #driver(id, headless = false) ⇒ Object
-
#html(url) ⇒ Object
create a new profile start the browser visit the page grab the html quit the browser from webdriver stop the broser from adspower delete the profile return the html.
-
#initialize(h = {}) ⇒ AdsPowerClient
constructor
A new instance of AdsPowerClient.
-
#online? ⇒ Boolean
send an GET request to “#url/status”.
-
#server_pids ⇒ Object
return an array of PIDs of all the adspower_global processes running in the local computer.
-
#server_running? ⇒ Boolean
return true if there is any adspower_global process running in the local computer.
-
#server_start(timeout = 10) ⇒ Object
run async command to start adspower server in headless mode.
-
#server_stop ⇒ Object
kill all the adspower_global processes running in the local computer.
-
#start(id, headless = false) ⇒ Object
run the browser return the URL to operate the browser thru selenium.
-
#stop(id) ⇒ Object
run the browser return the URL to operate the browser thru selenium.
Constructor Details
#initialize(h = {}) ⇒ AdsPowerClient
Returns a new instance of AdsPowerClient.
17 18 19 20 21 22 23 24 |
# File 'lib/adspower-client.rb', line 17 def initialize(h={}) self.key = h[:key] # mandatory self.port = h[:port] || '50325' self.server_log = h[:server_log] || '~/adspower-client.log' self.adspower_listener = h[:adspower_listener] || 'http://127.0.0.1' self.adspower_default_browser_version = h[:adspower_default_browser_version] || '116' # self.profiles_created = [] end |
Instance Attribute Details
#adspower_default_browser_version ⇒ Object
reference: localapi-doc-en.adspower.com/ reference: localapi-doc-en.adspower.com/docs/Rdw7Iu
11 12 13 |
# File 'lib/adspower-client.rb', line 11 def adspower_default_browser_version @adspower_default_browser_version end |
#adspower_listener ⇒ Object
reference: localapi-doc-en.adspower.com/ reference: localapi-doc-en.adspower.com/docs/Rdw7Iu
11 12 13 |
# File 'lib/adspower-client.rb', line 11 def adspower_listener @adspower_listener end |
#key ⇒ Object
reference: localapi-doc-en.adspower.com/ reference: localapi-doc-en.adspower.com/docs/Rdw7Iu
11 12 13 |
# File 'lib/adspower-client.rb', line 11 def key @key end |
#port ⇒ Object
reference: localapi-doc-en.adspower.com/ reference: localapi-doc-en.adspower.com/docs/Rdw7Iu
11 12 13 |
# File 'lib/adspower-client.rb', line 11 def port @port end |
#server_log ⇒ Object
reference: localapi-doc-en.adspower.com/ reference: localapi-doc-en.adspower.com/docs/Rdw7Iu
11 12 13 |
# File 'lib/adspower-client.rb', line 11 def server_log @server_log end |
Instance Method Details
#check(id) ⇒ Object
send an GET request to “#url/status” and return if I get the json response[‘status’] == ‘Active’. Otherwise, return false.
reference: localapi-doc-en.adspower.com/docs/YjFggL
164 165 166 167 168 169 170 171 172 |
# File 'lib/adspower-client.rb', line 164 def check(id) url = "#{self.adspower_listener}:#{port}/api/v1/browser/active?user_id=#{id}" uri = URI.parse(url) res = Net::HTTP.get(uri) # show respose body return false if JSON.parse(res)['msg'] != 'success' # return JSON.parse(res)['data']['status'] == 'Active' end |
#create ⇒ Object
send a post request to “#url/api/v1/user/create” and return the response body.
return id of the created user
reference: localapi-doc-en.adspower.com/docs/6DSiws reference: localapi-doc-en.adspower.com/docs/Lb8pOg reference: localapi-doc-en.adspower.com/docs/Awy6Dg
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/adspower-client.rb', line 86 def create url = "#{self.adspower_listener}:#{port}/api/v1/user/create" body = { #'api_key' => self.key, 'group_id' => '0', 'proxyid' => '1', 'fingerprint_config' => { 'browser_kernel_config' => {"version": self.adspower_default_browser_version, "type":"chrome"} } } # api call res = BlackStack::Netting.call_post(url, body) # show respose body ret = JSON.parse(res.body) raise "Error: #{ret.to_s}" if ret['msg'].to_s.downcase != 'success' # add to array of profiles created # self.profiles_created << ret # return id of the created user ret['data']['id'] end |
#delete(id) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/adspower-client.rb', line 107 def delete(id) url = "#{self.adspower_listener}:#{port}/api/v1/user/delete" body = { 'api_key' => self.key, 'user_ids' => [id], } # api call res = BlackStack::Netting.call_post(url, body) # show respose body ret = JSON.parse(res.body) # validation raise "Error: #{ret.to_s}" if ret['msg'].to_s.downcase != 'success' end |
#driver(id, headless = false) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/adspower-client.rb', line 175 def driver(id, headless=false) ret = self.start(id, headless) old = @@drivers[id] # si este driver sigue activo, lo devuelvo return old if old && self.check(id) # Attach test execution to the existing browser # reference: https://zhiminzhan.medium.com/my-innovative-solution-to-test-automation-attach-test-execution-to-the-existing-browser-b90cda3b7d4a url = ret['data']['ws']['selenium'] opts = Selenium::WebDriver::Chrome::Options.new opts.add_option("debuggerAddress", url) # connect to the existing browser # reference: https://localapi-doc-en.adspower.com/docs/K4IsTq driver = Selenium::WebDriver.for(:chrome, :options=>opts) # save the driver @@drivers[id] = driver # return driver end |
#html(url) ⇒ Object
create a new profile start the browser visit the page grab the html quit the browser from webdriver stop the broser from adspower delete the profile return the html
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/adspower-client.rb', line 207 def html(url) ret = { :profile_id => nil, :html => nil, :status => 'success', } id = nil html = nil begin # create the profile sleep(1) # Avoid the "Too many request per second" error id = self.create # update the result ret[:profile_id] = id # start the profile and attach the driver driver = self.driver(id) # get html driver.get(url) html = driver.page_source # update the result ret[:html] = html # stop the profile sleep(1) # Avoid the "Too many request per second" error driver.quit self.stop(id) # delete the profile sleep(1) # Avoid the "Too many request per second" error self.delete(id) # reset id id = nil rescue => e # stop and delete current profile if id sleep(1) # Avoid the "Too many request per second" error self.stop(id) sleep(1) # Avoid the "Too many request per second" error driver.quit self.delete(id) if id end # if id # inform the exception ret[:status] = e.to_s # # process interruption # rescue SignalException, SystemExit, Interrupt => e # if id # sleep(1) # Avoid the "Too many request per second" error # self.stop(id) # sleep(1) # Avoid the "Too many request per second" error # driver.quit # self.delete(id) if id # end # if id end # return ret end |
#online? ⇒ Boolean
send an GET request to “#url/status”. Return true if it responded successfully.
reference: localapi-doc-en.adspower.com/docs/6DSiws
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/adspower-client.rb', line 65 def online? begin url = "#{self.adspower_listener}:#{port}/status" uri = URI.parse(url) res = Net::HTTP.get(uri) # show respose body return JSON.parse(res)['msg'] == 'success' rescue => e return false end end |
#server_pids ⇒ Object
return an array of PIDs of all the adspower_global processes running in the local computer.
27 28 29 |
# File 'lib/adspower-client.rb', line 27 def server_pids `ps aux | grep "adspower_global" | grep -v grep | awk '{print $2}'`.split("\n") end |
#server_running? ⇒ Boolean
return true if there is any adspower_global process running in the local computer.
32 33 34 |
# File 'lib/adspower-client.rb', line 32 def server_running? self.server_pids.size > 0 end |
#server_start(timeout = 10) ⇒ Object
run async command to start adspower server in headless mode. wait up to 10 seconds to start the server, or raise an exception.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/adspower-client.rb', line 38 def server_start(timeout=10) `/usr/bin/adspower_global --headless=true --api-key=#{ADSPOWER_API_KEY} --api-port=#{ADSPOWER_PORT} > #{self.server_log} 2>&1 &` # wait up to 10 seconds to start the server timeout.times do break if self.server_running? sleep(1) end # add a delay of 5 more seconds sleep(5) # raise an exception if the server is not running raise "Error: the server is not running" if self.server_running? == false return end |
#server_stop ⇒ Object
kill all the adspower_global processes running in the local computer.
53 54 55 56 57 58 |
# File 'lib/adspower-client.rb', line 53 def server_stop self.server_pids.each { |pid| `kill -9 #{pid}` } return end |
#start(id, headless = false) ⇒ Object
run the browser return the URL to operate the browser thru selenium
reference: localapi-doc-en.adspower.com/docs/FFMFMf
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/adspower-client.rb', line 126 def start(id, headless=false) url = "#{self.adspower_listener}:#{port}/api/v1/browser/start?user_id=#{id}&headless=#{headless ? '1' : '0'}" uri = URI.parse(url) res = Net::HTTP.get(uri) # show respose bo ret = JSON.parse(res) raise "Error: #{ret.to_s}" if ret['msg'].to_s.downcase != 'success' # return id of the created user ret end |
#stop(id) ⇒ Object
run the browser return the URL to operate the browser thru selenium
reference: localapi-doc-en.adspower.com/docs/DXam94
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/adspower-client.rb', line 142 def stop(id) # if the profile is running with driver, kill chromedriver if @@drivers[id] && self.check(id) @@drivers[id].quit @@drivers[id] = nil end uri = URI.parse("#{self.adspower_listener}:#{port}/api/v1/browser/stop?user_id=#{id}") res = Net::HTTP.get(uri) # show respose body ret = JSON.parse(res) raise "Error: #{ret.to_s}" if ret['msg'].to_s.downcase != 'success' # return id of the created user ret end |