forked from Teyzer/ProjectEulerBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpe_session.py
More file actions
197 lines (123 loc) · 4.58 KB
/
pe_session.py
File metadata and controls
197 lines (123 loc) · 4.58 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
import traceback
from requests import TooManyRedirects
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.support import expected_conditions as EC
from anticaptchaofficial.imagecaptcha import *
from PIL import Image
import json
import pe_api
import phone_api
from rich.console import Console
import os
console = Console()
MAX_TRIES = 3
CAPTCHA_KEY = None
PROFILE_NAME = None
def session_setup(captcha: str, profile: str) -> None:
global CAPTCHA_KEY, PROFILE_NAME
CAPTCHA_KEY = captcha
PROFILE_NAME = profile
def get_captcha(driver, element, path):
location = element.location
size = element.size
driver.save_screenshot(path)
image = Image.open(path)
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
image = image.crop((left, top, right, bottom))
image.save(path)
def solve(image_name: str, human: bool = False):
if human:
return input("Human CAPTCHA: ")
solver = imagecaptcha()
solver.set_key(CAPTCHA_KEY)
captcha_text = solver.solve_and_return_solution(image_name)
phone_api.bot_info("Consumed a CAPTCHA token")
return str(captcha_text)
def try_fetching_cookies(human: bool = False):
url = "https://projecteuler.net/sign_in"
filename = "web_utils/current-captcha.png"
if 'web_utils' not in os.listdir('.'):
os.mkdir('web_utils')
# GET THE CAPTCHA
service = Service(executable_path='/usr/local/bin/geckodriver')
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
driver = webdriver.Firefox(service=service, options=options)
driver.set_window_size(1080, 720)
driver.get(url)
WebDriverWait(driver, 2)
captcha = driver.find_element(By.ID,"captcha_image")
get_captcha(driver, captcha, filename)
captcha_result = solve(filename, human)
# FILL THE FORM
driver.find_element("xpath",
"//input[@id='username' and @name='username']"
).send_keys("EulerCommunity")
driver.find_element("xpath",
"//input[@id='password' and @name='password']"
).send_keys("IncredibleBoy")
driver.find_element("xpath",
"//input[@id='captcha' and @name='captcha']"
).send_keys(captcha_result)
driver.find_element("xpath",
"//input[@id='remember_me' and @name='remember_me']"
).click()
driver.find_element("xpath",
"//input[@name='sign_in' and @type='submit']"
).click()
cookies = driver.get_cookies()
driver.quit()
return cookies
def refresh_tokens():
human = False
current_tries = 0
found_keepalive = False
values = {"PHPSESSID": None, "keep_alive": None} # [PHPSESSID, keep_alive]
while not found_keepalive and current_tries < MAX_TRIES:
cookies = try_fetching_cookies(human)
current_tries += 1
console.log(f"[-] Making try #{current_tries} to refresh cookies")
for cookie in cookies:
if cookie["name"] == "PHPSESSID":
values["PHPSESSID"] = cookie["value"]
if cookie["name"] == "keep_alive":
found_keepalive = True
values["keep_alive"] = cookie["value"]
if values["keep_alive"] is not None:
phone_api.bot_info("Token refreshed automatically")
console.log("[+] Token refreshed automatically")
else:
phone_api.bot_crashed("Failed to refresh token")
console.log("[*] Failed to refresh token")
with open(PROFILE_NAME, "r") as f:
data = json.load(f)
data["session_keys"] = values
with open(PROFILE_NAME, "w") as f:
json.dump(data, f, indent=4)
pe_api.COOKIES = values
return values
def is_connected() -> bool:
try:
pe_request = pe_api.ProjectEulerRequest("https://projecteuler.net/archives", True)
except TooManyRedirects as exc:
pe_api.console.log(exc, traceback.format_exc())
return False
if pe_request.status != 200:
return False
return "Logged in as" in pe_request.response
def is_website_active() -> bool:
pe_request = pe_api.ProjectEulerRequest("https://projecteuler.net/", False)
return pe_request.status == 200
if __name__ == "__main__":
profile_name = "profiles/authentic.json"
with open(profile_name, "r") as f:
data = json.load(f)
session_setup(data["captcha_key"], profile_name)
print(is_connected())
print(refresh_tokens())