Ejemplo n.º 1
0
def process_drive_links(update, context):
    if not update.message:
        return

    folder_ids = parse_entity_for_drive_id(update.message)

    if not folder_ids:
        return
    message = '📑 The following files were detected :\n'

    try:
        gd = GoogleDrive(update.effective_user.id)
    except Exception as e:
        update.message.reply_text(
            '🔸 Please make sure the SA archive has been uploaded and the collection folder has been configured.\n{}'
            .format(e))
        return

    for item in folder_ids:
        try:
            folder_name = gd.get_file_name(item)
        except Exception as e:
            update.message.reply_text(
                '🔸 Please make sure that the SA archive has been uplaoded and that the SA has permission to access the link.\n{}'
                .format(e))
            return
        message += '     <a href="https://drive.google.com/open?id={}">{}</a>\n'.format(
            item, html.escape(folder_name))
    message += '\n📂 Please select the target destination'
    fav_folder_ids = context.user_data.get(udkey_folders, None)

    callback_query_prefix = 'save_to_folder'
    page = 1
    if fav_folder_ids:
        page_data = []
        for item in fav_folder_ids:
            page_data.append({
                'text':
                simplified_path(fav_folder_ids[item]['path']),
                'data':
                '{}'.format(item)
            })
        inline_keyboard_drive_ids = get_inline_keyboard_pagination_data(
            callback_query_prefix,
            page_data,
            page=page,
            max_per_page=10,
        )
    else:
        inline_keyboard_drive_ids = [[
            InlineKeyboardButton(
                text='⚠️ Use /folders to add a favorite folder',
                callback_data='#')
        ]]
    inline_keyboard = inline_keyboard_drive_ids
    update.message.reply_text(
        message,
        parse_mode=ParseMode.HTML,
        disable_web_page_preview=True,
        reply_markup=InlineKeyboardMarkup(inline_keyboard))
Ejemplo n.º 2
0
def process_drive_links(update, context):
    if not update.message:
        return
    # if update.message.chat.id not in config.USER_IDS:
    #     logger.debug('Ignore message from {0}.'.format(update.message.chat.id))
    #     return

    folder_ids = parse_entity_for_drive_id(update.message)

    if not folder_ids:
        return
    message = '检测到以下文件:'

    try:
        gd = GoogleDrive(update.effective_user.id)
    except Exception as e:
        update.message.reply_text('请确认SA已正确上传,并配置收藏文件夹。\n{}'.format(e))
        return

    for item in folder_ids:
        try:
            folder_name = gd.get_file_name(item)
        except Exception as e:
            update.message.reply_text(
                '请确认SA已正确上传,并确认SA有访问该链接权限。\n{}'.format(e))
            return
        message += '<a href="https://drive.google.com/open?id={}">{}</a>\n'.format(
            item, html.escape(folder_name))
    message += '\n请选择目标团队盘'
    fav_folder_ids = context.user_data.get(udkey_folders, None)

    callback_query_prefix = 'save_to_folder'
    page = 1
    if fav_folder_ids:
        page_data = []
        for item in fav_folder_ids:
            page_data.append({
                'text':
                simplified_path(fav_folder_ids[item]['path']),
                'data':
                '{}'.format(item)
            })
        inline_keyboard_drive_ids = get_inline_keyboard_pagination_data(
            callback_query_prefix,
            page_data,
            page=page,
            max_per_page=10,
        )
    else:
        inline_keyboard_drive_ids = [[
            InlineKeyboardButton(text='未收藏团队盘,先收藏才能操作。', callback_data='#')
        ]]
    inline_keyboard = inline_keyboard_drive_ids
    update.message.reply_text(
        message,
        parse_mode=ParseMode.HTML,
        reply_markup=InlineKeyboardMarkup(inline_keyboard))
Ejemplo n.º 3
0
def chosen_folder(update, context):
    query = update.callback_query
    if query.message.chat_id < 0 and \
            (not query.message.reply_to_message or
             query.from_user.id != query.message.reply_to_message.from_user.id):
        alert_users(context, update.effective_user, 'invalid caller', query.data)
        query.answer(text='Yo-he!', show_alert=True)
        return
    if update.effective_user.id in config.USER_IDS\
            or (context.bot_data.get('vip', None) and update.effective_user.id in context.bot_data['vip']):
        max_folders = default_max_folders_vip
    else:
        max_folders = default_max_folders

    callback_query_prefix = 'chosen_folder'

    try:
        gd = GoogleDrive(update.effective_user.id)
    except Exception as e:
        context.bot.send_message(chat_id=update.effective_user.id,
                                 text='Please make sure the SA archive has been uploaded and the collection folder has been configured.\n'
                                      '<code>{}</code>'.format(html.escape(str(e))),
                                 parse_mode=ParseMode.HTML)
        return

    query = update.callback_query
    match = re.search(r'^{},(?P<folder_id>[\dA-Za-z\-_]+)$'.format(callback_query_prefix), query.data)
    if not match:
        alert_users(context, update.effective_user, 'invalid query', query.data)
        query.answer(text='Yo-he!', show_alert=True)
        return
    folder_id = match.group('folder_id')

    drive_ids_replace = context.user_data.get(udkey_fav_folders_replace, None)
    favourite_drive_ids = context.user_data.get(udkey_folders, {})
    new_fav_folders = copy.deepcopy(favourite_drive_ids)
    new_fav_folders.pop(drive_ids_replace, None)
    new_fav_folders_len = len(new_fav_folders)
    if new_fav_folders_len < max_folders:
        current_path_list = gd.get_file_path_from_id(folder_id)
        if not current_path_list:
            alert_users(context, update.effective_user, 'invalid folder id', query.data)
            query.answer(text='Yo-he!', show_alert=True)
            return
        current_path_list.reverse()
        new_fav_folders[folder_id] = {
            'name': current_path_list[-1]['name'],
            'path': '/' + '/'.join(item['name'] for item in current_path_list),
        }
        context.user_data[udkey_folders] = new_fav_folders
        context.user_data[udkey_fav_folders_replace] = None
        context.dispatcher.update_persistence()
        set_folders(update, context)
    else:
        query.answer(text='Maximum {}'.format(max_folders), show_alert=True)
    return
Ejemplo n.º 4
0
def chosen_folder(update, context):
    if update.effective_user.id in config.USER_IDS or update.effective_user.id in context.bot_data[
            'vip']:
        max_folders = default_max_folders_vip
    else:
        max_folders = default_max_folders

    callback_query_prefix = 'chosen_folder'

    try:
        gd = GoogleDrive(update.effective_user.id)
    except Exception as e:
        context.bot.send_message(
            chat_id=update.effective_user.id,
            text='请确认SA已正确上传,并配置收藏文件夹。\n<code>{}</code>'.format(e),
            parse_mode=ParseMode.HTML)
        return

    query = update.callback_query
    match = re.search(
        r'^{},(?P<folder_id>[\dA-Za-z\-_]+)$'.format(callback_query_prefix),
        query.data)
    if not match:
        alert_users(context, update.effective_user, 'invalid query',
                    query.data)
        query.answer(text='哟呵', show_alert=True)
        return
    folder_id = match.group('folder_id')

    drive_ids_replace = context.user_data.get(udkey_fav_folders_replace, None)
    favourite_drive_ids = context.user_data.get(udkey_folders, {})
    new_fav_folders = copy.deepcopy(favourite_drive_ids)
    new_fav_folders.pop(drive_ids_replace, None)
    new_fav_folders_len = len(new_fav_folders)
    if new_fav_folders_len < max_folders:
        current_path_list = gd.get_file_path_from_id(folder_id)
        if not current_path_list:
            alert_users(context, update.effective_user, 'invalid folder id',
                        query.data)
            query.answer(text='哟呵', show_alert=True)
            return
        current_path_list.reverse()
        new_fav_folders[folder_id] = {
            'name': current_path_list[-1]['name'],
            'path': '/' + '/'.join(item['name'] for item in current_path_list),
        }
        context.user_data[udkey_folders] = new_fav_folders
        context.user_data[udkey_fav_folders_replace] = None
        set_folders(update, context)
    else:
        query.answer(text='最多只能{}个'.format(max_folders), show_alert=True)
    return
