def __del_cookie(self): ''' 删除cookie ''' if os.path.exists(self._cookie_dir + self._cookie_filename): os.remove(self._cookie_dir + self._cookie_filename) logger.info('cookie删除成功')
def __load_cookie(self): ''' 加载本地cookie ''' if not os.path.exists(self._cookie_dir + self._cookie_filename): raise FileNotFoundError('Not Found Cookie') with open(self._cookie_dir + self._cookie_filename, 'rb') as f: self._session.cookies.update(pickle.load(f)) logger.info('cookie加载成功')
def __save_cookie(self): ''' 保存cookie ''' directory = os.path.dirname(self._cookie_dir) if not os.path.exists(directory): os.makedirs(directory) with open(self._cookie_dir + self._cookie_filename, 'wb') as f: pickle.dump(self._session.cookies, f) logger.info('cookie保存成功')
def __get_mailcode(self): ''' 获取邮箱验证码 ''' email = self._config['email'] mode = self._config['mode'] if mode == '0': code = input('请输入[{0}]邮箱收到的验证码:'.format(email)) logger.info('输入的验证码为:%s', code) return code elif mode == '1': time.sleep(60) return read_mailcode()
def read_mailcode(): #读取配置文件 host = config.get('email', 'host') port = config.get('email', 'port') password = config.get('email', 'password') email = config.get('account', 'email') logger.info('正在获取[%s]的邮件验证码', email) #连接邮箱 conn = Imbox(host, email, password, True, port) messages = conn.messages(unread=True, sent_from='*****@*****.**', sent_to=email, date__on=datetime.date.today(), subject='GLaDOS Authentication') #筛选邮件 for uid, message in messages: message_dict = eval(str(message)) date = message_dict['date'] subject = message_dict['subject'] sent_from = message_dict['sent_from'][0]['email'] sent_to = message_dict['sent_to'][0]['email'] logger.debug('邮件简要信息:subject:%s, sent_from:%s, sent_to:%s, date:%s', subject, sent_from, sent_to, date) email_datetime = datetime.datetime.strptime( date, '%a, %d %b %Y %H:%M:%S +0000 (UTC)') + datetime.timedelta(hours=8) min_datetime = datetime.datetime.now() - datetime.timedelta(minutes=3) max_datetime = datetime.datetime.now() + datetime.timedelta(minutes=3) condition1 = sent_from == '*****@*****.**' and sent_to == email and subject == 'GLaDOS Authentication' condition2 = email_datetime >= min_datetime and email_datetime <= max_datetime if condition1 and condition2: body = message_dict['body']['plain'][0] logger.debug('找到符合条件的邮件,邮件ID:%s,邮件内容:%s', uid, body) mailcode = re.search(r'[0-9]\d*', body, flags=0).group(0) logger.info('邮件验证码为:%s', mailcode) conn.mark_seen(uid) conn.logout() return mailcode conn.logout()
def login(self): ''' 登录 :return: bool ''' if not self.__send_auth_code(): return mailcode = self.__get_mailcode() email = self._config['email'] host = self._config['host'] logger.info('正在登录[%s]账号', email) url = 'https://{0}/api/login'.format(host) payload = { 'email' : email, 'mailcode' : mailcode, 'method' : 'email', 'site' : 'glados.network' } logger.debug('登录请求参数:%s', payload) response = self._session.post(url, data=payload).json() logger.debug('登录响应参数:%s', response) if response['code'] == 0: logger.info('登录成功,返回消息:%s', response['message']) self.__save_cookie() return True else: logger.info('登录失败,返回消息:%s', response['message']) return False
def __send_auth_code(self): ''' 请求邮箱验证码 ''' email = self._config['email'] host = self._config['host'] logger.info('向邮箱[%s]发送验证码', email) url = 'https://{0}/api/authorization'.format(host) payload = { 'address' : email, 'site' : 'glados.network' } logger.debug('验证码获取请求参数:%s', payload) response = self._session.post(url, data=payload).json() logger.debug('验证码获取响应响应:%s', response) if response['code'] == 0: logger.info('验证码发送成功') return True else: logger.warn('验证码发送失败') return False
def logout(self): ''' 退出账号 :return: ''' email = self._config['email'] host = self._config['host'] try: self.__load_cookie() except FileNotFoundError: logger.info('[%s]不存在cookie,无需退出', email) return logger.info('正在进行[%s]账号的退出', email) url = 'https://{0}/api/logout'.format(host) response = self._session.post(url, allow_redirects=False) if response.status_code == 302: logger.info('退出账号成功') self.__del_cookie() else: logger.info('退出账号失败')
def checkin(self): ''' 签到 :return: ''' email = self._config['email'] host = self._config['host'] logger.info('正在进行[%s]账号的签到', email) url = 'https://{0}/api/user/checkin'.format(host) payload = { 'token' : 'glados_network' } logger.debug('签到请求参数:%s', payload) try: self.__load_cookie() except FileNotFoundError as e: logger.info(e) self.login() response = self._session.post(url, data=payload).json() logger.debug('签到响应参数:%s', response) code = response['code'] message = response['message'] if code == 0: logger.info('签到成功,返回消息:%s', message) elif code == 1: logger.info('已经签到过了,返回消息:%s', message) elif code == -2: logger.info('登录已失效,返回消息:%s', message) self.login() self.checkin() else: logger.info('其他,返回消息:%s', message)