def account_all(self): """跑一次将51的登陆态搞上去,检查MySQL里面所有账号的登录态,假如没有登陆的话就尝试自动登陆(51),登陆不成功的发告警邮件 已登录的账号加入到redis key cookie_logined 中""" try: db = MySQLdb.connect(**self.sql_config) sql = ''' select user_name, `grap_source` from grapuser_info WHERE account_type = '购买账号' ''' cur = db.cursor() cur.execute(sql) data = cur.fetchall() db.close() data2 = list(data) for i in data: if i[1] == '51job': redis_key_pre = 'cookie51_' redis_key = redis_key_pre + i[0] if self.r.exists(redis_key): # 保证能在redis中找到相应的cookie字符串 a = liblogin.Login51() ck_str = self.r.get(redis_key) a.check_login(ck_str) if a.check_login(ck_str): data2.remove(i) self.r.sadd('cookie_logined', redis_key) else: cn, pw = self.sql_password(i[0]) a = liblogin.Login51(cn=cn, un=i[0], pw=pw) ck_str = a.main() # 这里那边尝试登陆3次 if len(ck_str) > 0: self.redis_ck_set(i[0], ck_str) data2.remove(i) elif i[1] == 'zl': redis_key_pre = 'cookiezl_' redis_key = redis_key_pre + i[0] if self.r.exists(redis_key): a = liblogin.LoginZL() ck_str = self.r.get(redis_key) a.check_login(ck_str) if a.check_login(ck_str): data2.remove(i) self.r.sadd('cookie_logined', redis_key) elif i[1] == 'zjol': # if i[1] == 'zjol': redis_key_pre = 'cookiecjol_' redis_key = redis_key_pre + i[0] if self.r.exists(redis_key): a = liblogin.LoginCJOL() ck_str = self.r.get(redis_key) if a.check_login(ck_str): data2.remove(i) self.r.sadd('cookie_logined', redis_key) except Exception, e: logging.warning('error, msg is {}'.format(str(e)), exc_info=True)
def cookie_choose(self, option=0): # 选择对应的账号 """挑选cookie, option 为1表示严格选择 buy pub num > 0 , 0 表示 不严格选择,用来先更新 buy pub num""" print '------selecting cookies---------' logging.info('begin to select cookie') s = requests.session() l = self.sql_select(option) # 选择的都是可以购买的 avail_list = [] if len(l) > 0: for user in l: if self.redis_exist(user): ck_str = self.redis_ck_get(user) company_name, password = self.sql_password(user) if self.source == '51job': a = liblogin.Login51(company_name.encode('utf-8'), user, password) elif self.source == 'zl': a = liblogin.LoginZL(company_name.encode('utf-8'), user, password) else: a = liblogin.LoginCJOL(company_name.encode('utf-8'), user, password) if a.check_login(ck_str): avail_list.append(user) else: ck_str2 = a.main() if a.check_login(ck_str2): avail_list.append(user) self.redis_ck_set(user, ck_str2) return avail_list
def insert_cookie(self): try: logging.info('update cookie start 333333 ........') redis_key_list = self.account.uni_user( time_period=self.time_period, num=self.time_num, hour_num=self.hour_num, day_num=self.day_num) print redis_key_list, 00000000, '------' t0 = time.time() if len(redis_key_list) > 0: for redis_key in redis_key_list: flag = False while not flag: self.username = redis_key.split('_')[1].encode('utf-8') print self.username, 1111111, '------' self.ck_str = self.account.redis_ck_get(self.username) print self.ck_str, 222222, '-------' self.headers['Cookie'] = self.ck_str if self.login_status_chk(): logging.info('cookie is useful {}'.format( self.username)) flag = True else: sql_res = self.account.sql_password(self.username) print sql_res, '333333333', '----+++' self.account.redis_ck_ex( self.username) # 更新该用户名的cookie失效时间 self.password = sql_res[1] self.ctmname = sql_res[0].encode('utf-8') print self.password, self.username, self.ctmname, '444444444' l_login = liblogin.Login51(cn=self.ctmname, un=self.username, pw=self.password) self.ck_str = l_login.main(sleep=5) self.headers['Cookie'] = self.ck_str if self.login_status_chk(): self.account.redis_ck_set( self.username, self.ck_str) logging.info( '51job user {} auto login success'.format( self.username)) logging.info( 'switching {} username {} success. '. format(self.module_name, self.username)) flag = True t1 = str('%.2f' % (time.time() - t0)) + 's' logging.info('cookie update complete use time {}'.format(t1)) return flag else: logging.error('no account lef for auto update 51 cookie') self.login2() return False except Exception, e: print e
def get_cookie(self): # 更改这里参数来选择账号 try: flag = False # self.account = libaccount.Manage(source='51job', option='down') redis_key_list = self.account.uni_user( time_period=self.time_period, num=self.time_num, hour_num=self.hour_num, day_num=self.day_num) # print redis_key_list, 8888888888 if len(redis_key_list) > 0: while len(redis_key_list) > 0 and not flag: redis_key = random.choice(redis_key_list) redis_key_list.remove(redis_key) self.username = redis_key.split('_')[1].encode('utf-8') logging.info('get username is {}'.format(self.username)) # print(self.username), 99999999999 if self.username in self.error_username: # 去掉拼id会失效的帐号 continue self.ck_str = self.account.redis_ck_get(self.username) # print self.ck_str self.headers['Cookie'] = self.ck_str if self.login_status_chk(): flag = True # print self.username * 100 logging.info( 'switching {} username {} success. '.format( self.module_name, self.username)) else: sql_res = self.account.sql_password(self.username) # print sql_res self.account.redis_ck_ex( self.username) # 更新该用户名的cookie失效时间 self.password = sql_res[1] self.ctmname = sql_res[0].encode('utf-8') # print self.password, self.username, self.ctmname l_login = liblogin.Login51(cn=self.ctmname, un=self.username, pw=self.password) self.ck_str = l_login.main(sleep=5) self.headers['Cookie'] = self.ck_str if self.login_status_chk(): self.account.redis_ck_set(self.username, self.ck_str) logging.info( '51job user {} auto login success'.format( self.username)) logging.info( 'switching {} username {} success. '.format( self.module_name, self.username)) flag = True return flag else: logging.critical('no account left for {}'.format( self.module_name)) # 这里需要发邮件警告。 return False except Exception, e: logging.critical('error msg is {}'.format(str(e)), exc_info=True) return False
def login(self): '''功能描述:判断登录状态处理登录过程,循环等待cookie更新直至登录cookie可用''' try: self.load_cookie(self.cookie_fpath) self.parse_cookie(self.cookie_fpath) flag = False if self.login_status_chk(): flag = True else: l_count = 0 while l_count <= 3: try: min_sleep = random.randint(10, 60) time.sleep(min_sleep * 60) l_count += 1 user_select = selectuser.Selcet_user('51job') sql_res = user_select.sql_password(self.username) self.password = sql_res[1] self.ctmname = sql_res[0] # try to login via liblogin l_login = liblogin.Login51( cn=self.ctmname.encode('utf-8'), un=self.username, pw=self.password) ck_str = l_login.login() with open(self.cookie_fpath, 'w+') as f: f.write(ck_str) self.load_cookie(self.cookie_fpath) if self.login_status_chk(): try: logging.info( "company name: {}, username: {} auto login success and count is {}, sleep time is {} min" .format(self.ctmname, self.username, l_count, min_sleep)) # self.send_mails('{} auto login success'.format(self.module_name), # "company name: {}, username: {} auto login success and count is {}, sleep time is {} min".format(self.ctmname, self.username, l_count, min_sleep)) except: pass flag = True if flag: break except Exception, e: logging.warning('auto try login error %s' % str(e)) pass if not flag: #exit(0) self.logout_at = datetime.datetime.now() # flag = False self.cookie_notice(0) count = 0 while not flag: try: count += 1 logging.info( 'the login action will be executed after %ds ...' % self.login_wait) time.sleep(self.login_wait) if os.path.exists( self.cookie_fpath): # 判断cookie文件是否存在 if os.path.getmtime(self.cookie_fpath ) > self.cookie_modtime: self.load_cookie(self.cookie_fpath) self.parse_cookie(self.cookie_fpath) logging.info( 'cookie file updated at %s' % time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) logging.info( 'try to login at the count %d ' % count) if self.login_status_chk(): flag = True self.cookie_notice(1) logging.info( 'success login at the count %d ' % count) else: if count % 240 == 0: self.cookie_notice(0) read_modtime = time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( os.path.getmtime( self.cookie_fpath))) record_modtime = time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(self.cookie_modtime)) logging.info( 'waite cookie update and modtime_read at %s ,modtime record at %s' % (read_modtime, record_modtime)) else: logging.info( 'cookie file in %s is not exist... ' % self.cookie_fpath) continue except Exception, e: logging.debug('single login error and msg is %s' % str(e)) self.login_at = datetime.datetime.now() self.logout_at = None