Ejemplo n.º 5
0
    def run(self):
        update, context, folder_ids, text, dest_folder = self.args
        self.owner = update.effective_user.id
        thread_id = self.ident
        is_multiple_ids = len(folder_ids) > 1
        is_fclone = 'fclone' in os.path.basename(config.PATH_TO_GCLONE)
        chat_id = update.effective_chat.id
        user_id = update.effective_user.id
        gd = GoogleDrive(user_id)
        message = '目标目录:{}\n\n'.format(dest_folder['path'])
        inline_keyboard = InlineKeyboardMarkup([[
            InlineKeyboardButton(text=f'停止',
                                 callback_data=f'stop_task,{thread_id}')
        ]])

        reply_message_id = update.callback_query.message.reply_to_message.message_id \
            if update.callback_query.message.reply_to_message else None
        rsp = context.bot.send_message(chat_id=chat_id,
                                       text=message,
                                       parse_mode=ParseMode.HTML,
                                       disable_web_page_preview=True,
                                       reply_to_message_id=reply_message_id,
                                       reply_markup=inline_keyboard)
        rsp.done.wait(timeout=60)
        message_id = rsp.result().message_id

        for folder_id in folder_ids:
            destination_path = folder_ids[folder_id]

            command_line = [
                config.PATH_TO_GCLONE, 'copy',
                '--drive-server-side-across-configs', '-P', '--stats', '1s',
                '--ignore-existing'
            ]
            if config.GCLONE_PARA_OVERRIDE:
                command_line.extend(config.GCLONE_PARA_OVERRIDE)
            elif is_fclone is True:
                command_line += [
                    '--checkers=256', '--transfers=256',
                    '--drive-pacer-min-sleep=1ms', '--drive-pacer-burst=5000',
                    '--check-first'
                ]
            else:
                command_line += [
                    '--transfers',
                    '8',
                    '--tpslimit',
                    '6',
                ]
            gclone_config = os.path.join(config.BASE_PATH, 'gclone_config',
                                         str(update.effective_user.id),
                                         'current', 'rclone.conf')
            command_line += ['--config', gclone_config]
            command_line += [
                '{}:{{{}}}'.format('gc', folder_id),
                ('{}:{{{}}}/{}'.format('gc', dest_folder['folder_id'],
                                       destination_path))
            ]

            logger.debug('command line: ' + str(command_line))

            process = subprocess.Popen(command_line,
                                       bufsize=1,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.STDOUT,
                                       encoding='utf-8',
                                       errors='ignore',
                                       universal_newlines=True)
            progress_checked_files = 0
            progress_total_check_files = 0
            progress_transferred_file = 0
            progress_total_files = 0
            progress_file_percentage = 0
            progress_file_percentage_10 = 0
            progress_transferred_size = '0'
            progress_total_size = '0 Bytes'
            progress_speed = '-'
            progress_speed_file = '-'
            progress_eta = '-'
            progress_size_percentage_10 = 0
            regex_checked_files = r'Checks:\s+(\d+)\s+/\s+(\d+)'
            regex_total_files = r'Transferred:\s+(\d+) / (\d+), (\d+)%(?:,\s*([\d.]+\sFiles/s))?'
            regex_total_size = r'Transferred:[\s]+([\d.]+\s*[kMGTP]?) / ([\d.]+[\s]?[kMGTP]?Bytes),' \
                               r'\s*(?:\-|(\d+)\%),\s*([\d.]+\s*[kMGTP]?Bytes/s),\s*ETA\s*([\-0-9hmsdwy]+)'
            message_progress_last = ''
            message_progress = ''
            progress_update_time = datetime.datetime.now(
            ) - datetime.timedelta(minutes=5)
            while True:
                try:
                    line = process.stdout.readline()
                except Exception as e:
                    logger.debug(str(e))
                    if process.poll() is not None:
                        break
                    else:
                        continue
                if not line and process.poll() is not None:
                    break
                output = line.rstrip()
                if output:
                    # logger.debug(output)
                    match_total_files = re.search(regex_total_files, output)
                    if match_total_files:
                        progress_transferred_file = int(
                            match_total_files.group(1))
                        progress_total_files = int(match_total_files.group(2))
                        progress_file_percentage = int(
                            match_total_files.group(3))
                        progress_file_percentage_10 = progress_file_percentage // 10
                        if match_total_files.group(4):
                            progress_speed_file = match_total_files.group(4)
                    match_total_size = re.search(regex_total_size, output)
                    if match_total_size:
                        progress_transferred_size = match_total_size.group(1)
                        progress_total_size = match_total_size.group(2)
                        progress_size_percentage = int(
                            match_total_size.group(
                                3)) if match_total_size.group(3) else 0
                        progress_size_percentage_10 = progress_size_percentage // 10
                        progress_speed = match_total_size.group(4)
                        progress_eta = match_total_size.group(5)
                    match_checked_files = re.search(regex_checked_files,
                                                    output)
                    if match_checked_files:
                        progress_checked_files = int(
                            match_checked_files.group(1))
                        progress_total_check_files = int(
                            match_checked_files.group(2))
                    progress_max_percentage_10 = max(
                        progress_size_percentage_10,
                        progress_file_percentage_10)
                    message_progress = '<a href="https://drive.google.com/open?id={}">{}</a>\n' \
                                       '检查文件: <code>{} / {}</code>\n' \
                                       '文件数量: <code>{} / {}</code>\n' \
                                       '任务容量:<code>{} / {}</code>\n{}' \
                                       '复制速度:<code>{} ETA {}</code>\n' \
                                       '任务进度:<code>[{}] {: >4}%</code>' \
                        .format(
                        folder_id,
                        html.escape(destination_path),
                        progress_checked_files,
                        progress_total_check_files,
                        progress_transferred_file,
                        progress_total_files,
                        progress_transferred_size,
                        progress_total_size,
                        f'任务速度:<code>{progress_speed_file}</code>\n' if is_fclone is True else '',
                        progress_speed,
                        progress_eta,
                        '█' * progress_file_percentage_10 + '░' * (
                                progress_max_percentage_10 - progress_file_percentage_10) + ' ' * (
                                10 - progress_max_percentage_10),
                        progress_file_percentage)

                    match = re.search(
                        r'Failed to copy: failed to make directory', output)
                    if match:
                        message_progress = '{}\n<code>写入权限错误,请确认权限</code>'.format(
                            message_progress)
                        temp_message = '{}{}'.format(message, message_progress)
                        # logger.info('写入权限错误,请确认权限'.format())
                        try:
                            context.bot.edit_message_text(
                                chat_id=chat_id,
                                message_id=message_id,
                                text=temp_message,
                                parse_mode=ParseMode.HTML,
                                disable_web_page_preview=True,
                                reply_markup=inline_keyboard)
                        except Exception as e:
                            logger.debug(
                                'Error {} occurs when editing message {} for user {} in chat {}: \n{}'
                                .format(e, message_id, user_id, chat_id,
                                        temp_message))
                        process.terminate()
                        self.critical_fault = True
                        break

                    match = re.search(r"couldn't list directory", output)
                    if match:
                        message_progress = '{}\n<code>读取权限错误,请确认权限</code>'.format(
                            message_progress)
                        temp_message = '{}{}'.format(message, message_progress)
                        # logger.info('读取权限错误,请确认权限:')
                        try:
                            context.bot.edit_message_text(
                                chat_id=chat_id,
                                message_id=message_id,
                                text=temp_message,
                                parse_mode=ParseMode.HTML,
                                disable_web_page_preview=True,
                                reply_markup=inline_keyboard)
                        except Exception as e:
                            logger.debug(
                                'Error {} occurs when editing message {} for user {} in chat {}: \n{}'
                                .format(e, message_id, user_id, chat_id,
                                        temp_message))
                        process.terminate()
                        self.critical_fault = True
                        break

                    if message_progress != message_progress_last:
                        if datetime.datetime.now(
                        ) - progress_update_time > datetime.timedelta(
                                seconds=5):
                            temp_message = '{}{}'.format(
                                message, message_progress)
                            try:
                                context.bot.edit_message_text(
                                    chat_id=chat_id,
                                    message_id=message_id,
                                    text=temp_message,
                                    parse_mode=ParseMode.HTML,
                                    disable_web_page_preview=True,
                                    reply_markup=inline_keyboard)
                            except Exception as e:
                                logger.debug(
                                    'Error {} occurs when editing message {} for user {} in chat {}: \n{}'
                                    .format(e, message_id, user_id, chat_id,
                                            temp_message))
                            message_progress_last = message_progress
                            progress_update_time = datetime.datetime.now()

                    if self.critical_fault:
                        message_progress = '{}\n<code>用户终止</code>'.format(
                            message_progress)
                        process.terminate()
                        break

            rc = process.poll()
            message_progress_heading, message_progress_content = message_progress.split(
                '\n', 1)
            link_text = 'Unable to get link.'
            try:
                link = gd.get_folder_link(dest_folder['folder_id'],
                                          destination_path)
                if link:
                    link_text = '👉<a href="{}">Link</a>'.format(link)
            except Exception as e:
                logger.info(str(e))

            if self.critical_fault is True:
                message = '{}{}❌\n{}\n{}\n\n'.format(message,
                                                     message_progress_heading,
                                                     message_progress_content,
                                                     link_text)
            elif progress_file_percentage == 0 and progress_checked_files > 0:
                message = '{}{}✅\n已存在\n{}\n\n'.format(
                    message, message_progress_heading, link_text)
            else:
                message = '{}{}{}\n{}\n{}\n\n'.format(
                    message, message_progress_heading, '✅' if rc == 0 else '❌',
                    message_progress_content, link_text)

            try:
                context.bot.edit_message_text(chat_id=chat_id,
                                              message_id=message_id,
                                              text=message,
                                              parse_mode=ParseMode.HTML,
                                              disable_web_page_preview=True,
                                              reply_markup=inline_keyboard)
            except Exception as e:
                logger.debug(
                    'Error {} occurs when editing message {} for user {} in chat {}: \n{}'
                    .format(e, message_id, user_id, chat_id, message))

            if self.critical_fault is True:
                break

        message += 'Finished.'
        try:
            context.bot.edit_message_text(chat_id=chat_id,
                                          message_id=message_id,
                                          text=message,
                                          parse_mode=ParseMode.HTML,
                                          disable_web_page_preview=True)
        except Exception as e:
            logger.debug(
                'Error {} occurs when editing message {} for user {} in chat {}: \n{}'
                .format(e, message_id, user_id, chat_id, message))
        update.callback_query.message.edit_reply_markup(
            reply_markup=InlineKeyboardMarkup(
                [[InlineKeyboardButton(text='已完成', callback_data='cancel')]]))

        logger.debug('User {} has finished task {}: \n{}'.format(
            user_id, thread_id, message))
        tasks = thread_pool.get(user_id, None)
        if tasks:
            for t in tasks:
                if t.ident == thread_id:
                    tasks.remove(t)
                    return
