Пример #1
0
    def _wait_for_recognize_captcha(self, encoded_captcha):
        etcd = db.get_etcd_client(config.get('app.db.etcd'))
        key = 'yugong/toutiao/login/captcha/%d' % random.randint(0, 999999)
        img_name = str(random.random())[2:] + '.gif'
        local_file = config.APP_PATH + '/storage/cache/' + img_name
        with open(local_file, 'ab') as f:
            f.write(base64.b64decode(encoded_captcha.split('base64,')[1]))
        qiniu_url = uploader.upload(config.APP_PATH + '/storage/cache/' +
                                    img_name)
        logger.notice(
            '=================Waiting for recognize=================')
        logger.notice('[%s] [%s]' % (key, qiniu_url))
        link_url = config.get('app.toutiao.notify_url') % (qiniu_url, key)
        mail.send(
            config.get('app.toutiao.notify_receiver'), 'Yugong-captcha',
            "<h2>%s</h2><img src=\"%s\" /><a href=\"%s\">填写</a>" %
            (key, qiniu_url, link_url))
        rtn = etcd.watch(key)
        print(rtn)
        etcd.delete(key)
        return rtn.value

        # while not res:
        #     logger.info('waiting for recognize captcha')
        #     time.sleep(10)
        #     res = cache.get('captcha-%s' % key)
        # print(type(res))
        # if isinstance(res, bytes):
        #     res = bytes.decode(res)

        #res = input('Please input the captcha')
        #cache.delete('captcha-%s' % key)
        logger.info('Get recognized captcha [%s]' % res)
        return str(res)
Пример #2
0
    def _login(self):
        '''
        Login with cookie if cookie file exists, otherwise login with account and password, and
        save cookie to cookie file
        Login with phone number has not been handled
        :return:
        '''
        self._browser.get(self._login_url)
        cached_cookie = self._get_saved_cookie()
        if cached_cookie and len(cached_cookie) > 0:
            cookies = json.loads(cached_cookie)
            for cookie in cookies:
                self._browser.add_cookie(cookie)
            self._browser.get(self._graphic_url)
            time.sleep(5)
        # cookie has expired, remove it
        if 'login' in self._browser.current_url:
            self._browser.get(self._login_url)
            logger.notice('Login toutiao with cached cookie failed')
        else:
            logger.info('Login toutiao with cookie successfully')

        # cookie_file = (config.APP_PATH + '/storage/cache/toutiao/%s.txt') % config.get('app.toutiao.account')
        # print(cookie_file)
        # print(os.path.exists(cookie_file))
        # if os.path.exists(cookie_file):
        #     logger.info('Login toutiao with cookie file')
        #     with open(cookie_file) as f:
        #         cookies = json.loads(f.read())
        #         for cookie in cookies:
        #             self._browser.add_cookie(cookie)
        #         self._browser.get(self._graphic_url)
        #         time.sleep(5)
        #         # cookie has expired, remove it
        #         if 'login' in self._browser.current_url:
        #             self._browser.get(self._login_url)
        #             logger.notice('Login toutiao with cookie file failed')
        #             os.remove(cookie_file)
        #         else:
        #             logger.info('Login toutiao with cookie successfully')
        #             return

        while 'login' in self._browser.current_url:
            try:
                err = self._browser.find_element_by_class_name('error-msg')
                tips = err.text.strip('\t\r\n ')
                if '手机验证码' in tips:
                    phone_num = config.get('app.toutiao.phone')
                    logger.notice('Verify account by phone number')
                    phone = self._wait.until(
                        EC.presence_of_element_located((By.ID, 'mobile')))
                    code = self._wait.until(
                        EC.presence_of_element_located((By.ID, 'code')))
                    send_btn = self._wait.until(
                        EC.presence_of_element_located(
                            (By.CLASS_NAME, 'code-btn')))
                    submitBtn = self._wait.until(
                        EC.presence_of_element_located((By.NAME, 'submitBtn')))
                    phone.send_keys(phone_num)
                    send_btn.click()
                    received_code = self._wait_for_phone_num(phone_num)
                    code.send_keys(received_code)
                    submitBtn.click()
                    self._update_cookie()
                    break
            except NoSuchElementException as e:
                pass
            logger.info('Try to login toutiao [%s]' %
                        self._browser.current_url)
            name = self._wait.until(
                EC.presence_of_element_located((By.ID, 'account')))
            pwd = self._wait.until(
                EC.presence_of_element_located((By.ID, 'password')))
            captcha = self._wait.until(
                EC.presence_of_element_located((By.ID, 'captcha')))
            captcha_img = self._wait.until(
                EC.presence_of_element_located((By.CLASS_NAME, 'captcha')))
            submit = self._wait.until(
                EC.presence_of_element_located((By.CLASS_NAME, 'action-btn')))
            recognized_captcha = self._wait_for_recognize_captcha(
                captcha_img.get_attribute('src'))
            name.clear()
            pwd.clear()
            captcha.clear()
            name.send_keys(config.get('app.toutiao.account'))
            pwd.send_keys(config.get('app.toutiao.password'))
            captcha.send_keys(recognized_captcha)
            submit.submit()
            time.sleep(3)
            err = self._browser.find_element_by_class_name('error-msg')
            print(err)
        self._update_cookie()
        logger.info('login toutiao successfully')
Пример #3
0
    def _handle_command(self, commands, meta):
        '''
        :param commands:
        {
            "target": "client"   // manager
            "client": "wxext",
            "commands": [
                // details, parsed by the client
            ]
        }
        :param meta:
        :return:
        '''
        try:
            logger.info('Receive command: %r' % commands)
            if commands['target'] == 'client':
                if 'client' in commands and commands['client'] in self._clients:
                    self._clients[commands['client']].perform(
                        commands['commands'])

            elif commands['target'] == 'manager':
                for command in commands['commands']:
                    if command['type'] == 'operate_client':

                        if command['action'] == 'start':
                            if command['target'] in self._clients:
                                logger.notice('%s is already running...' %
                                              command['target'])
                            elif command['target'] in _available_clients:
                                self._clients[
                                    command['target']] = _available_clients[
                                        command['target']]()
                            else:
                                raise UnknowCommandException()
                        elif command['action'] == 'stop':
                            if command['target'] in self._clients:
                                rtn = self._clients[command['target']].quit()
                                logger.info('Stop [%s] with status [%s]' %
                                            (command['target'], rtn))
                            else:
                                logger.notice('[%s] is not running!' %
                                              command['target'])
                        elif command['action'] == 'restart':
                            if command['target'] in self._clients:
                                rtn = self._clients[command['target']].quit()
                                logger.info('Stop [%s] with status [%s]' %
                                            (command['target'], rtn))
                            self._clients[
                                command['target']] = _available_clients[
                                    command['target']]()
                        else:
                            raise UnknowCommandException(command)
                    else:
                        raise UnknowCommandException(command)
                pass
            pass
        except UnknowCommandException as e:
            logger.error('Unknow command:')
            logger.error(e)
        except Exception as e:
            logger.error(e)