Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache token to avoid re-log when debugging #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions wxbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,9 +1177,10 @@ def search_content(key, content, fmat='attr'):

def run(self):
try:
self.get_uuid()
self.gen_qr_code(os.path.join(self.temp_pwd,'wxqr.png'))
print '[INFO] Please use WeChat to scan the QR code .'
if not os.path.exists('token.txt'):
self.get_uuid()
self.gen_qr_code(os.path.join(self.temp_pwd,'wxqr.png'))
print '[INFO] Please use WeChat to scan the QR code .'

result = self.wait4login()
if result != SUCCESS:
Expand Down Expand Up @@ -1209,6 +1210,11 @@ def run(self):
except Exception,e:
print '[ERROR] Web WeChat run failed --> %s'%(e)
self.status = 'loginout'
try:
os.remove('token.txt')
print '[INFO] Removed token file'
except:
print '[INFO] Fail to removed token file'


def get_uuid(self):
Expand Down Expand Up @@ -1268,8 +1274,16 @@ def wait4login(self):

retry_time = MAX_RETRY_TIMES
while retry_time > 0:
url = LOGIN_TEMPLATE % (tip, self.uuid, int(time.time()))
code, data = self.do_request(url)
# Use the cached token to login if available
# so that we don't need to re-login when re-run the codes
try:
with open('token.txt', 'r') as f:
line = f.read()
code, data = line.split(':', 1)
except:
url = LOGIN_TEMPLATE % (tip, self.uuid, int(time.time()))
code, data = self.do_request(url)

if code == SCANED:
print '[INFO] Please confirm to login .'
tip = 0
Expand All @@ -1280,6 +1294,11 @@ def wait4login(self):
self.base_uri = redirect_uri[:redirect_uri.rfind('/')]
temp_host = self.base_uri[8:]
self.base_host = temp_host[:temp_host.find("/")]

# Cache the token when login successfully
with open('token.txt', 'w') as f:
f.write(code + ':' + data)

return code
elif code == TIMEOUT:
print '[ERROR] WeChat login timeout. retry in %s secs later...' % (try_later_secs,)
Expand Down