Ejemplo n.º 6
0
def fire_save_files(update, context, folder_ids, text, dest_folder):
    is_multiple_ids = len(folder_ids) > 1
    chat_id = update.effective_user.id
    gd = GoogleDrive(chat_id)
    message = '目标目录:{}\n\n'.format(dest_folder['path'])
    rsp = context.bot.send_message(chat_id=chat_id,
                                   text=message,
                                   parse_mode=ParseMode.HTML)
    rsp.done.wait(timeout=60)
    message_id = rsp.result().message_id

    for folder_id in folder_ids:
        destination_path = folder_ids[folder_id]

        command_line = [
            config.PATH_TO_GCLONE, 'copy',
            '--drive-server-side-across-configs', '-P', '--stats', '1s',
            '--transfers', '6', '--tpslimit', '6', '--ignore-existing'
        ]
        gclone_config = os.path.join(config.BASE_PATH, 'gclone_config',
                                     str(update.effective_user.id), 'current',
                                     'rclone.conf')
        command_line += ['--config', gclone_config]
        command_line += [
            '{}:{{{}}}'.format('gc', folder_id),
            ('{}:{{{}}}/{}'.format('gc', dest_folder['folder_id'],
                                   destination_path))
        ]

        logger.debug('command line: ' + str(command_line))

        process = subprocess.Popen(command_line,
                                   bufsize=1,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT,
                                   encoding='utf-8',
                                   errors='ignore',
                                   universal_newlines=True)
        progress_checked_files = 0
        progress_total_check_files = 0
        progress_transferred_file = 0
        progress_total_files = 0
        progress_file_percentage = 0
        progress_file_percentage_10 = 0
        progress_transferred_size = '0'
        progress_total_size = '0 Bytes'
        progress_speed = '-'
        progress_eta = '-'
        progress_size_percentage_10 = 0
        regex_checked_files = r'Checks:\s+(\d+)\s+/\s+(\d+)'
        regex_total_files = r'Transferred:\s+(\d+) / (\d+), (\d+)%'
        regex_total_size = r'Transferred:[\s]+([\d.]+\s*[kMGTP]?) / ([\d.]+[\s]?[kMGTP]?Bytes),' \
                           r'\s*(?:\-|(\d+)\%),\s*([\d.]+\s*[kMGTP]?Bytes/s),\s*ETA\s*([\-0-9hmsdwy]+)'
        critical_fault = False
        message_progress_last = ''
        message_progress = ''
        progress_update_time = datetime.datetime.now() - datetime.timedelta(
            minutes=5)
        while True:
            try:
                line = process.stdout.readline()
            except Exception as e:
                logger.debug(str(e))
                if process.poll() is not None:
                    break
                else:
                    continue
            if not line and process.poll() is not None:
                break
            output = line.rstrip()
            if output:
                logger.debug(output)
                match_total_files = re.search(regex_total_files, output)
                if match_total_files:
                    progress_transferred_file = int(match_total_files.group(1))
                    progress_total_files = int(match_total_files.group(2))
                    progress_file_percentage = int(match_total_files.group(3))
                    progress_file_percentage_10 = progress_file_percentage // 10
                match_total_size = re.search(regex_total_size, output)
                if match_total_size:
                    progress_transferred_size = match_total_size.group(1)
                    progress_total_size = match_total_size.group(2)
                    progress_size_percentage = int(match_total_size.group(
                        3)) if match_total_size.group(3) else 0
                    progress_size_percentage_10 = progress_size_percentage // 10
                    progress_speed = match_total_size.group(4)
                    progress_eta = match_total_size.group(5)
                match_checked_files = re.search(regex_checked_files, output)
                if match_checked_files:
                    progress_checked_files = int(match_checked_files.group(1))
                    progress_total_check_files = int(
                        match_checked_files.group(2))
                progress_max_percentage_10 = max(progress_size_percentage_10,
                                                 progress_file_percentage_10)
                message_progress = '<a href="https://drive.google.com/open?id={}">{}</a>\n' \
                                   '检查文件: <code>{} / {}</code>\n' \
                                   '文件数量: <code>{} / {}</code>\n' \
                                   '任务容量:<code>{} / {}</code>\n' \
                                   '传输速度:<code>{} ETA {}</code>\n' \
                                   '任务进度:<code>[{}] {: >4}%</code>'\
                    .format(
                        folder_id,
                        destination_path,
                        progress_checked_files,
                        progress_total_check_files,
                        progress_transferred_file,
                        progress_total_files,
                        progress_transferred_size,
                        progress_total_size,
                        progress_speed,
                        progress_eta,
                        '█' * progress_file_percentage_10 + '░' * (
                            progress_max_percentage_10 - progress_file_percentage_10) + ' ' * (
                            10 - progress_max_percentage_10),
                        progress_file_percentage)

                match = re.search(r'Failed to copy: failed to make directory',
                                  output)
                if match:
                    message_progress = '{}\n<code>写入权限错误,请确认权限</code>'.format(
                        message_progress)
                    temp_message = '{}{}'.format(message, message_progress)
                    # logger.info('写入权限错误,请确认权限'.format())
                    context.bot.edit_message_text(chat_id=chat_id,
                                                  message_id=message_id,
                                                  text=temp_message,
                                                  parse_mode=ParseMode.HTML)
                    process.terminate()
                    critical_fault = True
                    break

                match = re.search(r"couldn't list directory", output)
                if match:
                    message_progress = '{}\n<code>读取权限错误,请确认权限</code>'.format(
                        message_progress)
                    temp_message = '{}{}'.format(message, message_progress)
                    # logger.info('读取权限错误,请确认权限:')
                    context.bot.edit_message_text(chat_id=chat_id,
                                                  message_id=message_id,
                                                  text=temp_message,
                                                  parse_mode=ParseMode.HTML)
                    process.terminate()
                    critical_fault = True
                    break

                if message_progress != message_progress_last:
                    if datetime.datetime.now(
                    ) - progress_update_time > datetime.timedelta(seconds=5):
                        temp_message = '{}{}'.format(message, message_progress)
                        context.bot.edit_message_text(
                            chat_id=chat_id,
                            message_id=message_id,
                            text=temp_message,
                            parse_mode=ParseMode.HTML)
                        message_progress_last = message_progress
                        progress_update_time = datetime.datetime.now()
        rc = process.poll()
        message_progress_heading, message_progress_content = message_progress.split(
            '\n', 1)
        link_text = 'Unable to get link.'
        try:
            link = gd.get_folder_link(dest_folder['folder_id'],
                                      destination_path)
            if link:
                link_text = '👉<a href="{}">Link</a>'.format(link)
        except Exception as e:
            logger.info(e)

        if critical_fault is True:
            message = '{}{}❌\n{}\n{}\n\n'.format(message,
                                                 message_progress_heading,
                                                 message_progress_content,
                                                 link_text)
        elif progress_file_percentage == 0 and progress_checked_files > 0:
            message = '{}{}✅\n已存在\n{}\n\n'.format(message,
                                                  message_progress_heading,
                                                  link_text)
        else:
            message = '{}{}{}\n{}\n{}\n\n'.format(message,
                                                  message_progress_heading,
                                                  '✅' if rc == 0 else '❌',
                                                  message_progress_content,
                                                  link_text)

        context.bot.edit_message_text(chat_id=chat_id,
                                      message_id=message_id,
                                      text=message,
                                      parse_mode=ParseMode.HTML)
        if critical_fault is True:
            break

    message_append = 'Finished.'
    message += message_append
    context.bot.edit_message_text(chat_id=chat_id,
                                  message_id=message_id,
                                  text=message,
                                  parse_mode=ParseMode.HTML)
    update.callback_query.message.edit_reply_markup(
        reply_markup=InlineKeyboardMarkup(
            [[InlineKeyboardButton(text='已完成', callback_data='cancel')]]))
