-
Notifications
You must be signed in to change notification settings - Fork 20
/
bot.py
94 lines (68 loc) · 3.22 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import logging, threading, time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from utils import write_cookies_to_file, find_path
opened_sessions = []
def transfer_session(browser):
driver = browser['browser']
logging.info("[New Thread] Transferring session {}".format(driver.session_id))
# Save cookies
write_cookies_to_file(driver.get_cookies())
url = driver.current_url
chrome_options = Options()
chrome_options.add_argument("user-agent={}".format(browser['user_agent']))
chrome_options.add_argument("--proxy-server=http://{}".format(browser['proxy'][0]))
chrome = webdriver.Chrome(executable_path=find_path('chromedriver'), chrome_options=chrome_options)
# Open URL and wait for proxy login
chrome.get(url)
logging.info("[CHROME/PROXY] Login with {}".format(browser['proxy'][1]))
chrome.implicitly_wait(10)
element = WebDriverWait(chrome, 1000).until(EC.presence_of_element_located((By.TAG_NAME, "div")))
# Transfer Cookies
chrome.delete_all_cookies()
for cookie in driver.get_cookies():
chrome.add_cookie(cookie)
chrome.refresh()
time.sleep(10000)
def start_session(url, browser):
global opened_sessions
# empty the cookies file
open('cookies.txt', 'w').close()
while True:
if browser['browser'].session_id not in opened_sessions:
logging.info("Checking if session {} is past splash".format(browser['browser'].session_id))
tries = 0
while tries < 5:
# Check for captcha field
try:
captcha = "captcha-container clearfix" in browser['browser'].page_source.lower()
if captcha:
opened_sessions.append(browser['browser'])
threading.Thread(target=transfer_session, kwargs={'browser': browser}).start()
return False
except:
pass
# Check cookies
hmac_cookie_exists = False
for cookie in browser['browser'].get_cookies():
hmac_cookie_exists = True if 'hmac=' in cookie['value'] or 'gceeqs' == cookie['name'] else False
if hmac_cookie_exists:
break
if hmac_cookie_exists:
pass
# opened_sessions.append(browser['browser'])
# threading.Thread(target=transfer_session, kwargs={'browser': browser}).start()
# return False
# Delete cookies and try again, seems to work these days
browser['browser'].delete_all_cookies()
browser['browser'].refresh()
tries += 1
time.sleep(10)
def run(url, faceless_browsers):
for browser in faceless_browsers:
threading.Thread(target=start_session, kwargs={'url': url, 'browser': browser}).start()