From 74dc1bd7eee958fb9bd593ee834f942133d7ad78 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 8 Apr 2026 09:35:28 +0000 Subject: [PATCH] fix: address code review feedback on keyword_search - Change limit validation from 0-100 to 1-100 - Fix negative slice index when search_query exceeds title length - Use hashlib.md5 for deterministic fallback ID generation - Simplify redundant None check for results Agent-Logs-Url: https://github.com/thaitien280401-stack/RedditVideoMakerBot/sessions/c6cae05a-91f1-4ab3-abd3-22dba1f74f6d Co-authored-by: thaitien280401-stack <271128961+thaitien280401-stack@users.noreply.github.com> --- threads/threads_client.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/threads/threads_client.py b/threads/threads_client.py index 0d27f65..1ccf064 100644 --- a/threads/threads_client.py +++ b/threads/threads_client.py @@ -15,6 +15,7 @@ Các API được hỗ trợ: 8. Keyword Search API – Tìm kiếm bài viết theo từ khóa hoặc thẻ chủ đề """ +import hashlib import re import time as _time from typing import Any, Dict, List, Optional @@ -1024,9 +1025,9 @@ class ThreadsClient: params["until"] = until if limit is not None: - if limit < 0 or limit > 100: + if limit < 1 or limit > 100: raise ValueError( - f"limit không hợp lệ: {limit}. Phải trong khoảng 0-100." + f"limit không hợp lệ: {limit}. Phải trong khoảng 1-100." ) params["limit"] = limit @@ -1418,10 +1419,8 @@ def _get_keyword_search_content( break if thread is None: - if results: - thread = results[0] - else: - return None + # results is guaranteed non-empty here (checked earlier) + thread = results[0] thread_id = thread.get("id", "") thread_text = thread.get("text", "") @@ -1433,11 +1432,11 @@ def _get_keyword_search_content( shortcode = thread.get("shortcode", "") # Dùng search_query làm tiêu đề video - display_title = ( - f"{search_query}: {thread_text[:_MAX_TITLE_LENGTH - len(search_query) - 2]}" - if search_query - else thread_text[:_MAX_TITLE_LENGTH] - ) + remaining = _MAX_TITLE_LENGTH - len(search_query) - 2 + if search_query and remaining > 0: + display_title = f"{search_query}: {thread_text[:remaining]}" + else: + display_title = thread_text[:_MAX_TITLE_LENGTH] print_substep( f"Video sẽ được tạo từ Keyword Search: {display_title[:100]}...", @@ -1455,7 +1454,7 @@ def _get_keyword_search_content( "thread_title": display_title, "thread_id": re.sub( r"[^\w\s-]", "", - shortcode or thread_id or f"kwsearch_{hash(thread_text) % 10**8}", + shortcode or thread_id or f"kwsearch_{hashlib.md5(thread_text.encode()).hexdigest()[:8]}", ), "thread_author": f"@{thread_username}", "is_nsfw": False,