forked from Noctem/Monocle
/
solve_captchas.py
executable file
·168 lines (148 loc) · 6.63 KB
/
solve_captchas.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
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
#!/usr/bin/env python3
from multiprocessing.managers import BaseManager
from asyncio import get_event_loop, sleep
from time import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from aiopogo import PGoApi, close_sessions, activate_hash_server, exceptions as ex
from aiopogo.auth_ptc import AuthPtc
from monocle import altitudes, sanitized as conf
from monocle.utils import get_device_info, get_address, randomize_point
from monocle.bounds import center
async def solve_captcha(url, api, driver, timestamp):
driver.get(url)
WebDriverWait(driver, 86400).until(EC.text_to_be_present_in_element_value((By.NAME, "g-recaptcha-response"), ""))
driver.switch_to.frame(driver.find_element_by_xpath("//*/iframe[@title='recaptcha challenge']"))
token = driver.find_element_by_id("recaptcha-token").get_attribute("value")
request = api.create_request()
request.verify_challenge(token=token)
request.get_hatched_eggs()
request.get_inventory(last_timestamp_ms=timestamp)
request.check_awarded_badges()
request.get_buddy_walked()
request.check_challenge()
for attempt in range(-1, conf.MAX_RETRIES):
try:
response = await request.call()
return response['responses']['VERIFY_CHALLENGE']['success']
except (ex.HashServerException, ex.MalformedResponseException, ex.ServerBusyOrOfflineException) as e:
if attempt == conf.MAX_RETRIES - 1:
raise
else:
print('{}, trying again soon.'.format(e))
await sleep(4)
except ex.NianticThrottlingException:
if attempt == conf.MAX_RETRIES - 1:
raise
else:
print('Throttled, trying again in 11 seconds.')
await sleep(11)
except (KeyError, TypeError):
return False
async def main():
try:
class AccountManager(BaseManager): pass
AccountManager.register('captcha_queue')
AccountManager.register('extra_queue')
manager = AccountManager(address=get_address(), authkey=conf.AUTHKEY)
manager.connect()
captcha_queue = manager.captcha_queue()
extra_queue = manager.extra_queue()
activate_hash_server(conf.HASH_KEY)
driver = webdriver.Chrome()
driver.set_window_size(803, 807)
while not captcha_queue.empty():
account = captcha_queue.get()
username = account.get('username')
location = account.get('location')
if location and location != (0,0,0):
lat = location[0]
lon = location[1]
else:
lat, lon = randomize_point(center, 0.0001)
try:
alt = altitudes.get(point)
except KeyError:
alt = await altitudes.fetch(point)
try:
device_info = get_device_info(account)
api = PGoApi(device_info=device_info)
api.set_position(lat, lon, alt)
authenticated = False
try:
if account['provider'] == 'ptc':
api.auth_provider = AuthPtc()
api.auth_provider._access_token = account['auth']
api.auth_provider._access_token_expiry = account['expiry']
if api.auth_provider.check_access_token():
api.auth_provider.authenticated = True
authenticated = True
except KeyError:
pass
if not authenticated:
await api.set_authentication(username=username,
password=account['password'],
provider=account.get('provider', 'ptc'))
request = api.create_request()
await request.call()
await sleep(.6)
request.download_remote_config_version(platform=1, app_version=6100)
request.check_challenge()
request.get_hatched_eggs()
request.get_inventory()
request.check_awarded_badges()
request.download_settings()
response = await request.call()
account['time'] = time()
responses = response['responses']
challenge_url = responses['CHECK_CHALLENGE']['challenge_url']
timestamp = responses.get('GET_INVENTORY', {}).get('inventory_delta', {}).get('new_timestamp_ms')
account['location'] = lat, lon, alt
account['inventory_timestamp'] = timestamp
if challenge_url == ' ':
account['captcha'] = False
print('No CAPTCHA was pending on {}.'.format(username))
extra_queue.put(account)
else:
if await solve_captcha(challenge_url, api, driver, timestamp):
account['time'] = time()
account['captcha'] = False
print('Solved CAPTCHA for {}, putting back in rotation.'.format(username))
extra_queue.put(account)
else:
account['time'] = time()
print('Failed to solve for {}'.format(username))
captcha_queue.put(account)
except KeyboardInterrupt:
captcha_queue.put(account)
break
except KeyError:
print('Unexpected or empty response for {}, putting back on queue.'.format(username))
captcha_queue.put(account)
try:
print(response)
except Exception:
pass
await sleep(3)
except (ex.AuthException, ex.AuthTokenExpiredException) as e:
print('Authentication error on {}: {}'.format(username, e))
captcha_queue.put(account)
await sleep(3)
except ex.AiopogoError as e:
print('aiopogo error on {}: {}'.format(username, e))
captcha_queue.put(account)
await sleep(3)
except Exception:
captcha_queue.put(account)
raise
finally:
try:
driver.close()
close_sessions()
except Exception:
pass
if __name__ == '__main__':
loop = get_event_loop()
loop.run_until_complete(main())