PaperParser/app/blueprints/api/tasks/tasks.py

119 lines
3.5 KiB
Python
Raw Normal View History

2024-03-09 23:12:27 +10:00
import datetime
2024-01-31 19:37:01 +10:00
import time
from celery import shared_task
import asyncio
2024-03-13 16:49:37 +10:00
from loguru import logger
2024-03-10 00:29:25 +10:00
from telethon.tl.types import UserStatusOnline, UserStatusOffline, UserStatusRecently
2024-01-31 19:37:01 +10:00
2024-03-09 23:12:27 +10:00
from app.models.user import User
2024-01-31 19:37:01 +10:00
from paper.parser import PaperParser
from app.models.task import Task
from app.extensions import db
from .handlers import run_state, failure_state, success_state
async def add_to_group(session, task, task_self):
async with PaperParser(session.name) as parser:
2024-03-09 01:51:36 +10:00
await parser.invite_users(task.collection.users, task.url, task_self)
2024-01-31 19:37:01 +10:00
async def sending_message(session, task, task_self):
async with PaperParser(session.name) as parser:
2024-03-09 01:51:36 +10:00
await parser.send_messages(task.collection.users, task.message, task.file, task_self)
2024-04-12 21:41:37 +10:00
async def parse_users(session, task, db, collection, task_self):
parse_bio = True if task.file else False
2024-03-09 01:51:36 +10:00
async with PaperParser(session.name) as parser:
2024-04-12 16:55:27 +10:00
users = await parser.get_participants(task.url)
filtered_users = filter(filter_users, users)
for user in filtered_users:
2024-04-12 21:41:37 +10:00
if task_self.is_aborted():
return
2024-04-12 16:55:27 +10:00
if not User.exist(user.username, collection):
2024-04-12 21:41:37 +10:00
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 }")
2024-04-12 16:55:27 +10:00
db.session.add(
User(
first_name=user.first_name,
last_name=user.last_name,
username=user.username,
phone=user.phone,
description=bio,
collection=collection,
)
)
db.session.commit()
2024-04-12 21:41:37 +10:00
await asyncio.sleep(5)
2024-04-12 16:55:27 +10:00
def filter_users(user):
if not user.username or user.bot:
return False
is_active = False
if isinstance(user.status, UserStatusOffline):
was_online = user.status.was_online.replace(tzinfo=datetime.timezone.utc)
today = datetime.datetime.today().replace(tzinfo=datetime.timezone.utc)
is_active = was_online + datetime.timedelta(days=5) >= today
if isinstance(user.status, UserStatusOnline) or isinstance(user.status, UserStatusRecently):
is_active = True
return is_active
2024-01-31 19:37:01 +10:00
@shared_task(bind=True)
2024-03-09 01:51:36 +10:00
def add_to_group_task(self, task_id: int):
2024-01-31 19:37:01 +10:00
try:
task: Task = Task.query.get(task_id)
run_state(self.request.id, task_id)
session = task.session
asyncio.run(
add_to_group(session, task, self)
)
except Exception as e:
2024-03-13 16:49:37 +10:00
logger.exception(e)
2024-02-08 18:00:37 +10:00
failure_state(task_id, e)
2024-01-31 19:37:01 +10:00
else:
2024-02-08 18:00:37 +10:00
success_state(task_id)
2024-01-31 19:37:01 +10:00
2024-04-12 16:55:27 +10:00
2024-03-09 01:51:36 +10:00
@shared_task(bind=True)
def parse_users_task(self, task_id: int):
2024-04-12 16:55:27 +10:00
try:
task: Task = Task.query.get_or_404(task_id)
session = task.session
collection = task.collection
2024-01-31 19:37:01 +10:00
2024-04-12 16:55:27 +10:00
run_state(self.request.id, task_id)
asyncio.run(
2024-04-12 21:41:37 +10:00
parse_users(session, task, db, collection, self)
2024-04-12 16:55:27 +10:00
)
2024-03-09 01:51:36 +10:00
2024-04-12 16:55:27 +10:00
except Exception as e:
logger.exception(e)
failure_state(task_id, e)
2024-03-09 01:51:36 +10:00
2024-04-12 16:55:27 +10:00
else:
success_state(task_id)
2024-01-31 19:37:01 +10:00
@shared_task(bind=True)
2024-03-09 01:51:36 +10:00
def send_messages_task(self, task_id: int):
2024-01-31 19:37:01 +10:00
task: Task = Task.query.get(task_id)
session = task.session
users = session.users