def choose_folder(update, context):
    current_folder_id = ''
    folders = None

    try:
        gd = GoogleDrive(update.effective_user.id)
    except Exception as e:
        context.bot.send_message(
            chat_id=update.effective_user.id,
            text=
            'Please confirm that the SA has been uploaded correctly and configure the favorite folder.\n'
            '<code>{}</code>'.format(html.escape(str(e))),
            parse_mode=ParseMode.HTML)
        return

    if context.args:
        current_folder_id = context.args[0]
        try:
            gd.get_file_name(current_folder_id)
            folders = gd.list_folders(current_folder_id)
        except Exception as e:
            folders = gd.get_drives()
            current_folder_id = ''
            context.bot.send_message(chat_id=update.effective_user.id,
                                     text='error:\n<code>{}</code>'.format(
                                         html.escape(str(e))),
                                     parse_mode=ParseMode.HTML)

    callback_query_prefix = 'choose_folder'
    query = update.callback_query
    page = None
    message_id = -1
    if not query:
        rsp = update.message.reply_text('Get catalog...')
        rsp.done.wait(timeout=60)
        message_id = rsp.result().message_id
        if not folders:
            folders = gd.get_drives()
            context.user_data[udkey_folders_cache] = copy.deepcopy(folders)

    if query:
        logger.debug('{}: {}'.format(update.effective_user.id, query.data))
        if query.message.chat_id < 0 and \
                (not query.message.reply_to_message or
                 query.from_user.id != query.message.reply_to_message.from_user.id):
            alert_users(context, update.effective_user, 'invalid caller',
                        query.data)
            query.answer(text='Yo ha', show_alert=True)
            return
        message_id = query.message.message_id
        match = re.search(
            r'^(?P<un>un)?{}(?P<replace>_replace)?(?:_page#(?P<page>\d+))?'
            r'(?:,(?P<folder_id>[\dA-Za-z\-_]+))?$'.format(
                callback_query_prefix), query.data)
        if match:
            match_folder_id = match.group('folder_id')
            if match_folder_id:
                current_folder_id = match_folder_id
                try:
                    gd.get_file_name(current_folder_id)
                    folders = gd.list_folders(match_folder_id)
                except Exception as e:
                    folders = gd.get_drives()
                    current_folder_id = ''
                    context.bot.send_message(
                        chat_id=update.effective_user.id,
                        text='error:\n<code>{}</code>'.format(
                            html.escape(str(e))),
                        parse_mode=ParseMode.HTML)
                context.user_data[udkey_folders_cache] = copy.deepcopy(folders)
                if not folders:
                    folders = {'#': '(No subfolders)'}
                match_folder_id_replace = match.group('replace')
                if match_folder_id_replace:
                    context.user_data[
                        udkey_fav_folders_replace] = match_folder_id
            if match.group('page'):
                page = int(match.group('page'))
            if not folders and match.group('page'):
                folders = context.user_data.get(udkey_folders_cache, None)
            if not folders:
                folders = gd.get_drives()
                context.user_data[udkey_folders_cache] = copy.deepcopy(folders)
            if not folders:
                folders = {
                    '#':
                    'If the team disk is not saved, please save it before operation.'
                }
        else:
            alert_users(context, update.effective_user, 'invalid query data',
                        query.data)
            query.answer(text='Yo ha', show_alert=True)
            return

    if not page:
        page = 1

    folders_len = len(folders)
    page_data = []
    for item in folders:
        page_data.append({'text': folders[item], 'data': item})

    page_data_chosen = list(context.user_data.get(udkey_folders, {}))
    inline_keyboard_drive_ids = get_inline_keyboard_pagination_data(
        callback_query_prefix,
        page_data,
        page_data_chosen=page_data_chosen,
        page=page,
        max_per_page=10,
    )

    if current_folder_id:
        current_path = ''
        current_path_list = gd.get_file_path_from_id(current_folder_id)
        if current_path_list:
            current_folder_name = current_path_list[0]['name']
            for item in current_path_list:
                current_path = '/{}{}'.format(item['name'], current_path)
            if len(current_path_list) > 1:
                inline_keyboard_drive_ids.insert(0, [
                    InlineKeyboardButton(
                        '📁' + current_path,
                        callback_data='{},{}'.format(
                            callback_query_prefix,
                            current_path_list[1]['folder_id']))
                ])
            else:
                inline_keyboard_drive_ids.insert(0, [
                    InlineKeyboardButton('📁' + current_path,
                                         callback_data=callback_query_prefix)
                ])
            inline_keyboard_drive_ids.append([
                InlineKeyboardButton(
                    'Select this folder({})'.format(current_folder_name),
                    callback_data='chosen_folder,{}'.format(current_folder_id))
            ])
    inline_keyboard_drive_ids.append([
        InlineKeyboardButton(
            'Back to top',
            callback_data='choose_folder' if current_folder_id else '#'),
        InlineKeyboardButton('cancel', callback_data='cancel')
    ])
    context.bot.edit_message_text(
        chat_id=update.effective_chat.id,
        message_id=message_id,
        text='Select the directory to save, a total of {} subdirectories'.
        format(folders_len),
        reply_markup=InlineKeyboardMarkup(inline_keyboard_drive_ids))
