Compare commits
No commits in common. "2b7ad0c72438f3421d1fc4ec5b8c583ecfe56969" and "dd1518b69cd2d8ef5abb6a242da617dd756fd6ef" have entirely different histories.
2b7ad0c724
...
dd1518b69c
|
@ -5,26 +5,6 @@ from app.extensions import db
|
|||
|
||||
collections = Blueprint("collections", __name__, url_prefix="/collections", template_folder="templates")
|
||||
|
||||
@collections.route('/', methods=["POST"])
|
||||
def create_collection():
|
||||
data = request.form
|
||||
name = data.get('collection_name')
|
||||
|
||||
if not name:
|
||||
return render_template('collections/create_form.j2', collections_error='Введите название базы!')
|
||||
|
||||
if Collection.exists(name):
|
||||
return render_template('collections/create_form.j2', collections_error='База уже существует!')
|
||||
|
||||
swap = 'afterbegin' if len(Collection.query.all()) > 0 else 'innerHTML'
|
||||
|
||||
collection = Collection(name=name)
|
||||
|
||||
db.session.add(collection)
|
||||
db.session.commit()
|
||||
|
||||
return f"<option value=\"{collection.id}\" selected>{ collection.name }</option>", 200, {'Hx-Reswap': swap, 'HX-Retarget': '#collection-select'}
|
||||
|
||||
@collections.route("/<int:id>", methods=["DELETE"])
|
||||
def delete_collection(id: int):
|
||||
|
||||
|
@ -38,4 +18,4 @@ def delete_collection(id: int):
|
|||
if length <= 0:
|
||||
return "<small class=\"form-text\">Здесь ничего нет</small>", 200
|
||||
|
||||
return "", 200
|
||||
return "", 204
|
|
@ -1,5 +1,4 @@
|
|||
<div data-collection-id="{{ collection.id }}" class="col mb-3">
|
||||
<div class="card">
|
||||
<div data-collection-id="{{ collection.id }}" class="card">
|
||||
<div class="card-header"><h5>{{ collection.name }}</h5></div>
|
||||
<ul class="list-group list-group-flush">
|
||||
<li class="list-group-item">Кол-во пользрвателей: {{ collection.users|length }}</li>
|
||||
|
@ -17,4 +16,3 @@
|
|||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
|
@ -1,2 +0,0 @@
|
|||
{% include "collections/create_form.j2" %}
|
||||
{% include "collections/select.j2" %}
|
|
@ -1,33 +0,0 @@
|
|||
<div id="collection-create-form" class="input-group has-validation">
|
||||
<input
|
||||
type="text"
|
||||
|
||||
{% if collections_error is defined %}
|
||||
class="form-control is-invalid"
|
||||
{% else %}
|
||||
class="form-control"
|
||||
{% endif %}
|
||||
|
||||
name="collection_name"
|
||||
id="collection-name-input"
|
||||
placeholder="Название базы"
|
||||
aria-describedby="collection-feedback"
|
||||
required
|
||||
>
|
||||
<button
|
||||
type="submit"
|
||||
class="btn btn-outline-primary"
|
||||
hx-post="/api/collections"
|
||||
hx-swap="outerHTML"
|
||||
hx-target="#collection-create-form"
|
||||
hx-indicator="#indicator"
|
||||
>
|
||||
Добавить
|
||||
</button>
|
||||
|
||||
{% if collections_error is defined %}
|
||||
<div id="collection-feedback" class="invalid-feedback">
|
||||
{{ collections_error }}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
|
@ -1,6 +1,8 @@
|
|||
<div id="cards-grid" class="row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4">
|
||||
{% for collection in collections %}
|
||||
<div class="col mb-3">
|
||||
{% include "collections/card.j2" %}
|
||||
</div>
|
||||
{% else %}
|
||||
<small class="form-text">Здесь ничего нет.</small>
|
||||
{% endfor %}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
<select name="collection" id="collection-select" class="form-select flex-fill" size="3">
|
||||
{% for collection in collections %}
|
||||
<option value="{{collection.id}}">{{ collection.name }}</option>
|
||||
{% else %}
|
||||
<option disabled>Добавьте новую базу пользователей</option>
|
||||
{% endfor %}
|
||||
</select>
|
|
@ -1,13 +1,11 @@
|
|||
import uuid
|
||||
from flask import Blueprint, render_template, request
|
||||
from sqlalchemy import delete
|
||||
|
||||
from app.extensions import db
|
||||
from app.models.collection import Collection
|
||||
from app.models.task import Task
|
||||
from app.models.session import Session
|
||||
|
||||
from .tasks import parse_users_task, send_messages_task, add_to_group_task
|
||||
from .tasks import send_messages_task, add_to_group_task
|
||||
|
||||
tasks = Blueprint("tasks", __name__, url_prefix="/tasks", template_folder="templates")
|
||||
|
||||
|
@ -18,27 +16,21 @@ def get_tasks():
|
|||
|
||||
return render_template("tasks_cards.j2", tasks=tasks, sessions=sessions)
|
||||
|
||||
@tasks.route("/<int:task_id>", methods=["GET"])
|
||||
def get_task(task_id: int):
|
||||
...
|
||||
|
||||
@tasks.route("/session/<int:session_id>", methods=["POST"])
|
||||
def create_task(session_id: int):
|
||||
info = request.form
|
||||
collection_id = int(info['collection'])
|
||||
|
||||
collection = Collection.query.get_or_404(collection_id)
|
||||
session = Session.query.get_or_404(session_id)
|
||||
|
||||
types = list(filter(lambda item: item in ('parse', 'add', 'message'), info.keys()))
|
||||
|
||||
if len(types) > 0:
|
||||
type = types[0]
|
||||
|
||||
task = Task(
|
||||
name = info.get('task_name') or str(uuid.uuid4()),
|
||||
name = str(uuid.uuid4()),
|
||||
session = session,
|
||||
collection = collection,
|
||||
status = "CREATED",
|
||||
status_message = "Задача создана",
|
||||
type = type,
|
||||
url = info.get('parse') or info.get('add'),
|
||||
type = info.get('task'),
|
||||
url = info.get('url'),
|
||||
message = info.get('message'),
|
||||
file = None
|
||||
)
|
||||
|
@ -48,7 +40,7 @@ def create_task(session_id: int):
|
|||
|
||||
start_task(task.id)
|
||||
|
||||
return '', 204
|
||||
return 'Created', 200
|
||||
|
||||
@tasks.route("/<int:task_id>", methods=["DELETE"])
|
||||
def delete_task(task_id: int):
|
||||
|
@ -57,7 +49,7 @@ def delete_task(task_id: int):
|
|||
db.session.delete(task)
|
||||
db.session.commit()
|
||||
|
||||
return '', 204
|
||||
return 'Deleted', 200
|
||||
|
||||
@tasks.route("/<int:task_id>/stop", methods=["PUT"])
|
||||
def stop_task(task_id: int):
|
||||
|
@ -80,7 +72,7 @@ def stop_task(task_id: int):
|
|||
|
||||
db.session.commit()
|
||||
|
||||
return '', 204
|
||||
return 'Stoping', 200
|
||||
|
||||
|
||||
@tasks.route("/<int:task_id>/start", methods=["PUT"])
|
||||
|
@ -94,11 +86,7 @@ def start_task(task_id: int):
|
|||
case 'add':
|
||||
add_to_group_task.delay(task_id=task_id)
|
||||
|
||||
case 'parse':
|
||||
parse_users_task.apply(task_id=task_id)
|
||||
delete_task(task_id)
|
||||
|
||||
return '', 204
|
||||
return 'Started', 200
|
||||
|
||||
@tasks.route("/<int:task_id>/change", methods=["PUT"])
|
||||
def change_settings(task_id: int):
|
||||
|
@ -113,4 +101,4 @@ def change_settings(task_id: int):
|
|||
|
||||
db.session.commit()
|
||||
|
||||
return '', 204
|
||||
return 'Changed', 200
|
|
@ -2,8 +2,6 @@ import time
|
|||
|
||||
from celery import shared_task
|
||||
import asyncio
|
||||
from app.models.session import Session
|
||||
from app.models.user import User
|
||||
|
||||
from paper.parser import PaperParser
|
||||
from app.models.task import Task
|
||||
|
@ -12,18 +10,14 @@ 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:
|
||||
await parser.invite_users(task.collection.users, task.url, task_self)
|
||||
await parser.invite_users(session.users, task.url, task_self)
|
||||
|
||||
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):
|
||||
async with PaperParser(session.name) as parser:
|
||||
return await parser.get_participants(task.url)
|
||||
await parser.send_messages(session.users, task.message, task.file, task_self)
|
||||
|
||||
@shared_task(bind=True)
|
||||
def add_to_group_task(self, task_id: int):
|
||||
def add_to_group_task(self, task_id):
|
||||
try:
|
||||
task: Task = Task.query.get(task_id)
|
||||
run_state(self.request.id, task_id)
|
||||
|
@ -43,35 +37,10 @@ def add_to_group_task(self, task_id: int):
|
|||
else:
|
||||
success_state(task_id)
|
||||
|
||||
@shared_task(bind=True)
|
||||
def parse_users_task(self, task_id: int):
|
||||
task: Task = Task.query.get_or_404(task_id)
|
||||
session = task.session
|
||||
collection = task.collection
|
||||
|
||||
users = asyncio.run(
|
||||
parse_users(session, task)
|
||||
)
|
||||
|
||||
for user in users:
|
||||
if not user.username:
|
||||
continue
|
||||
|
||||
if not User.exist(user.username, collection):
|
||||
db.session.add(
|
||||
User(
|
||||
first_name=user.first_name,
|
||||
last_name=user.last_name,
|
||||
username=user.username,
|
||||
phone=user.phone,
|
||||
collection=collection,
|
||||
)
|
||||
)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
@shared_task(bind=True)
|
||||
def send_messages_task(self, task_id: int):
|
||||
def send_messages_task(self, task_id):
|
||||
task: Task = Task.query.get(task_id)
|
||||
|
||||
session = task.session
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from flask import Blueprint, render_template
|
||||
from app.blueprints.api.sessions.routes import get_sessions
|
||||
from app.blueprints.api.users.routes import get_users
|
||||
from app.blueprints.api.tasks.routes import get_tasks
|
||||
from app.models.collection import Collection
|
||||
|
||||
|
@ -20,8 +21,7 @@ def tasks():
|
|||
|
||||
@frontend.route("/parse/<int:id>")
|
||||
def parse(id: int):
|
||||
collections = Collection.query.all()
|
||||
return render_template("parse.j2", session_id=id, collections=collections)
|
||||
return render_template("parse.j2", session_id=id, users_template=get_users(id))
|
||||
|
||||
@frontend.route("/collections")
|
||||
def collections():
|
||||
|
|
|
@ -5,19 +5,12 @@
|
|||
{% endblock title %}
|
||||
|
||||
{% block main %}
|
||||
<form method="post" action="/api/tasks/session/{{ session_id }}" class="container">
|
||||
<form method="post" action="/test" class="container">
|
||||
<div class="row">
|
||||
<!-- Actions -->
|
||||
<section class="col-lg mb-3">
|
||||
<div class="separator">
|
||||
<div class="d-flex gap-2">
|
||||
<h2>Действия</h2>
|
||||
<div id="indicator" class="htmx-indicator">
|
||||
<div class="spinner-border" role="status">
|
||||
<span class="visually-hidden">Loading...</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<hr class="divider">
|
||||
</div>
|
||||
<nav class="mb-3">
|
||||
|
@ -31,14 +24,14 @@
|
|||
|
||||
<section class="tab-pane fade show active" id="actions-parse-section">
|
||||
<div class="form-floating">
|
||||
<input type="text" class="form-control" name="parse" id="group-from-input" placeholder="Группа для парсинга">
|
||||
<input type="text" class="form-control" name="group" id="group-from-input" placeholder="Группа для парсинга">
|
||||
<label for="group-from-input">Группа для парсинга</label>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="tab-pane fade" id="actions-add-section">
|
||||
<div class="form-floating">
|
||||
<input type="text" name="url" name="add" class="form-control" id="group-to-input"
|
||||
<input type="text" name="url" class="form-control" id="group-to-input"
|
||||
placeholder="Группа для добавлнеия">
|
||||
<label for="group-to-input">Группа для добавления</label>
|
||||
</div>
|
||||
|
@ -70,7 +63,7 @@
|
|||
<input
|
||||
type="text"
|
||||
class="form-control"
|
||||
name="task_name"
|
||||
name="collection"
|
||||
id="task-name-input"
|
||||
placeholder="Название задачи"
|
||||
>
|
||||
|
@ -78,9 +71,29 @@
|
|||
</div>
|
||||
|
||||
<section class="d-flex flex-column gap-3 mb-3">
|
||||
{% include "collections/collections_section.j2" %}
|
||||
<div class="input-group">
|
||||
<div class="form-floating">
|
||||
<input
|
||||
type="text"
|
||||
class="form-control"
|
||||
name="collection"
|
||||
id="collection-name-input"
|
||||
placeholder="Группа для парсинга"
|
||||
>
|
||||
<label for="collection-name-input">Название базы пользователей</label>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-outline-primary">Добавить</button>
|
||||
</div>
|
||||
|
||||
<select class="form-select flex-fill" size="3">
|
||||
{% for collection in collections %}
|
||||
<option value="{{collection.id}}">{{ collection.name }}</option>
|
||||
{% else %}
|
||||
<option disabled>Добавьте новую базу пользователей</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</section>
|
||||
<button type="submit" hx-post="/api/tasks/session/{{ session_id }}" hx-indicator="#indicator" class="w-100 btn btn-primary">Создать задачу</button>
|
||||
<button type="submit" class="w-100 btn btn-primary">Создать задачу</button>
|
||||
</section>
|
||||
</div>
|
||||
</form>
|
||||
|
|
|
@ -10,8 +10,3 @@ class Collection(db.Model):
|
|||
|
||||
tasks: Mapped[List['Task']] = relationship("Task", back_populates="collection")
|
||||
users: Mapped[List['User']] = relationship("User", cascade="all, delete-orphan", back_populates="collection")
|
||||
|
||||
def exists(name: str):
|
||||
collections = Collection.query.filter_by(name=name).all()
|
||||
|
||||
return len(collections) > 0
|
|
@ -17,6 +17,3 @@ class User(db.Model):
|
|||
|
||||
collection_id = Column(Integer, ForeignKey("collection.id"))
|
||||
collection: Mapped["Collection"] = relationship("Collection", back_populates="users")
|
||||
|
||||
def exist(username: str, collection):
|
||||
return not User.query.filter_by(username=username, collection=collection).first() is None
|
|
@ -1,9 +1,11 @@
|
|||
import asyncio
|
||||
import os
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from paper.client import PaperClient
|
||||
from paper.errors import IgnoreException, NeedPasswordException, UserPrivacyException
|
||||
from paper.models import Message
|
||||
|
||||
|
||||
class PaperParser:
|
||||
|
@ -15,7 +17,7 @@ class PaperParser:
|
|||
|
||||
self.users_to_delete = []
|
||||
|
||||
async def invite_users(self, users, group, task):
|
||||
async def invite_users(self, users, group, task = None):
|
||||
await self.client.invite_self(group)
|
||||
|
||||
group_participants = await self.client.get_participants(group)
|
||||
|
@ -31,7 +33,7 @@ class PaperParser:
|
|||
|
||||
await self.client.invite_user(user, group)
|
||||
|
||||
except (UserPrivacyException, IgnoreException):
|
||||
except (UserPrivacyException, IgnoreException) as e:
|
||||
self.users_to_delete.append(user)
|
||||
logger.warning("Exception occurred. Skipping user...")
|
||||
|
||||
|
@ -45,8 +47,6 @@ class PaperParser:
|
|||
# if dialog.is_user:
|
||||
# messages = tuple(filter(lambda chat_message: message.text == chat_message.text, await self.client.get_messages(dialog)))
|
||||
# print(messages)
|
||||
if not task:
|
||||
return
|
||||
|
||||
for user in users:
|
||||
try:
|
||||
|
@ -55,7 +55,7 @@ class PaperParser:
|
|||
|
||||
await self.client.send_message(user, message, file)
|
||||
|
||||
except (UserPrivacyException, IgnoreException):
|
||||
except (UserPrivacyException, IgnoreException) as e:
|
||||
self.users_to_delete.append(user)
|
||||
logger.warning("Exception occurred. Skipping user...")
|
||||
|
||||
|
|
Loading…
Reference in New Issue