5ca4829be4
Research topics across Reddit + X from the last 30 days using OpenAI and xAI APIs. Features: - Auto model selection (GPT-5.x, Grok-3) - Popularity-aware scoring (relevance + recency + engagement) - Reddit thread enrichment with real metrics - Near-duplicate detection - Multiple emit modes (compact, json, context, path) - 24h caching with --refresh bypass - NUX for API key setup - 87 passing unit tests Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
159 lines
4.5 KiB
Python
159 lines
4.5 KiB
Python
"""OpenAI Responses API client for Reddit discovery."""
|
|
|
|
import json
|
|
import re
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from . import http
|
|
|
|
OPENAI_RESPONSES_URL = "https://api.openai.com/v1/responses"
|
|
|
|
REDDIT_SEARCH_PROMPT = """Search Reddit for discussions about: {topic}
|
|
|
|
Focus on threads from the last 30 days. Find 15-30 high-quality, relevant threads.
|
|
|
|
IMPORTANT: Return ONLY valid JSON in this exact format, no other text:
|
|
{{
|
|
"items": [
|
|
{{
|
|
"title": "Thread title",
|
|
"url": "https://reddit.com/r/...",
|
|
"subreddit": "subreddit_name",
|
|
"date": "YYYY-MM-DD or null if unknown",
|
|
"why_relevant": "Brief explanation of relevance",
|
|
"relevance": 0.85
|
|
}}
|
|
]
|
|
}}
|
|
|
|
Rules:
|
|
- relevance is 0.0 to 1.0 (1.0 = highly relevant)
|
|
- date must be YYYY-MM-DD format or null
|
|
- Include diverse subreddits if applicable
|
|
- Prefer threads with substantive discussions
|
|
- Do NOT include engagement metrics (upvotes, comments) - those will be fetched separately"""
|
|
|
|
|
|
def search_reddit(
|
|
api_key: str,
|
|
model: str,
|
|
topic: str,
|
|
mock_response: Optional[Dict] = None,
|
|
) -> Dict[str, Any]:
|
|
"""Search Reddit for relevant threads using OpenAI Responses API.
|
|
|
|
Args:
|
|
api_key: OpenAI API key
|
|
model: Model to use
|
|
topic: Search topic
|
|
mock_response: Mock response for testing
|
|
|
|
Returns:
|
|
Raw API response
|
|
"""
|
|
if mock_response is not None:
|
|
return mock_response
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {api_key}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
|
|
payload = {
|
|
"model": model,
|
|
"tools": [
|
|
{
|
|
"type": "web_search",
|
|
"filters": {
|
|
"allowed_domains": ["reddit.com"]
|
|
}
|
|
}
|
|
],
|
|
"include": ["web_search_call.action.sources"],
|
|
"input": REDDIT_SEARCH_PROMPT.format(topic=topic),
|
|
}
|
|
|
|
return http.post(OPENAI_RESPONSES_URL, payload, headers=headers, timeout=60)
|
|
|
|
|
|
def parse_reddit_response(response: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|
"""Parse OpenAI response to extract Reddit items.
|
|
|
|
Args:
|
|
response: Raw API response
|
|
|
|
Returns:
|
|
List of item dicts
|
|
"""
|
|
items = []
|
|
|
|
# Try to find the output text
|
|
output_text = ""
|
|
if "output" in response:
|
|
output = response["output"]
|
|
if isinstance(output, str):
|
|
output_text = output
|
|
elif isinstance(output, list):
|
|
for item in output:
|
|
if isinstance(item, dict):
|
|
if item.get("type") == "message":
|
|
content = item.get("content", [])
|
|
for c in content:
|
|
if isinstance(c, dict) and c.get("type") == "output_text":
|
|
output_text = c.get("text", "")
|
|
break
|
|
elif "text" in item:
|
|
output_text = item["text"]
|
|
elif isinstance(item, str):
|
|
output_text = item
|
|
if output_text:
|
|
break
|
|
|
|
# Also check for choices (older format)
|
|
if not output_text and "choices" in response:
|
|
for choice in response["choices"]:
|
|
if "message" in choice:
|
|
output_text = choice["message"].get("content", "")
|
|
break
|
|
|
|
if not output_text:
|
|
return items
|
|
|
|
# Extract JSON from the response
|
|
json_match = re.search(r'\{[\s\S]*"items"[\s\S]*\}', output_text)
|
|
if json_match:
|
|
try:
|
|
data = json.loads(json_match.group())
|
|
items = data.get("items", [])
|
|
except json.JSONDecodeError:
|
|
pass
|
|
|
|
# Validate and clean items
|
|
clean_items = []
|
|
for i, item in enumerate(items):
|
|
if not isinstance(item, dict):
|
|
continue
|
|
|
|
url = item.get("url", "")
|
|
if not url or "reddit.com" not in url:
|
|
continue
|
|
|
|
clean_item = {
|
|
"id": f"R{i+1}",
|
|
"title": str(item.get("title", "")).strip(),
|
|
"url": url,
|
|
"subreddit": str(item.get("subreddit", "")).strip().lstrip("r/"),
|
|
"date": item.get("date"),
|
|
"why_relevant": str(item.get("why_relevant", "")).strip(),
|
|
"relevance": min(1.0, max(0.0, float(item.get("relevance", 0.5)))),
|
|
}
|
|
|
|
# Validate date format
|
|
if clean_item["date"]:
|
|
if not re.match(r'^\d{4}-\d{2}-\d{2}$', str(clean_item["date"])):
|
|
clean_item["date"] = None
|
|
|
|
clean_items.append(clean_item)
|
|
|
|
return clean_items
|