Ejemplo n.º 8
0
    def run(self):
        update, context, folder_ids, text, dest_folder = self.args
        self.owner = update.effective_user.id
        thread_id = self.ident
        is_multiple_ids = len(folder_ids) > 1
        is_fclone = 'fclone' in os.path.basename(config.PATH_TO_GCLONE)
        chat_id = update.effective_chat.id
        user_id = update.effective_user.id
        gd = GoogleDrive(user_id)
        message = '╭──────⌈ 🅲🅻🅾🅽🅴 ⌋──────╮\n│\n├ 📂 🅷🅴🅳🅴🅵:{}\n'.format(
            dest_folder['path'])
        inline_keyboard = InlineKeyboardMarkup([[
            InlineKeyboardButton(text=f'🚫 Dur',
                                 callback_data=f'stop_task,{thread_id}')
        ]])

        reply_message_id = update.callback_query.message.reply_to_message.message_id \
            if update.callback_query.message.reply_to_message else None
        rsp = context.bot.send_message(chat_id=chat_id,
                                       text=message,
                                       parse_mode=ParseMode.HTML,
                                       disable_web_page_preview=True,
                                       reply_to_message_id=reply_message_id,
                                       reply_markup=inline_keyboard)
        rsp.done.wait(timeout=60)
        message_id = rsp.result().message_id

        for folder_id in folder_ids:
            destination_path = folder_ids[folder_id]

            command_line = [
                config.PATH_TO_GCLONE, 'copy',
                '--drive-server-side-across-configs', '-P', '--stats', '1s',
                '--ignore-existing'
            ]
            if config.GCLONE_PARA_OVERRIDE:
                command_line.extend(config.GCLONE_PARA_OVERRIDE)
            elif is_fclone is True:
                command_line += [
                    '--checkers=256', '--transfers=256',
                    '--drive-pacer-min-sleep=1ms', '--drive-pacer-burst=5000',
                    '--check-first'
                ]
            else:
                command_line += [
                    '--transfers',
                    '8',
                    '--tpslimit',
                    '6',
                ]
            gclone_config = os.path.join(config.BASE_PATH, 'gclone_config',
                                         str(update.effective_user.id),
                                         'current', 'rclone.conf')
            command_line += ['--config', gclone_config]
            command_line += [
                '{}:{{{}}}'.format('gc', folder_id),
                ('{}:{{{}}}/{}'.format('gc', dest_folder['folder_id'],
                                       destination_path))
            ]

            logger.debug('command line: ' + str(command_line))

            process = subprocess.Popen(command_line,
                                       bufsize=1,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.STDOUT,
                                       encoding='utf-8',
                                       errors='ignore',
                                       universal_newlines=True)
            progress_checked_files = 0
            progress_total_check_files = 0
            progress_transferred_file = 0
            progress_total_files = 0
            progress_file_percentage = 0
            progress_file_percentage_10 = 0
            progress_transferred_size = '0'
            progress_total_size = '0 Bytes'
            progress_speed = '-'
            progress_speed_file = '-'
            progress_eta = '-'
            progress_size_percentage_10 = 0
            regex_checked_files = r'Checks:\s+(\d+)\s+/\s+(\d+)'
            regex_total_files = r'Transferred:\s+(\d+) / (\d+), (\d+)%(?:,\s*([\d.]+\sFiles/s))?'
            regex_total_size = r'Transferred:[\s]+([\d.]+\s*[kMGTP]?) / ([\d.]+[\s]?[kMGTP]?Bytes),' \
                               r'\s*(?:\-|(\d+)\%),\s*([\d.]+\s*[kMGTP]?Bytes/s),\s*ETA\s*([\-0-9hmsdwy]+)'
            message_progress_last = ''
            message_progress = ''
            progress_update_time = datetime.datetime.now(
            ) - datetime.timedelta(minutes=5)
            while True:
                try:
                    line = process.stdout.readline()
                except Exception as e:
                    logger.debug(str(e))
                    if process.poll() is not None:
                        break
                    else:
                        continue
                if not line and process.poll() is not None:
                    break
                output = line.rstrip()
                if output:
                    # logger.debug(output)
                    match_total_files = re.search(regex_total_files, output)
                    if match_total_files:
                        progress_transferred_file = int(
                            match_total_files.group(1))
                        progress_total_files = int(match_total_files.group(2))
                        progress_file_percentage = int(
                            match_total_files.group(3))
                        progress_file_percentage_10 = progress_file_percentage // 10
                        if match_total_files.group(4):
                            progress_speed_file = match_total_files.group(4)
                    match_total_size = re.search(regex_total_size, output)
                    if match_total_size:
                        progress_transferred_size = match_total_size.group(1)
                        progress_total_size = match_total_size.group(2)
                        progress_size_percentage = int(
                            match_total_size.group(
                                3)) if match_total_size.group(3) else 0
                        progress_size_percentage_10 = progress_size_percentage // 10
                        progress_speed = match_total_size.group(4)
                        progress_eta = match_total_size.group(5)
                    match_checked_files = re.search(regex_checked_files,
                                                    output)
                    if match_checked_files:
                        progress_checked_files = int(
                            match_checked_files.group(1))
                        progress_total_check_files = int(
                            match_checked_files.group(2))
                    progress_max_percentage_10 = max(
                        progress_size_percentage_10,
                        progress_file_percentage_10)
                    message_progress = '├ 🗂  𝗞𝗮𝘆𝗻𝗮𝗸 : <a href="https://drive.google.com/open?id={}">{}</a>\n│\n' \
                                       '├ ✔️ 𝐃𝐨𝐠𝐫𝐮𝐥𝐚𝐦𝐚: <code>{} / {}</code>\n' \
                                       '├ 📥 𝐓𝐫𝐚𝐧𝐬𝐟𝐞𝐫: <code>{} / {}</code>\n' \
                                       '├ 📦 𝐁𝐨𝐲𝐮𝐭:<code>{} / {}</code>\n{}' \
                                       '├ ⚡️ 𝐇ı𝐳:<code>{}</code> \n├⏳  ETA: <code>{}</code>\n' \
                                       '├ ⛩  𝐈𝐥𝐞𝐫𝐥𝐞𝐦𝐞:[<code>{}</code>] {: >4}%\n│\n' \
                                       '├──────⌈ ⚡️@kamileecher1⚡️ ⌋──────' \
                        .format(
                        folder_id,
                        html.escape(destination_path),
                        progress_checked_files,
                        progress_total_check_files,
                        progress_transferred_file,
                        progress_total_files,
                        progress_transferred_size,
                        progress_total_size,
                        f'Speed:<code>{progress_speed_file}</code>\n' if is_fclone is True else '',
                        progress_speed,
                        progress_eta,
                        '●' * progress_file_percentage_10 + '○' * (
                                progress_max_percentage_10 - progress_file_percentage_10) + ' ' * (
                                10 - progress_max_percentage_10),
                        progress_file_percentage)

                    match = re.search(r'𝗞𝗼𝗽𝘆𝗮𝗹𝗮𝗻𝗮𝗺𝗮𝗱ı: 𝗱𝗶𝘇𝗶𝗻 𝗼𝗹𝘂ş𝘁𝘂𝗿𝘂𝗹𝗮𝗺𝗮𝗱ı',
                                      output)
                    if match:
                        message_progress = '{}\n│<code>İ𝘇𝗶𝗻 𝗵𝗮𝘁𝗮𝘀ı 𝘆𝗮𝘇, 𝗹ü𝘁𝗳𝗲𝗻 𝗶𝘇𝗶𝗻𝗹𝗲𝗿𝗶 𝗸𝗼𝗻𝘁𝗿𝗼𝗹 𝗲𝘁</code>'.format(
                            message_progress)
                        temp_message = '{}{}'.format(message, message_progress)
                        # logger.info('写入权限错误,请确认权限'.format())
                        try:
                            context.bot.edit_message_text(
                                chat_id=chat_id,
                                message_id=message_id,
                                text=temp_message,
                                parse_mode=ParseMode.HTML,
                                disable_web_page_preview=True,
                                reply_markup=inline_keyboard)
                        except Exception as e:
                            logger.debug(
                                'Error {} occurs when editing message {} for user {} in chat {}: \n│{}'
                                .format(e, message_id, user_id, chat_id,
                                        temp_message))
                        process.terminate()
                        self.critical_fault = True
                        break

                    match = re.search(r"𝗱𝗶𝘇𝗶𝗻 𝗹𝗶𝘀𝘁𝗲𝗹𝗲𝗻𝗲𝗺𝗲𝗱𝗶", output)
                    if match:
                        message_progress = '{}\n│<code>İ𝘇𝗶𝗻 𝗵𝗮𝘁𝗮𝘀ı 𝘆𝗮𝘇, 𝗹ü𝘁𝗳𝗲𝗻 𝗶𝘇𝗶𝗻𝗹𝗲𝗿𝗶 𝗸𝗼𝗻𝘁𝗿𝗼𝗹 𝗲𝘁</code>'.format(
                            message_progress)
                        temp_message = '{}{}'.format(message, message_progress)
                        # logger.info('读取权限错误,请确认权限:')
                        try:
                            context.bot.edit_message_text(
                                chat_id=chat_id,
                                message_id=message_id,
                                text=temp_message,
                                parse_mode=ParseMode.HTML,
                                disable_web_page_preview=True,
                                reply_markup=inline_keyboard)
                        except Exception as e:
                            logger.debug(
                                'Error {} occurs when editing message {} for user {} in chat {}: \n│{}'
                                .format(e, message_id, user_id, chat_id,
                                        temp_message))
                        process.terminate()
                        self.critical_fault = True
                        break

                    if message_progress != message_progress_last:
                        if datetime.datetime.now(
                        ) - progress_update_time > datetime.timedelta(
                                seconds=5):
                            temp_message = '{}{}'.format(
                                message, message_progress)
                            try:
                                context.bot.edit_message_text(
                                    chat_id=chat_id,
                                    message_id=message_id,
                                    text=temp_message,
                                    parse_mode=ParseMode.HTML,
                                    disable_web_page_preview=True,
                                    reply_markup=inline_keyboard)
                            except Exception as e:
                                logger.debug(
                                    'Error {} occurs when editing message {} for user {} in chat {}: \n│{}'
                                    .format(e, message_id, user_id, chat_id,
                                            temp_message))
                            message_progress_last = message_progress
                            progress_update_time = datetime.datetime.now()

                    if self.critical_fault:
                        message_progress = '{}\n│\n│ 𝗞𝘂𝗹𝗹𝗮𝗻ı𝗰ı 𝘀𝗼𝗻𝗹𝗮𝗻𝗱ı𝗿ı𝗹𝗱ı'.format(
                            message_progress)
                        process.terminate()
                        break

            rc = process.poll()
            message_progress_heading, message_progress_content = message_progress.split(
                '\n│', 1)
            link_text = '𝗕𝗮𝗴𝗹𝗮𝗻𝘁ı 𝗮𝗹ı𝗻𝗮𝗺ı𝘆𝗼𝗿.'
            try:
                link = gd.get_folder_link(dest_folder['folder_id'],
                                          destination_path)
                if link:
                    link_text = '\n│ \n│      🔻 <a href="{}">𝔻𝕣𝕚𝕧𝕖 𝕃𝕚𝕟𝕜</a> 🔺'.format(
                        link)
            except Exception as e:
                logger.info(str(e))

            if self.critical_fault is True:
                message = '{}{} ❌\n│{}\n│{}\n│'.format(
                    message, message_progress_heading,
                    message_progress_content, link_text)
            elif progress_file_percentage == 0 and progress_checked_files > 0:
                message = '{}{} ✅\n│ 𝐝𝐨𝐬𝐲𝐚 𝐳𝐚𝐭𝐞𝐧 𝐦𝐞𝐯𝐜𝐮𝐭!\n│ {}\n│'.format(
                    message, message_progress_heading, link_text)
            else:
                message = '{}{}{}\n│{}\n│{}\n│\n│'.format(
                    message, message_progress_heading, '✅' if rc == 0 else '❌',
                    message_progress_content, link_text)

            try:
                context.bot.edit_message_text(chat_id=chat_id,
                                              message_id=message_id,
                                              text=message,
                                              parse_mode=ParseMode.HTML,
                                              disable_web_page_preview=True,
                                              reply_markup=inline_keyboard)
            except Exception as e:
                logger.debug(
                    'Error {} occurs when editing message {} for user {} in chat {}: \n│{}'
                    .format(e, message_id, user_id, chat_id, message))

            if self.critical_fault is True:
                break

        message += '\n╰──────⌈ 🅱🅸🆃🆃🅸 ⌋──────╯'
        try:
            context.bot.edit_message_text(chat_id=chat_id,
                                          message_id=message_id,
                                          text=message,
                                          parse_mode=ParseMode.HTML,
                                          disable_web_page_preview=True)
        except Exception as e:
            logger.debug(
                'Error {} occurs when editing message {} for user {} in chat {}: \n│{}'
                .format(e, message_id, user_id, chat_id, message))
        update.callback_query.message.edit_reply_markup(
            reply_markup=InlineKeyboardMarkup([[
                InlineKeyboardButton(text='🅱🅸🆃🆃🅸', callback_data='cancel')
            ]]))

        logger.debug('User {} 𝕘ö𝕣𝕖𝕧𝕚 𝕓𝕚𝕥𝕚𝕣𝕕𝕚 {}: \n│{}'.format(
            user_id, thread_id, message))
        tasks = thread_pool.get(user_id, None)
        if tasks:
            for t in tasks:
                if t.ident == thread_id:
                    tasks.remove(t)
                    return
