diff --git a/scripts/last30days.py b/scripts/last30days.py index a979e05..8c98744 100644 --- a/scripts/last30days.py +++ b/scripts/last30days.py @@ -120,6 +120,27 @@ def run_research( # Parse response reddit_items = openai_reddit.parse_reddit_response(raw_openai) + # Quick retry with simpler query if few results + if len(reddit_items) < 5 and not mock: + core = openai_reddit._extract_core_subject(topic) + if core.lower() != topic.lower(): + try: + retry_raw = openai_reddit.search_reddit( + config["OPENAI_API_KEY"], + selected_models["openai"], + core, + from_date, to_date, + depth=depth, + ) + retry_items = openai_reddit.parse_reddit_response(retry_raw) + # Add items not already found (by URL) + existing_urls = {item.get("url") for item in reddit_items} + for item in retry_items: + if item.get("url") not in existing_urls: + reddit_items.append(item) + except Exception: + pass + if progress: progress.end_reddit(len(reddit_items)) diff --git a/scripts/lib/openai_reddit.py b/scripts/lib/openai_reddit.py index 4faac92..c7988fa 100644 --- a/scripts/lib/openai_reddit.py +++ b/scripts/lib/openai_reddit.py @@ -66,6 +66,16 @@ Return JSON: }}""" +def _extract_core_subject(topic: str) -> str: + """Extract core subject from verbose query for retry.""" + noise = ['best', 'top', 'how to', 'tips for', 'practices', 'features', + 'killer', 'guide', 'tutorial', 'recommendations', 'advice', + 'prompting', 'using', 'for', 'with', 'the', 'of', 'in', 'on'] + words = topic.lower().split() + result = [w for w in words if w not in noise] + return ' '.join(result[:3]) or topic # Keep max 3 words + + def search_reddit( api_key: str, model: str, @@ -74,6 +84,7 @@ def search_reddit( to_date: str, depth: str = "default", mock_response: Optional[Dict] = None, + _retry: bool = False, ) -> Dict[str, Any]: """Search Reddit for relevant threads using OpenAI Responses API.