def get_pre_login_status(username_): """ Perform pre login action, get pre login status, including server time, nonce, rsa kv, etc. """ pre_login_url_ = PRE_LOGIN_URL % get_user(username_) data_ = StringUtils.convert_to_str(HttpClient.get(pre_login_url_)) p_ = re.compile('\((.*)\)') json_data = p_.search(data_).group(1) data_ = json.loads(json_data) server_time_ = str(data_['servertime']) nonce_ = data_['nonce'] rsa_kv_ = data_['rsakv'] return server_time_, nonce_, rsa_kv_
def do_login(): logging.info('Starting to login...') username_ = Configuration.UESRNAME pwd_ = Configuration.PASSWORD """" Perform login action with use name, password and saving cookies. @param username_: login user name @param pwd_: login password @param cookie_file_: file name where to save cookies when login succeeded """ # POST data per login weibo, these fields can be captured using httpfox extension in Firefox login_data_ = { 'entry': 'weibo', 'gateway': '1', 'from': '', 'savestate': '7', 'userticket': '1', 'pagerefer': '', 'vsnf': '1', 'su': '', 'service': 'miniblog', 'servertime': '', 'nonce': '', 'pwencode': 'rsa2', 'rsakv': '', 'sp': '', 'encoding': 'UTF-8', 'prelt': '45', 'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', 'returntype': 'META' } login_url_ = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.11)' try: server_time_, nonce_, rsakv_ = get_pre_login_status(username_) except Exception as e: logging.error(e) return # Fill POST data print('starting to set login_data') login_data_['servertime'] = server_time_ login_data_['nonce'] = nonce_ login_data_['su'] = get_user(username_) login_data_['sp'] = get_pwd_rsa(pwd_, server_time_, nonce_) login_data_['rsakv'] = rsakv_ text_ = HttpClient.get(login_url_, login_data_) text_ = StringUtils.convert_to_str(text_, Charset.GBK) p_ = re.compile('location\.replace\(\'(.*?)\'\)') try: # Search login redirection URL login_url_ = p_.search(text_).group(1) data_ = HttpClient.get(login_url_) data_ = StringUtils.convert_to_str(data_, Charset.GBK) #Verify login feedback, check whether result is TRUE patt_feedback_ = 'feedBackUrlCallBack\((.*)\)' p_ = re.compile(patt_feedback_, re.MULTILINE) feedback_ = p_.search(data_).group(1) feedback_json_ = json.loads(feedback_) if feedback_json_['result']: HttpClient.save_cookie_in_file() except Exception as e: logging.error(e)