Ejemplo n.º 9
0
def process_drive_links(update, context):
    if not update.message:
        return

    folder_ids = parse_entity_for_drive_id(update.message)

    if not folder_ids:
        return
    message = '📑 ᴀŞᴀĞɪᴅᴀᴋɪ ᴅᴏꜱʏᴀʟᴀʀ ᴀʟɢɪʟᴀɴᴅɪ :\n'

    try:
        gd = GoogleDrive(update.effective_user.id)
    except Exception as e:
        update.message.reply_text(
            '🔸 𝕷ü𝖙𝖋𝖊𝖓 𝕾𝕬 𝖞ü𝖐𝖑𝖊𝖓𝖉𝖎ğ𝖎𝖓𝖉𝖊𝖓 𝖛𝖊 𝕾𝕬"𝖓ı𝖓 𝖇𝖆ğ𝖑𝖆𝖓𝖙ı𝖞𝖆 𝖊𝖗𝖎ş𝖎𝖒 𝖎𝖟𝖓𝖎𝖓𝖊 𝖘𝖆𝖍𝖎𝖕 𝖔𝖑𝖉𝖚ğ𝖚𝖓𝖉𝖆𝖓 𝖊𝖒𝖎𝖓 𝖔𝖑𝖚𝖓..\n{}'
            .format(e))
        return

    for item in folder_ids:
        try:
            folder_name = gd.get_file_name(item)
        except Exception as e:
            update.message.reply_text(
                '🔸 𝕷ü𝖙𝖋𝖊𝖓 𝕾𝕬 𝖞ü𝖐𝖑𝖊𝖓𝖉𝖎ğ𝖎𝖓𝖉𝖊𝖓 𝖛𝖊 𝕾𝕬"𝖓ı𝖓 𝖇𝖆ğ𝖑𝖆𝖓𝖙ı𝖞𝖆 𝖊𝖗𝖎ş𝖎𝖒 𝖎𝖟𝖓𝖎𝖓𝖊 𝖘𝖆𝖍𝖎𝖕 𝖔𝖑𝖉𝖚ğ𝖚𝖓𝖉𝖆𝖓 𝖊𝖒𝖎𝖓 𝖔𝖑𝖚𝖓..\n{}'
                .format(e))
            return
        message += '     <a href="https://drive.google.com/open?id={}">{}</a>\n'.format(
            item, html.escape(folder_name))
    message += '\n📂 Ꝉüէƒҽղ հҽժҽƒ հҽժҽƒ ʂҽçìղ'
    fav_folder_ids = context.user_data.get(udkey_folders, None)

    callback_query_prefix = 'save_to_folder'
    page = 1
    if fav_folder_ids:
        page_data = []
        for item in fav_folder_ids:
            page_data.append({
                'text':
                simplified_path(fav_folder_ids[item]['path']),
                'data':
                '{}'.format(item)
            })
        inline_keyboard_drive_ids = get_inline_keyboard_pagination_data(
            callback_query_prefix,
            page_data,
            page=page,
            max_per_page=10,
        )
    else:
        inline_keyboard_drive_ids = [[
            InlineKeyboardButton(
                text=
                '⚠️ 𝗳𝗮𝘃𝗼𝗿𝗶 𝗸𝗹𝗮𝘀ö𝗿 𝗲𝗸𝗹𝗲𝗺𝗲𝗸 𝗶ç𝗶𝗻 /folders [̲̅k][̲̅u][̲̅l][̲̅l][̲̅a][̲̅n]',
                callback_data='#')
        ]]
    inline_keyboard = inline_keyboard_drive_ids
    update.message.reply_text(
        message,
        parse_mode=ParseMode.HTML,
        disable_web_page_preview=True,
        reply_markup=InlineKeyboardMarkup(inline_keyboard))
