def __init__(self): self.logger = logging.getLogger('') self.http_operator = HttpOperator()
class WeixinLogin(object): def __init__(self): self.logger = logging.getLogger('') self.http_operator = HttpOperator() def login(self): url = 'https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_={}'.format(int(time.time() * 1000)) content = self.http_operator.get(url) # eg. window.QRLogin.code = 200; window.QRLogin.uuid = "IfiCmjmY-w=="; self.logger.debug(content) regex = ur'window.QRLogin.code ?= ?(\d+); *window.QRLogin.uuid ?= ?\"(.+)\"' matcher = re.compile(regex).search(content) if matcher: status_code = int(matcher.group(1)) uuid = matcher.group(2) if status_code != 200: self.logger.warn(u'window.QRLogin.code = %s' % status_code) else: self.logger.error(u'请求失败:%s' % url) url = 'https://login.weixin.qq.com/qrcode/%s?t=webwx' % uuid content = self.http_operator.get(url) with open('qrcode.jpg', 'w') as f: f.write(content) redirect_uri = '' for x in xrange(10): url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid={}&tip=1&_={}'.format(uuid, int(time.time()*1000)) print 'requesting %s' % url content = self.http_operator.get(url) print content regex = ur'window.code *= *(\d+)' matcher = re.compile(regex).search(content) if matcher: status_code = int(matcher.group(1)) if status_code >= 400 or status_code == 201: continue elif status_code == 200: regex = ur'window.redirect_uri *= *\"(.+)\"' m = re.compile(regex).search(content) if m: redirect_uri = m.group(1) else: self.logger.error(u'%s code=200但是没有获取到window.redirect_uri: %s' % (url, content)) break else: self.logger.error(u'%s 没有获取到window.code: %s' % (url, content)) break self.logger.debug(u'redirect_uri: %s' % redirect_uri) print u'redirect_uri: %s' % redirect_uri if redirect_uri == '': self.logger.warn(u'没有成功获取到redirect_uri') return url = '%s&fun=new' % redirect_uri content = self.http_operator.get(url) cookies = {c.name: c.value for c in self.http_operator.cookiejar} wxsid = cookies.get('wxsid') wxuin = cookies.get('wxuin') if 'wxsid' not in cookies or 'wxuin' not in cookies: self.logger.error(u'wxsid or wxuin not in cookie: %s' % url) matcher = re.compile(r'<skey>(.+)</skey').search(content) sid = matcher.group(1) if matcher else '' print content for cookie in self.http_operator.cookiejar: print '%s: %s' % (cookie.name, cookie.value) url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r={}'.format(int(time.time()*1000)) # post_data = dict( # BaseRequest=dict( # Uin=wxuin, Sid=wxsid, Skey='', DeviceId='' # ) # ) print 'requesting %s' % url # content = self.http_operator.post(url, post_data=post_data, headers=dict(Referer='https://wx2.qq.com/')) # print content post_data = dict(BaseRequest=dict(Uin=wxuin, Sid=wxsid, Skey='', DeviceId='')) req = urllib2.Request(url, data=json.dumps(post_data), headers=dict(Referer='https://wx.qq.com/')) rsp = urllib2.urlopen(req) body = rsp.read() print json.loads(body)