def __login_check(self, stage=0): self.apidata = dict() req = urllib2.Request("http://web.im.baidu.com/") ret = self.opener.open(req) ret.read() # Fix ret = self.api_request("check", v=30, time=utils.timechecksum()) self.logdebug("Login check return value: %s", ret) # 登陆校验成功. if ret["result"] == "ok": self.cookiejar.save() self.emit("login-check", True) self.loginfo("Login check success!") return True # 登陆校验失败(超过两次登陆校验) elif stage >= 2: self.emit("login-check", False) self.loginfo("Login check failed!") return False assert ret['result'] == 'offline' req = urllib2.Request('http://passport.baidu.com/api/?login&tpl=mn&time=%d' % utils.timestamp()) data = self.opener.open(req).read().strip()[1:-1] # remove brackets data = eval(data, type('Dummy', (dict,), dict(__getitem__=lambda s,n:n))()) if int(data["error_no"]) != 0: self.logdebug("Login passport error: %s", data) return False param_out = data["param_out"] param_in = data["param_in"] params = {v : param_out[k.replace("name", "contex")] for k, v in param_out.items() if k.endswith("_name")} params.update({v: param_in[k.replace("name", "value")] for k,v in param_in.items() if k.endswith("_name")}) params["username"] = self.username params["password"] = self.password params["safeflg"] = "" params["mem_pass"] = "******" if int(params["verifycode"]) == 1 and stage == 1: self.loginfo("Login check require verifycode") self.emit("login-verify", self.get_verify_code()) self.login_verify_condition.acquire() self.login_verify_condition.wait() params["verifycode"] = self.login_verify_code params['staticpage'] = 'http://web.im.baidu.com/popup/src/login_jump.htm' self.logdebug("After filing params: %s", params) req = urllib2.Request('https://passport.baidu.com/api/?login', data=urllib.urlencode(params)) html = self.opener.open(req).read() url = re.findall(r"encodeURI\('(.*?)'\)", html)[0] self.opener.open(url).read() # 二次登陆校验 if stage == 0: self.loginfo("Begin second login check..") elif stage == 1: self.loginfo("Begin three login check..") return self.login(stage=stage+1)
def __login_check(self, stage=0): self.apidata = dict() req = urllib2.Request("http://web.im.baidu.com/") ret = self.opener.open(req) ret.read() # Fix ret = self.api_request("check", v=30, time=utils.timechecksum()) self.logdebug("Login check return value: %s", ret) # 登陆校验成功. if ret["result"] == "ok": self.cookiejar.save() self.emit("login-check", True) self.loginfo("Login check success!") return True # 登陆校验失败(超过两次登陆校验) elif stage >= 2: self.emit("login-check", False) self.loginfo("Login check failed!") return False assert ret['result'] == 'offline' req = urllib2.Request('http://passport.baidu.com/api/?login&tpl=mn&time=%d' % utils.timestamp()) data = self.opener.open(req).read().strip()[1:-1] # remove brackets data = eval(data, type('Dummy', (dict,), dict(__getitem__=lambda s,n:n))()) if int(data["error_no"]) != 0: self.logdebug("Login passport error: %s", data) return False param_out = data["param_out"] param_in = data["param_in"] params = {v : param_out[k.replace("name", "contex")] for k, v in param_out.items() if k.endswith("_name")} params.update({v: param_in[k.replace("name", "value")] for k,v in param_in.items() if k.endswith("_name")}) params["username"] = self.username params["password"] = self.password params["safeflg"] = "" params["mem_pass"] = "******" if int(params["verifycode"]) == 1 and stage == 1: self.loginfo("Login check require verifycode") self.emit("login-verify", self.get_verify_code()) self.login_verify_condition.acquire() self.login_verify_condition.wait() params["verifycode"] = self.login_verify_code params['staticpage'] = 'http://web.im.baidu.com/popup/src/login_jump.htm' self.logdebug("After filing params: %s", params) req = urllib2.Request('https://passport.baidu.com/api/?login', data=urllib.urlencode(params)) html = self.opener.open(req).read() url = re.findall(r"encodeURI\('(.*?)'\)", html)[0] self.opener.open(url).read() # 二次登陆校验 if stage == 0: self.loginfo("Begin second login check..") elif stage == 1: self.loginfo("Begin three login check..") return self.__login_check(stage=stage+1)
def __login_init(self): # 登陆后初始化. self.seq = 0 guid = utils.timechecksum() # API请求公用数据 self.apidata = dict(v=30, session="", source=22, guid=guid, seq=lambda : self.seq) # 开始登陆过程 self.api_request("welcome", method="POST", extra_data={"from" : 0}, seq=self.seq, force="true") ret = self.api_request("init", method="POST", status="online") if ret["result"] == "ok": self.loginfo("Login ok: username=%s, nick=%s", ret["content"]["username"], ret["content"]["nickname"]) # 第一次 pick 自己是否登陆成功, ack = 0 self.pick() # 获取分组信息 self.get_multi_team_info() self.logdebug("Team infos: %s", self.multi_team_info) # 获取阻止联系人信息 self.get_block_list() self.logdebug("Block list: %s", self.block_list) # 获取离线系统信息 self.get_old_system() self.logdebug("Old system: %s", self.old_system) # 获取离线消息 self.get_old_message() self.logdebug("Old message: %s", self.old_message) # 获取离线通知信息 self.get_old_notify() self.logdebug("Old notify: %s", self.old_notify) # 获取系统信息 self.get_system_message() # 获取离线群信息 self.get_old_group_message() self.logdebug("Old group message: %s", self.old_group_message) self.emit("init_finished") return True
def sign_list(self): ret = self.api_request("f/user/sign_list?t=%s" % utils.timechecksum()) return ret
def verify_code(self, type, **params): ret = self.api_request("verifycode", type=type, **params) vdata = ret["content"]["validate"] if vdata.get("v_code", None): return ",".join([vdata['v_url'], vdata['v_period'], vdata['v_time'], vdata['v_code']]) else: self.logerror('Verifycode not implemented! type=%s, args=%s', type, params) return None image_url = 'http://vcode.im.baidu.com/cgi-bin/genimg?%s&_time=%s' % (vdata["v_url"], utils.timechecksum()) data = self._opener.open(image_url).read() pic_image = get_cache_file("pic.jpg") with open(pic_image, 'wb') as fp: fp.write(data) self.loginfo('Verify code pic download ok!') code = 'abcd' return ','.join([vdata['v_url'], vdata['v_period'], vdata['v_time'], code])