Ejemplo n.º 10
0
def choose_folder(update, context):
    current_folder_id = ''
    folders = None

    try:
        gd = GoogleDrive(update.effective_user.id)
    except Exception as e:
        context.bot.send_message(chat_id=update.effective_user.id,
                                 text='请确认SA已正确上传,并配置收藏文件夹。\n'
                                 '<code>{}</code>'.format(html.escape(str(e))),
                                 parse_mode=ParseMode.HTML)
        return

    if context.args:
        current_folder_id = context.args[0]
        try:
            gd.get_file_name(current_folder_id)
            folders = gd.list_folders(current_folder_id)
        except Exception as e:
            folders = gd.get_drives()
            current_folder_id = ''
            context.bot.send_message(chat_id=update.effective_user.id,
                                     text='错误:\n<code>{}</code>'.format(
                                         html.escape(str(e))),
                                     parse_mode=ParseMode.HTML)

    callback_query_prefix = 'choose_folder'
    query = update.callback_query
    page = None
    message_id = -1
    if not query:
        rsp = update.message.reply_text('获取目录...')
        rsp.done.wait(timeout=60)
        message_id = rsp.result().message_id
        if not folders:
            # folders = context.user_data.get(udkey_fav_drives, None)
            folders = gd.get_drives()
            context.user_data[udkey_folders_cache] = copy.deepcopy(folders)

    if query:
        logger.debug('{}: {}'.format(update.effective_user.id, query.data))
        if query.message.chat_id < 0 and \
                (not query.message.reply_to_message or
                 query.from_user.id != query.message.reply_to_message.from_user.id):
            alert_users(context, update.effective_user, 'invalid caller',
                        query.data)
            query.answer(text='哟呵', show_alert=True)
            return
        message_id = query.message.message_id
        match = re.search(
            r'^(?P<un>un)?{}(?P<replace>_replace)?(?:_page#(?P<page>\d+))?'
            r'(?:,(?P<folder_id>[\dA-Za-z\-_]+))?$'.format(
                callback_query_prefix), query.data)
        if match:
            match_folder_id = match.group('folder_id')
            if match_folder_id:
                current_folder_id = match_folder_id
                try:
                    gd.get_file_name(current_folder_id)
                    folders = gd.list_folders(match_folder_id)
                except Exception as e:
                    folders = gd.get_drives()
                    current_folder_id = ''
                    context.bot.send_message(
                        chat_id=update.effective_user.id,
                        text='错误:\n<code>{}</code>'.format(html.escape(
                            str(e))),
                        parse_mode=ParseMode.HTML)
                context.user_data[udkey_folders_cache] = copy.deepcopy(folders)
                if not folders:
                    folders = {'#': '(无子文件夹)'}
                match_folder_id_replace = match.group('replace')
                if match_folder_id_replace:
                    context.user_data[
                        udkey_fav_folders_replace] = match_folder_id
            if match.group('page'):
                page = int(match.group('page'))
            if not folders and match.group('page'):
                folders = context.user_data.get(udkey_folders_cache, None)
            if not folders:
                # folders = context.user_data.get(udkey_fav_drives, None)
                folders = gd.get_drives()
                context.user_data[udkey_folders_cache] = copy.deepcopy(folders)
            if not folders:
                folders = {'#': '未收藏团队盘,先收藏才能操作。'}
        else:
            alert_users(context, update.effective_user, 'invalid query data',
                        query.data)
            query.answer(text='哟呵', show_alert=True)
            return

    if not page:
        page = 1

    folders_len = len(folders)
    page_data = []
    for item in folders:
        page_data.append({'text': folders[item], 'data': item})

    page_data_chosen = list(context.user_data.get(udkey_folders, {}))
    inline_keyboard_drive_ids = get_inline_keyboard_pagination_data(
        callback_query_prefix,
        page_data,
        page_data_chosen=page_data_chosen,
        page=page,
        max_per_page=10,
    )

    if current_folder_id:
        current_path = ''
        current_path_list = gd.get_file_path_from_id(current_folder_id)
        if current_path_list:
            current_folder_name = current_path_list[0]['name']
            for item in current_path_list:
                current_path = '/{}{}'.format(item['name'], current_path)
            if len(current_path_list) > 1:
                inline_keyboard_drive_ids.insert(0, [
                    InlineKeyboardButton(
                        '📁' + current_path,
                        callback_data='{},{}'.format(
                            callback_query_prefix,
                            current_path_list[1]['folder_id']))
                ])
            else:
                inline_keyboard_drive_ids.insert(0, [
                    InlineKeyboardButton('📁' + current_path,
                                         callback_data=callback_query_prefix)
                ])
            inline_keyboard_drive_ids.append([
                InlineKeyboardButton(
                    '选择本文件夹({})'.format(current_folder_name),
                    callback_data='chosen_folder,{}'.format(current_folder_id))
            ])
    inline_keyboard_drive_ids.append([
        InlineKeyboardButton(
            '返回顶层',
            callback_data='choose_folder' if current_folder_id else '#'),
        InlineKeyboardButton('取消', callback_data='cancel')
    ])
    context.bot.edit_message_text(
        chat_id=update.effective_chat.id,
        message_id=message_id,
        text='选择要保存的目录,共{}个子目录'.format(folders_len),
        reply_markup=InlineKeyboardMarkup(inline_keyboard_drive_ids))
