Class: BlackStack::BrowserFactory
- Inherits:
-
Object
- Object
- BlackStack::BrowserFactory
- Defined in:
- lib/browserfactory.rb
Constant Summary collapse
- LOCKING_FILENAME =
manejo de concurrencia en la creación de browsers
BlackStack::MyProcess.macaddress
- PROFILE_PID_LIST_FILENAME =
manejo de concurrencia en la creación de browsers
"./browserfactory.%PRFILE_NAME%.list"- PROFILE_PID_LOCK_FILENAME =
manejo de concurrencia en la creación de browsers
"./browserfactory.%PRFILE_NAME%.lock"- CHROME_EXTENSION_DIRECTORY =
File.absolute_path('./chrome_extension')
- DEFAULT_LOAD_TIMEOUT =
360- DEFAULT_CHROMEDRIVER_PATH =
'./chromedriver.exe'- TYPE_PHANTOMJS =
discontinued!
0- TYPE_FIREFOX =
1- TYPE_CHROME =
2- @@fd =
File.open(LOCKING_FILENAME,"w")
- @@fd_profile =
nil- @@driver =
nil- @@browser =
nil- @@pid =
nil- @@profile_name =
nil- @@arAgents =
NOTA: esta lista debe estar permitida por LinkedIn. De caso contrario, aparecera el mensaje “Upgrad your browser” Se puede obtener una lista actualizada de este sitio: techblog.willshouse.com/2012/01/03/most-common-user-agents/
[ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063", ]
Class Method Summary collapse
- .addPidToProfileList(profile_name, pid) ⇒ Object
- .agents ⇒ Object
- .chrome(h) ⇒ Object
- .chrome_version ⇒ Object
- .chromedriver_version(filename = nil) ⇒ Object
- .destroy ⇒ Object
- .getPid ⇒ Object
- .isCreated ⇒ Object
- .isCreated? ⇒ Boolean
- .launch_chrome(proxy = nil, load_timeout = DEFAULT_LOAD_TIMEOUT, user_agent = nil, profile_name = nil, chromedriver_path = nil, chromedriver_signature = nil) ⇒ Object
- .lock ⇒ Object
- .lockProfileList ⇒ Object
- .mimic(mimic_profile_id) ⇒ Object
- .readPidToProfileList(profile_name) ⇒ Object
- .release ⇒ Object
- .releaseProfileList ⇒ Object
- .removePidToProfileList(profile_name, pid) ⇒ Object
- .reservation_id ⇒ Object
-
.screenshot(filename) ⇒ Object
toma una captura del area visible de la pagina mas informacion: stackoverflow.com/questions/25543651/screenshot-of-entire-webpage-using-selenium-webdriver-rc-rails.
- .updateProfileList ⇒ Object
Class Method Details
.addPidToProfileList(profile_name, pid) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/browserfactory.rb', line 146 def self.addPidToProfileList(profile_name, pid) fname = PROFILE_PID_LIST_FILENAME.gsub('%PRFILE_NAME%', profile_name) f = File.open(fname, 'a') f.write("#{pid},") f.close end |
.agents ⇒ Object
225 226 227 |
# File 'lib/browserfactory.rb', line 225 def self.agents() return @@arAgents end |
.chrome(h) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/browserfactory.rb', line 307 def self.chrome(h) # TODO: check if h is a hash # TODO: check the variable type of each param # TODO: check mandatory params self.launch_chrome( h[:proxy], h[:load_timeout].nil? ? DEFAULT_LOAD_TIMEOUT : h[:load_timeout], h[:user_agent], h[:profile_name], h[:chromedriver_path], h[:chromedriver_signature] ) end |
.chrome_version ⇒ Object
290 291 292 293 294 295 |
# File 'lib/browserfactory.rb', line 290 def self.chrome_version res = `reg query \"HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon\" /v version`.scan(/REG_SZ (.*)$/).first return nil if res.nil? return res[0] if res.size > 0 return nil end |
.chromedriver_version(filename = nil) ⇒ Object
298 299 300 301 302 303 304 |
# File 'lib/browserfactory.rb', line 298 def self.chromedriver_version(filename=nil) res = `chromedriver -v`.scan(/ChromeDriver\s(.*)\s\(/).first if filename.nil? res = `#{filename} -v`.scan(/ChromeDriver\s(.*)\s\(/).first if !filename.nil? return nil if res.nil? return res[0] if res.size > 0 return nil end |
.destroy ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/browserfactory.rb', line 266 def self.destroy() self.lockProfileList() self.readPidToProfileList(@@profile_name).each { |pid| MyProcess.kill(pid) self.removePidToProfileList(@@profile_name, pid) } self.releaseProfileList() begin if (@@browser != nil) @@browser.close else end rescue => e # end # reseteo las variables @@browser = nil @@pid = nil @@profile_name = nil @@driver = nil @@type = nil end |
.getPid ⇒ Object
229 230 231 |
# File 'lib/browserfactory.rb', line 229 def self.getPid() return @@pid end |
.isCreated ⇒ Object
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/browserfactory.rb', line 233 def self.isCreated() if (self.readPidToProfileList(@@profile_name).size>0) return true end if (@@browser != nil) return true end return false end |
.isCreated? ⇒ Boolean
244 245 246 |
# File 'lib/browserfactory.rb', line 244 def self.isCreated? self.isCreated() end |
.launch_chrome(proxy = nil, load_timeout = DEFAULT_LOAD_TIMEOUT, user_agent = nil, profile_name = nil, chromedriver_path = nil, chromedriver_signature = nil) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/browserfactory.rb', line 388 def self.launch_chrome(proxy=nil, load_timeout=DEFAULT_LOAD_TIMEOUT, user_agent=nil, profile_name=nil, chromedriver_path=nil, chromedriver_signature=nil) # @@browser = nil @@driver = nil @@profile_name = profile_name # fname = PROFILE_PID_LOCK_FILENAME.gsub('%PRFILE_NAME%', @@profile_name) @@fd_profile = File.open(fname,"w") # agent = @@arAgents[0] if user_agent.nil? agent = user_agent if !user_agent.nil? # #959 - valido que el parametro agents sea un array - porque si se pasa un string se lo procesa como array y se toma un UA name de un char. #raise "Array expected in parameter agents in BrowserFactory.create." if !(agent.is_a? String) # while (@@browser == nil) # Levantar el flag de reserva a mi favor self.lock() begin client = Selenium::WebDriver::Remote::Http::Default.new begin client.read_timeout = load_timeout # for newest selenium webdriver version rescue => e client.timeout = load_timeout # deprecated in newest selenium webdriver version end # se agregan extensiones por el issue #1171 # # UPDATE: Las extensiones en JavaScript para eliminar rastros, dejan rastros en si mismas. # switches = ["lang=en", "--log-level=3", "--user-agent=#{agent}", "disable-infobars", "load-extension=#{File.absolute_path('../chrome_extension')}"] # if (profile_name!=nil) # issue #964 filename = "#{profile_name}/#{profile_name}/Default/Preferences" if ( File.file?(filename) == true ) File.delete(filename) end # issue #964 filename = "#{profile_name}/#{profile_name}/Local State" if ( File.file?(filename) == true ) File.delete(filename) end # configuro el browser para que use este perfil switches += ["--user-data-dir=#{profile_name}/#{profile_name}"] end if (proxy!=nil) switches += proxy.chrome_switches end self.lockProfileList() begin Selenium::WebDriver::Chrome.driver_path = chromedriver_path if !chromedriver_path.nil? Selenium::WebDriver::Chrome.driver_path = DEFAULT_CHROMEDRIVER_PATH if chromedriver_path.nil? @@driver = Selenium::WebDriver.for :chrome, :switches => switches bridge = @@driver.instance_variable_get(:@bridge) service = bridge.instance_variable_get(:@service) process = service.instance_variable_get(:@process) @@pid = process.pid.to_s # es el PID del chromedriver @@driver.manage.window().maximize() @@browser = PampaBrowser.new(@@driver) @@browser.proxy = proxy.clone if !proxy.nil? @@browser.agent_name = agent @@browser.profile_name = profile_name self.updateProfileList rescue => e p1 = PROCESS.list().select { |p| p[:ppid] == PROCESS.pid && p[:executablepath] =~ /chromedriver(.*)\.exe/ }.first if (p1 != nil) @@pid = p1[:pid] self.updateProfileList end self.releaseProfileList() raise e end self.releaseProfileList() # Liberar el flag de reserva de creacion del browser self.release() # rescue => e # TODO: Se atrapa una expecion porque sigue ocurreiendo el error reportado en el issue #134 y #129 # Liberar el flag de reserva de creacion del browser self.release() # disparar la exepcion raise e end end # while # @@browser end |
.lock ⇒ Object
258 259 260 |
# File 'lib/browserfactory.rb', line 258 def self.lock() @@fd.flock(File::LOCK_EX) end |
.lockProfileList ⇒ Object
191 192 193 |
# File 'lib/browserfactory.rb', line 191 def self.lockProfileList() # @@fd_profile.flock(File::LOCK_EX) end |
.mimic(mimic_profile_id) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/browserfactory.rb', line 321 def self.mimic(mimic_profile_id) begin # Levantar el flag de reserva a mi favor self.lock() @@profile_name = mimic_profile_id url_2 = nil i = 0 max = 2 while (i<max && url_2.nil?) i += 1 url = "http://127.0.0.1:#{BlackStack::StealthBrowserAutomation::Multilogin::mla_local_port}/api/v1/profile/start?automation=true&profileId=#{@@profile_name}" uri = URI.parse(url) req = Net::HTTP::Get.new(url) res = Net::HTTP.start(uri.host, uri.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) {|http| http.request(req) } res = JSON.parse(res.body) if !res.has_key?('status') if i<max self.release() # libero el flag de reserva de turno para crar un browser raise "Error removing Multilogin profile: #{res.to_s}" end elsif res['status'] != 'OK' if i<max self.release() # libero el flag de reserva de turno para crar un browser raise "Error removing Multilogin profile: #{res['status'].to_s}" end else url_2 = res['value'] end end if !url_2.nil? #uri_2 = URI.parse(url_2) #req_2 = Net::HTTP::Get.new(url_2) #res_2 = Net::HTTP.start(uri_2.host, uri_2.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) {|http| # http.request(req_2) #} @@driver = Selenium::WebDriver.for(:remote, :url => url_2, :desired_capabilities => nil) bridge = @@driver.instance_variable_get(:@bridge) service = bridge.instance_variable_get(:@service) process = service.instance_variable_get(:@process) @@pid = process.pid.to_s # es el PID del chromedriver @@driver.manage.window().maximize() @@browser = Watir::Browser.new(@@driver) self.updateProfileList end rescue => e p1 = PROCESS.list().select { |p| p[:ppid] == PROCESS.pid && p[:executablepath] =~ /chromedriver(.*)\.exe/ }.first if (p1 != nil) @@pid = p1[:pid] self.updateProfileList end self.releaseProfileList() raise e end # self.releaseProfileList() # libero el flag de reserva de turno para crar un browser self.release() end |
.readPidToProfileList(profile_name) ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/browserfactory.rb', line 162 def self.readPidToProfileList(profile_name) if (profile_name != nil) fname = PROFILE_PID_LIST_FILENAME.gsub('%PRFILE_NAME%', profile_name) s = File.read(fname) return s.split(",") else return [] end end |
.release ⇒ Object
262 263 264 |
# File 'lib/browserfactory.rb', line 262 def self.release() @@fd.flock(File::LOCK_UN) end |
.releaseProfileList ⇒ Object
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/browserfactory.rb', line 195 def self.releaseProfileList() # @@fd_profile.flock(File::LOCK_UN) =begin begin @@fd_profile.close rescue end @@fd_profile = nil =end end |
.removePidToProfileList(profile_name, pid) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/browserfactory.rb', line 153 def self.removePidToProfileList(profile_name, pid) fname = PROFILE_PID_LIST_FILENAME.gsub('%PRFILE_NAME%', profile_name) s = File.read(fname) s = s.gsub("#{pid},", "") f = File.open(fname, 'w') f.write(s) f.close end |
.reservation_id ⇒ Object
248 249 250 251 252 253 254 255 256 |
# File 'lib/browserfactory.rb', line 248 def self.reservation_id() q = "SELECT TOP 1 reservation_id FROM browserfactory WHERE host_name='#{Socket.gethostname}'" row = DB[q].first if (row == nil) DB.execute("INSERT INTO browserfactory (reservation_id, reservation_time, host_name) VALUES (NULL, NULL, '#{Socket.gethostname}')") end row = DB[q].first return row[:reservation_id] end |
.screenshot(filename) ⇒ Object
toma una captura del area visible de la pagina mas informacion: stackoverflow.com/questions/25543651/screenshot-of-entire-webpage-using-selenium-webdriver-rc-rails
221 222 223 |
# File 'lib/browserfactory.rb', line 221 def self.screenshot(filename) @@driver.save_screenshot(filename) end |
.updateProfileList ⇒ Object
172 173 174 175 176 177 |
# File 'lib/browserfactory.rb', line 172 def self.updateProfileList() addPidToProfileList(@@profile_name, @@pid) PROCESS.list().select { |p| p[:ppid] == @@pid && p[:executablepath] =~ /chrome\.exe/ }.each { |p2| addPidToProfileList(@@profile_name, p2[:pid]) } end |