generate seo text when draft created

This commit is contained in:
Untone 2025-04-15 20:09:22 +03:00
parent 710f522c8f
commit b9f6033e66
5 changed files with 18 additions and 3 deletions

View File

@ -14,4 +14,5 @@ gql
ariadne
granian
orjson
pydantic
pydantic
trafilatura

View File

@ -114,6 +114,10 @@ async def create_draft(_, info, draft_input):
# Remove id from input if present since it's auto-generated
if "id" in draft_input:
del draft_input["id"]
if "seo" not in draft_input and not draft_input["seo"]:
body_teaser = draft_input.get("body", "")[:300].split('\n')[:-1].join("\n")
draft_input["seo"] = draft_input.get("lead", body_teaser)
# Добавляем текущее время создания
draft_input["created_at"] = int(time.time())

View File

@ -22,6 +22,7 @@ from services.notify import notify_shout
from services.schema import query
from services.search import search_service
from utils.logger import root_logger as logger
import trafilatura
async def cache_by_id(entity, entity_id: int, cache_method):
@ -176,9 +177,16 @@ async def create_shout(_, info, inp):
logger.info(f"Creating shout with input: {inp}")
# Создаем публикацию без topics
body = inp.get("body", "")
lead = inp.get("lead", "")
body_text = trafilatura.extract(body)
lead_text = trafilatura.extract(lead)
seo = inp.get("seo", lead_text or body_text[:300].split('. ')[:-1].join(". "))
new_shout = Shout(
slug=slug,
body=inp.get("body", ""),
body=body,
seo=seo,
lead=lead,
layout=inp.get("layout", "article"),
title=inp.get("title", ""),
created_by=author_id,
@ -380,7 +388,7 @@ def patch_topics(session, shout, topics_input):
# @login_required
async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
logger.info(f"Starting update_shout with id={shout_id}, publish={publish}")
logger.debug(f"Full shout_input: {shout_input}")
logger.debug(f"Full shout_input: {shout_input}") # DraftInput
user_id = info.context.get("user_id")
roles = info.context.get("roles", [])

View File

@ -41,6 +41,7 @@ input DraftInput {
slug: String
cover: String
cover_caption: String
seo: String
}
input ProfileInput {

View File

@ -99,6 +99,7 @@ type Shout {
featured_at: Int
deleted_at: Int
seo: String # generated if not set
version_of: Shout # TODO: use version_of somewhere
draft: Draft
media: [MediaItem]