Ejemplo n.º 11
0
def choose_folder(update, context):
    current_folder_id = ''
    folders = None

    try:
        gd = GoogleDrive(update.effective_user.id)
    except Exception as e:
        context.bot.send_message(
            chat_id=update.effective_user.id,
            text=
            '🔸 𝗟ü𝘁𝗳𝗲𝗻 𝗦𝗔  𝘆ü𝗸𝗹𝗲𝗻𝗱𝗶ğ𝗶𝗻𝗱𝗲𝗻 𝘃𝗲 𝗸𝗹𝗮𝘀ö𝗿ü𝗻ü𝗻 𝘆𝗮𝗽ı𝗹𝗮𝗻𝗱ı𝗿ı𝗹𝗱ığı𝗻𝗱𝗮𝗻 𝗲𝗺𝗶𝗻 𝗼𝗹𝘂𝗻..\n'
            '<code>{}</code>'.format(html.escape(str(e))),
            parse_mode=ParseMode.HTML)
        return

    if context.args:
        current_folder_id = context.args[0]
        try:
            gd.get_file_name(current_folder_id)
            folders = gd.list_folders(current_folder_id)
        except Exception as e:
            folders = gd.get_drives()
            current_folder_id = ''
            context.bot.send_message(chat_id=update.effective_user.id,
                                     text='Error:\n<code>{}</code>'.format(
                                         html.escape(str(e))),
                                     parse_mode=ParseMode.HTML)

    callback_query_prefix = 'choose_folder'
    query = update.callback_query
    page = None
    message_id = -1
    if not query:
        rsp = update.message.reply_text('⚙️ 𝕯𝖎𝖟𝖎𝖓 𝖆𝖑ı𝖓ı𝖞𝖔𝖗...')
        rsp.done.wait(timeout=60)
        message_id = rsp.result().message_id
        if not folders:
            folders = gd.get_drives()
            context.user_data[udkey_folders_cache] = copy.deepcopy(folders)

    if query:
        logger.debug('{}: {}'.format(update.effective_user.id, query.data))
        if query.message.chat_id < 0 and \
                (not query.message.reply_to_message or
                 query.from_user.id != query.message.reply_to_message.from_user.id):
            alert_users(context, update.effective_user, 'invalid caller',
                        query.data)
            query.answer(text='Yo-he!', show_alert=True)
            return
        message_id = query.message.message_id
        match = re.search(
            r'^(?P<un>un)?{}(?P<replace>_replace)?(?:_page#(?P<page>\d+))?'
            r'(?:,(?P<folder_id>[\dA-Za-z\-_]+))?$'.format(
                callback_query_prefix), query.data)
        if match:
            match_folder_id = match.group('folder_id')
            if match_folder_id:
                current_folder_id = match_folder_id
                try:
                    gd.get_file_name(current_folder_id)
                    folders = gd.list_folders(match_folder_id)
                except Exception as e:
                    folders = gd.get_drives()
                    current_folder_id = ''
                    context.bot.send_message(
                        chat_id=update.effective_user.id,
                        text='⁉️ Error:\n<code>{}</code>'.format(
                            html.escape(str(e))),
                        parse_mode=ParseMode.HTML)
                context.user_data[udkey_folders_cache] = copy.deepcopy(folders)
                if not folders:
                    folders = {'#': '(No subfolders)'}
                match_folder_id_replace = match.group('replace')
                if match_folder_id_replace:
                    context.user_data[
                        udkey_fav_folders_replace] = match_folder_id
            if match.group('page'):
                page = int(match.group('page'))
            if not folders and match.group('page'):
                folders = context.user_data.get(udkey_folders_cache, None)
            if not folders:
                folders = gd.get_drives()
                context.user_data[udkey_folders_cache] = copy.deepcopy(folders)
            if not folders:
                folders = {
                    '#':
                    'Ortak sürücünüz yoksa bir tane olmak için @kamileecher1.'
                }
        else:
            alert_users(context, update.effective_user, 'invalid query data',
                        query.data)
            query.answer(text='Yo-he!', show_alert=True)
            return

    if not page:
        page = 1

    folders_len = len(folders)
    page_data = []
    for item in folders:
        page_data.append({'text': folders[item], 'data': item})

    page_data_chosen = list(context.user_data.get(udkey_folders, {}))
    inline_keyboard_drive_ids = get_inline_keyboard_pagination_data(
        callback_query_prefix,
        page_data,
        page_data_chosen=page_data_chosen,
        page=page,
        max_per_page=10,
    )

    if current_folder_id:
        current_path = ''
        current_path_list = gd.get_file_path_from_id(current_folder_id)
        if current_path_list:
            current_folder_name = current_path_list[0]['name']
            for item in current_path_list:
                current_path = '/{}{}'.format(item['name'], current_path)
            if len(current_path_list) > 1:
                inline_keyboard_drive_ids.insert(0, [
                    InlineKeyboardButton(
                        '📁 ' + current_path,
                        callback_data='{},{}'.format(
                            callback_query_prefix,
                            current_path_list[1]['folder_id']))
                ])
            else:
                inline_keyboard_drive_ids.insert(0, [
                    InlineKeyboardButton('📁' + current_path,
                                         callback_data=callback_query_prefix)
                ])
            inline_keyboard_drive_ids.append([
                InlineKeyboardButton(
                    '✔️ Select this folder({})'.format(current_folder_name),
                    callback_data='chosen_folder,{}'.format(current_folder_id))
            ])
    inline_keyboard_drive_ids.append([
        InlineKeyboardButton(
            '🔙 Go back',
            callback_data='choose_folder' if current_folder_id else '#'),
        InlineKeyboardButton('Cancel', callback_data='cancel')
    ])
    context.bot.edit_message_text(
        chat_id=update.effective_chat.id,
        message_id=message_id,
        text=
        '🔶 Select the directory you want to use, there are {} subdirectories.'.
        format(folders_len),
        reply_markup=InlineKeyboardMarkup(inline_keyboard_drive_ids))