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

sync check 1101 问题 #998

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
47 changes: 32 additions & 15 deletions itchat/components/login.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, time, re, io
import os, time, re, io, sys
import threading
import json, xml.dom.minidom
import random
Expand Down Expand Up @@ -56,10 +56,18 @@ def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
if status == '200':
isLoggedIn = True
elif status == '201':
# Here we should sleep for a while to wait user
# otherwise it will call a lot of check_login
# which acts like attacking server
time.sleep(20)
if isLoggedIn is not None:
logger.info('Please press confirm on your phone.')
isLoggedIn = None
elif status != '408':
elif status == '403':
sys.exit(-1)
elif status == '408':
logger.info("You didn't scan the QR code yet.")
else:
break
if isLoggedIn:
break
Expand Down Expand Up @@ -123,25 +131,29 @@ def get_QR(self, uuid=None, enableCmdQR=False, picDir=None, qrCallback=None):
utils.print_qr(picDir)
return qrStorage

def check_login(self, uuid=None):
def check_login(self, uuid=None, tip=1):
uuid = uuid or self.uuid
url = '%s/cgi-bin/mmwebwx-bin/login' % config.BASE_URL
localTime = int(time.time())
params = 'loginicon=true&uuid=%s&tip=1&r=%s&_=%s' % (
uuid, int(-localTime / 1579), localTime)
params = 'loginicon=true&uuid=%s&tip=%d&r=%s&_=%s' % (
uuid, tip, int(-localTime / 1579), localTime)
headers = { 'User-Agent' : config.USER_AGENT }
r = self.s.get(url, params=params, headers=headers)
regx = r'window.code=(\d+)'
data = re.search(regx, r.text)
if data and data.group(1) == '200':
if process_login_info(self, r.text):
return '200'
try:
r = self.s.get(url, params=params, headers=headers)
regx = r'window.code=(\d+)'
data = re.search(regx, r.text)
if data and data.group(1) == '200':
if process_login_info(self, r.text):
return '200'
else:
return '400'
elif data:
return data.group(1)
else:
return '400'
elif data:
return data.group(1)
else:
return '400'
except requests.exceptions.ConnectionError:
logger.error('Remote end closed connection without response.')
return '403'

def process_login_info(core, loginContent):
''' when finish login (scanning qrcode)
Expand Down Expand Up @@ -250,6 +262,9 @@ def maintain_loop():
self.alive = False
elif i == '0':
pass
elif i == '1101':
logger.info('Confirm on mobile to re-login')
status = self.check_login(tip=0)
else:
msgList, contactList = self.get_msg()
if msgList:
Expand Down Expand Up @@ -317,6 +332,8 @@ def sync_check(self):
r.raise_for_status()
regx = r'window.synccheck={retcode:"(\d+)",selector:"(\d+)"}'
pm = re.search(regx, r.text)
if pm is not None and pm.group(1) == '1101': # mobile login out
return '1101'
if pm is None or pm.group(1) != '0':
logger.debug('Unexpected sync check result: %s' % r.text)
return None
Expand Down
7 changes: 6 additions & 1 deletion itchat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ def _emoji_formatter(m):
def msg_formatter(d, k):
emoji_formatter(d, k)
d[k] = d[k].replace('<br/>', '\n')
d[k] = htmlParser.unescape(d[k])
try:
d[k] = htmlParser.unescape(d[k])
except AttributeError: # Python3.9 above
from html import unescape
d[k] = unescape(d[k])


def check_file(fileDir):
try:
Expand Down