""" Reddit via PRAW -- official free API, needs client_id + client_secret. """ import praw from supabase_client import require_tool_keys def run(params: dict) -> dict: creds = require_tool_keys("reddit") client_id = creds["api_key"] client_secret = creds["secondary_key"] query = params.get("query") if not query: raise ValueError("query is required") subreddit_name = params.get("subreddit", "all") sort = params.get("sort", "new") time_filter = params.get("time_filter", "month") limit = int(params.get("limit", 50)) reddit = praw.Reddit( client_id=client_id, client_secret=client_secret, user_agent="xtools/1.0 (by /u/90two)", ) submissions = reddit.subreddit(subreddit_name).search( query, sort=sort, time_filter=time_filter, limit=limit ) results = [] for post in submissions: results.append( { "title": post.title, "subreddit": str(post.subreddit), "score": post.score, "comments": post.num_comments, "date": post.created_utc, "author": str(post.author) if post.author else None, "url": f"https://reddit.com{post.permalink}", } ) return {"count": len(results), "results": results}