示例#1
0
async def run_user_tasks(
        user: dict,  #用户配置
        default: dict  #默认配置
) -> None:
    async with asyncbili() as biliapi:
        try:
            if not await biliapi.login_by_cookie(user["cookieDatas"]):
                logging.warning(
                    f'id为{user["cookieDatas"]["DedeUserID"]}的账户cookie失效,跳过此账户后续操作'
                )
                tasks.webhook.addMsg(
                    'msg_simple',
                    f'id为{user["cookieDatas"]["DedeUserID"]}的账户cookie失效\n')
                return
        except Exception as e:
            logging.warning(
                f'登录验证id为{user["cookieDatas"]["DedeUserID"]}的账户失败,原因为{str(e)},跳过此账户后续操作'
            )
            return

        show_name = user.get("show_name", "")
        if show_name:
            biliapi.name = show_name

        logging.info(
            f'{biliapi.name}: 等级{biliapi.level},经验{biliapi.myexp},剩余硬币{biliapi.mycoin}'
        )
        tasks.webhook.addMsg(
            'msg_simple',
            f'{biliapi.name}: 等级{biliapi.level},经验{biliapi.myexp},剩余硬币{biliapi.mycoin}\n'
        )

        task_array = []  #存放本账户所有任务

        for task in default:  #遍历任务列表,把需要运行的任务添加到task_array
            task_function = getattr(tasks, task, None)
            if task_function:
                if task in user["tasks"]:
                    if isinstance(user["tasks"][task], bool):
                        if user["tasks"][task]:
                            task_array.append(task_function(biliapi))
                    elif isinstance(user["tasks"][task], dict):
                        if 'enable' in user["tasks"][task] and user["tasks"][
                                task]["enable"]:
                            task_array.append(
                                task_function(biliapi, user["tasks"][task]))
                else:
                    if isinstance(default[task], bool):
                        if default[task]:
                            task_array.append(task_function(biliapi))
                    elif isinstance(default[task], dict):
                        if 'enable' in default[task] and default[task][
                                "enable"]:
                            task_array.append(
                                task_function(biliapi, default[task]))

        if task_array:
            await asyncio.wait(task_array)  #异步等待所有任务完成
示例#2
0
async def run_user_tasks(
        user: dict,  # 用户配置
        default: dict,  # 默认配置
        header: dict = None,
        debug_tasks: list = None) -> None:
    async with asyncbili(header) as biliapi:
        try:
            if not await biliapi.login_by_cookie(user["cookieDatas"]):
                logging.warning(
                    f'id为{user["cookieDatas"]["DedeUserID"]}的账户cookie失效,跳过此账户后续操作'
                )
                tasks.webhook.addMsg(
                    'msg_simple',
                    f'id为{user["cookieDatas"]["DedeUserID"]}的账户cookie失效\n')
                return
        except Exception as e:
            logging.warning(
                f'登录验证id为{user["cookieDatas"]["DedeUserID"]}的账户失败,原因为{str(e)},跳过此账户后续操作'
            )
            return

        show_name = user.get("show_name", "")
        if show_name:
            biliapi.name = show_name

        logging.info(
            f'{biliapi.name}: 等级{biliapi.level},经验{biliapi.myexp},剩余硬币{biliapi.mycoin}'
        )
        tasks.webhook.addMsg(
            'msg_simple',
            f'{biliapi.name}: 等级{biliapi.level},经验{biliapi.myexp},剩余硬币{biliapi.mycoin}\n'
        )

        task_array = []  # 存放本账户所有任务

        for task in default:  # 遍历任务列表,把需要运行的任务添加到task_array
            if debug_tasks and task not in debug_tasks:
                continue

            try:
                task_module = import_module(f'tasks.{task}')  # 加载任务模块
            except ModuleNotFoundError:
                logging.error(f'{biliapi.name}: 未找到任务模块{task}')
                continue

            task_function = getattr(task_module, task, None)
            if not task_function:
                logging.error(f'{biliapi.name}: 未找到任务{task}的入口函数')
                continue

            if task in user["tasks"]:
                task_config = user['tasks'][task]
            else:
                task_config = default[task]
            if isinstance(task_config, bool):
                if task_config:
                    task_array.append(
                        asyncio.ensure_future(task_function(biliapi)))
            elif isinstance(task_config, dict):
                if 'enable' in task_config and task_config["enable"]:
                    task_array.append(
                        asyncio.ensure_future(
                            task_function(biliapi, default[task])))

        if task_array:
            await asyncio.wait(task_array)  # 异步等待所有任务完成