From 111a81ae264ef4c809eb0e5be06ee7bebe73dcf0 Mon Sep 17 00:00:00 2001 From: Anatoly Bogomolov Date: Fri, 12 Apr 2024 21:41:37 +1000 Subject: [PATCH] Fixes --- app/blueprints/api/tasks/routes.py | 10 +++++----- app/blueprints/api/tasks/tasks.py | 19 +++++++++++++++---- app/blueprints/frontend/templates/parse.j2 | 8 +++++++- app/models/user.py | 2 +- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app/blueprints/api/tasks/routes.py b/app/blueprints/api/tasks/routes.py index df275fe..4f18d63 100644 --- a/app/blueprints/api/tasks/routes.py +++ b/app/blueprints/api/tasks/routes.py @@ -22,6 +22,9 @@ def get_tasks(): def create_task(session_id: int): info = request.form collection_id = int(info['collection']) + + #! FIXME: При первой же возможности добавить метаданные в задачи + parse_bio = True if info.get('parse-bio-check') == "on" else False collection = Collection.query.get_or_404(collection_id) session = Session.query.get_or_404(session_id) @@ -42,7 +45,7 @@ def create_task(session_id: int): type = type, url = info.get('parse') or info.get('add'), message = info.get('message'), - file = None + file = None or str(parse_bio) ) db.session.add(task) @@ -97,10 +100,7 @@ def start_task(task_id: int): add_to_group_task.delay(task_id=task_id) case 'parse': - try: - parse_users_task.run(task_id=task_id) - finally: - delete_task(task_id) + parse_users_task.delay(task_id=task_id) return '', 204 diff --git a/app/blueprints/api/tasks/tasks.py b/app/blueprints/api/tasks/tasks.py index 0c74350..d20a7dc 100644 --- a/app/blueprints/api/tasks/tasks.py +++ b/app/blueprints/api/tasks/tasks.py @@ -20,16 +20,25 @@ async def sending_message(session, task, task_self): async with PaperParser(session.name) as parser: await parser.send_messages(task.collection.users, task.message, task.file, task_self) -async def parse_users(session, task, db, collection): +async def parse_users(session, task, db, collection, task_self): + parse_bio = True if task.file else False + async with PaperParser(session.name) as parser: users = await parser.get_participants(task.url) filtered_users = filter(filter_users, users) for user in filtered_users: + if task_self.is_aborted(): + return + if not User.exist(user.username, collection): - full = await parser.get_full_info() - bio = full.full_user.about if full.full_user else None + bio = None + + if parse_bio: + full = await parser.get_full_info(user) + bio = full.full_user.about if full.full_user else None + logger.info(f"Got bio for {user.username}. Bio: { bio }") db.session.add( User( @@ -44,6 +53,8 @@ async def parse_users(session, task, db, collection): db.session.commit() + await asyncio.sleep(5) + def filter_users(user): if not user.username or user.bot: return False @@ -89,7 +100,7 @@ def parse_users_task(self, task_id: int): run_state(self.request.id, task_id) asyncio.run( - parse_users(session, task, db, collection) + parse_users(session, task, db, collection, self) ) except Exception as e: diff --git a/app/blueprints/frontend/templates/parse.j2 b/app/blueprints/frontend/templates/parse.j2 index a2df21f..391ee14 100644 --- a/app/blueprints/frontend/templates/parse.j2 +++ b/app/blueprints/frontend/templates/parse.j2 @@ -30,10 +30,16 @@