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>
pull/2482/head
copilot-swe-agent[bot] 6 months ago committed by GitHub
parent 0d9d5f9f7b
commit 74dc1bd7ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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ủ đề 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 re
import time as _time import time as _time
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
@ -1024,9 +1025,9 @@ class ThreadsClient:
params["until"] = until params["until"] = until
if limit is not None: if limit is not None:
if limit < 0 or limit > 100: if limit < 1 or limit > 100:
raise ValueError( 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 params["limit"] = limit
@ -1418,10 +1419,8 @@ def _get_keyword_search_content(
break break
if thread is None: if thread is None:
if results: # results is guaranteed non-empty here (checked earlier)
thread = results[0] thread = results[0]
else:
return None
thread_id = thread.get("id", "") thread_id = thread.get("id", "")
thread_text = thread.get("text", "") thread_text = thread.get("text", "")
@ -1433,11 +1432,11 @@ def _get_keyword_search_content(
shortcode = thread.get("shortcode", "") shortcode = thread.get("shortcode", "")
# Dùng search_query làm tiêu đề video # Dùng search_query làm tiêu đề video
display_title = ( remaining = _MAX_TITLE_LENGTH - len(search_query) - 2
f"{search_query}: {thread_text[:_MAX_TITLE_LENGTH - len(search_query) - 2]}" if search_query and remaining > 0:
if search_query display_title = f"{search_query}: {thread_text[:remaining]}"
else thread_text[:_MAX_TITLE_LENGTH] else:
) display_title = thread_text[:_MAX_TITLE_LENGTH]
print_substep( print_substep(
f"Video sẽ được tạo từ Keyword Search: {display_title[:100]}...", 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_title": display_title,
"thread_id": re.sub( "thread_id": re.sub(
r"[^\w\s-]", "", 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}", "thread_author": f"@{thread_username}",
"is_nsfw": False, "is_nsfw": False,

Loading…
Cancel
Save