Class: AdsPowerClient

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ AdsPowerClient

Returns a new instance of AdsPowerClient.



17
18
19
20
21
22
# File 'lib/adspower-client.rb', line 17

def initialize(h={})
    self.key = h[:key] # mandatory
    self.adspower_listener = h[:adspower_listener] || 'http://127.0.0.1:50325'
    self.adspower_default_browser_version = h[:adspower_default_browser_version] || '116'
#        self.profiles_created = []
end

Instance Attribute Details

#adspower_default_browser_versionObject



11
12
13
# File 'lib/adspower-client.rb', line 11

def adspower_default_browser_version
  @adspower_default_browser_version
end

#adspower_listenerObject



11
12
13
# File 'lib/adspower-client.rb', line 11

def adspower_listener
  @adspower_listener
end

#keyObject



11
12
13
# File 'lib/adspower-client.rb', line 11

def key
  @key
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



121
122
123
124
125
126
127
128
129
# File 'lib/adspower-client.rb', line 121

def check(id)
    url = "#{self.adspower_listener}/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

#createObject

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/adspower-client.rb', line 50

def create
    url = "#{self.adspower_listener}/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



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/adspower-client.rb', line 71

def delete(id)
    url = "#{self.adspower_listener}/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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/adspower-client.rb', line 132

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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/adspower-client.rb', line 164

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

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/adspower-client.rb', line 29

def online?
    begin
        url = "#{self.adspower_listener}/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

#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



90
91
92
93
94
95
96
97
98
# File 'lib/adspower-client.rb', line 90

def start(id, headless=false)
    uri = URI.parse("#{self.adspower_listener}/api/v1/browser/start?user_id=#{id}&headless=#{headless ? '1' : '0'}'}")
    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



105
106
107
108
109
110
111
112
113
# File 'lib/adspower-client.rb', line 105

def stop(id)
    uri = URI.parse("#{self.adspower_listener}/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