Skip to content

Commit d731951

Browse files
committed
Adding recaptcha
Tesla changed to use re-captcha so this code uses new recaptcha solver and applies it to authorize call
1 parent 828d71b commit d731951

14 files changed

+115
-33
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ crontab -e
5050
# About code
5151
* powerwallDriver.py - main driver file
5252
* powerwallBackup.py - runs back up for given account (set reserve to 100%) forces charge
53-
* powerwallTimeOfUser.py - runs time of use given account (set reserve to 0%) forces discharge
53+
* powerwallTimeOfUse.py - runs time of use given account (set reserve to 0%) forces discharge

captcha.png

-6.35 KB
Binary file not shown.

captcha.svg

-1
This file was deleted.

captchasolver.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# The link above allows you to create a acount with 2captcha which is what we use
1212
# If you know me we can share API key, just reach out to me thanks
1313
# Appreciate donations to keep 2captcha going, anything helps
14-
API_KEY = '8041550c5164a06259d450ef3256ee5d' # Your 2captcha API KEY
14+
API_KEY = '' # Your 2captcha API KEY
1515
CAPTCHA_ENABLE = True
1616

1717
def main(session, headers):

powerwallBackup.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import random
3333
import string
3434

35-
import captchasolver
35+
import recaptchasolver
3636

3737

3838
_LOGGER = logging.getLogger(__name__)
@@ -59,7 +59,7 @@ def main(emailItem, passwordItem):
5959

6060
attempts = 0
6161
tpw = powerwall_site(gateway_host, password, email)
62-
while 'fail' in tpw.token and attempts < 6:
62+
while 'fail' in tpw.token and attempts < 5:
6363
print ("attempts count: ", attempts)
6464
try:
6565
attempts = attempts + 1
@@ -143,19 +143,22 @@ def authenticate(self):
143143
print "authenticate method"
144144
auth_url = self.authUrl();
145145

146-
headers = {
147-
'User-Agent' : 'PowerwallDarwinManager'
148-
}
149-
#headers = {}
146+
#headers = {
147+
# #'User-Agent' : 'PowerwallDarwinManager'
148+
#}
149+
headers = {}
150150
resp = session.get(auth_url, headers=headers)
151+
#print (resp.text)
151152

152-
csrf = re.search(r'name="_csrf".+value="([^"]+)"', resp.text).group(1)
153-
transaction_id = re.search(r'name="transaction_id".+value="([^"]+)"', resp.text).group(1)
154153

154+
recaptcha_site_key = re.search(r".*sitekey.* : '(.*)'", resp.text).group(1)
155+
print ('captcha sitekey: ' + recaptcha_site_key)
156+
print ('auth url: ' + auth_url)
155157

156158

157-
captchacode = captchasolver.main(session, headers)
158-
print("captchacode: ", captchacode)
159+
csrf = re.search(r'name="_csrf".+value="([^"]+)"', resp.text).group(1)
160+
transaction_id = re.search(r'name="transaction_id".+value="([^"]+)"', resp.text).group(1)
161+
captchacode = recaptchasolver.main(recaptcha_site_key, auth_url)
159162

160163
data = {
161164
"_csrf": csrf,
@@ -165,8 +168,11 @@ def authenticate(self):
165168
"cancel": "",
166169
"identity": self.email,
167170
"credential": self.password,
168-
"captcha" : captchacode
171+
"g-recaptcha-response:": captchacode,
172+
"recaptcha": captchacode
169173
}
174+
175+
#print(data)
170176
print "Opening session with login"
171177
# Important to say redirects false cause this will result in 302 and need to see next data
172178
resp = session.post(auth_url, headers=headers, data=data, allow_redirects=False)
@@ -253,7 +259,14 @@ def authenticate(self):
253259

254260
# If not MFA This code plays instead , which is parising location
255261
print "Coming to non MFA flow:"
262+
#print (resp)
263+
#print ("response text: ==================================")
264+
#print (resp.text)
265+
#print ("response headers: ----------------------------------------")
266+
#print (resp.headers)
267+
256268
code_url = resp.headers["location"]
269+
print("location header: " + str(code_url))
257270
parsed = urlparse.urlparse(code_url)
258271
code = urlparse.parse_qs(parsed.query)['code']
259272

powerwallBackup.pyc

152 Bytes
Binary file not shown.

powerwallTimeOfUse.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import random
3333
import string
3434

35-
import captchasolver
35+
import recaptchasolver
3636

3737

3838

@@ -142,19 +142,19 @@ def authenticate(self):
142142
print "authenticate method"
143143
auth_url = self.authUrl();
144144

145-
headers = {
146-
'User-Agent' : 'PowerwallDarwinManager'
147-
}
148-
# headers = {}
145+
headers = {}
149146
resp = session.get(auth_url, headers=headers)
147+
#print (resp.text)
150148

151-
csrf = re.search(r'name="_csrf".+value="([^"]+)"', resp.text).group(1)
152-
transaction_id = re.search(r'name="transaction_id".+value="([^"]+)"', resp.text).group(1)
153149

150+
recaptcha_site_key = re.search(r".*sitekey.* : '(.*)'", resp.text).group(1)
151+
print ('captcha sitekey: ' + recaptcha_site_key)
152+
print ('auth url: ' + auth_url)
154153

155154

156-
captchacode = captchasolver.main(session, headers)
157-
print("captchacode: ", captchacode)
155+
csrf = re.search(r'name="_csrf".+value="([^"]+)"', resp.text).group(1)
156+
transaction_id = re.search(r'name="transaction_id".+value="([^"]+)"', resp.text).group(1)
157+
captchacode = recaptchasolver.main(recaptcha_site_key, auth_url)
158158

159159
data = {
160160
"_csrf": csrf,
@@ -164,8 +164,10 @@ def authenticate(self):
164164
"cancel": "",
165165
"identity": self.email,
166166
"credential": self.password,
167-
"captcha" : captchacode
167+
"g-recaptcha-response:": captchacode,
168+
"recaptcha": captchacode
168169
}
170+
169171
print "Opening session with login"
170172
# Important to say redirects false cause this will result in 302 and need to see next data
171173
resp = session.post(auth_url, headers=headers, data=data, allow_redirects=False)

powerwallTimeOfUse.pyc

118 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)