diff --git a/requirements.txt b/requirements.txt index d6ce74b9..ad4acff6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,4 +14,5 @@ gql ariadne granian orjson -pydantic \ No newline at end of file +pydantic +trafilatura \ No newline at end of file diff --git a/resolvers/draft.py b/resolvers/draft.py index b08a8d0b..168ea904 100644 --- a/resolvers/draft.py +++ b/resolvers/draft.py @@ -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()) diff --git a/resolvers/editor.py b/resolvers/editor.py index 1efc40cc..4f2d8beb 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -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", []) diff --git a/schema/input.graphql b/schema/input.graphql index c1637723..f7e4a7b2 100644 --- a/schema/input.graphql +++ b/schema/input.graphql @@ -41,6 +41,7 @@ input DraftInput { slug: String cover: String cover_caption: String + seo: String } input ProfileInput { diff --git a/schema/type.graphql b/schema/type.graphql index d0a5350f..d902d143 100644 --- a/schema/type.graphql +++ b/schema/type.graphql @@ -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]