Created
March 23, 2026 18:26
-
-
Save mathivhahy/9f2ece426f8bce12f9942bb194f6ba7d to your computer and use it in GitHub Desktop.
reddit search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * reddit-monitor.ts | |
| * | |
| * Personal monitoring script that periodically searches Reddit for posts | |
| * matching keyword queries. Read-only — no posting, voting, or user interaction. | |
| * | |
| * Uses Reddit's Data API search endpoint (oauth.reddit.com) to find recent | |
| * posts and comments for personal alerting. | |
| * | |
| * Usage: npx tsx reddit-monitor.ts | |
| */ | |
| const REDDIT_CLIENT_ID = process.env.REDDIT_CLIENT_ID!; | |
| const REDDIT_CLIENT_SECRET = process.env.REDDIT_CLIENT_SECRET!; | |
| const QUERY = '"example query"'; | |
| const RECENCY_MINUTES = 60; | |
| const POLL_INTERVAL_MS = 30 * 60 * 1000; | |
| async function getAccessToken(): Promise<string> { | |
| const response = await fetch('https://www.reddit.com/api/v1/access_token', { | |
| method: 'POST', | |
| headers: { | |
| 'Authorization': 'Basic ' + btoa(`${REDDIT_CLIENT_ID}:${REDDIT_CLIENT_SECRET}`), | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| 'User-Agent': 'nodejs:reddit-monitor:1.0 (personal script)', | |
| }, | |
| body: 'grant_type=client_credentials', | |
| }); | |
| const data = await response.json(); | |
| return data.access_token; | |
| } | |
| async function searchPosts(token: string): Promise<any[]> { | |
| const params = new URLSearchParams({ | |
| q: QUERY, | |
| sort: 'new', | |
| t: 'hour', | |
| type: 'link', | |
| limit: '25', | |
| }); | |
| const response = await fetch(`https://oauth.reddit.com/search?${params}`, { | |
| headers: { | |
| 'Authorization': `Bearer ${token}`, | |
| 'User-Agent': 'nodejs:reddit-monitor:1.0 (personal script)', | |
| }, | |
| }); | |
| const data = await response.json(); | |
| return data.data.children.map((child: any) => child.data); | |
| } | |
| function filterByRecency(posts: any[]): any[] { | |
| const cutoff = RECENCY_MINUTES * 60 * 1000; | |
| return posts.filter((post) => Date.now() - post.created_utc * 1000 <= cutoff); | |
| } | |
| async function main() { | |
| const token = await getAccessToken(); | |
| while (true) { | |
| console.log(`[${new Date().toISOString()}] Searching: ${QUERY}`); | |
| const posts = await searchPosts(token); | |
| const recent = filterByRecency(posts); | |
| console.log(` Found ${posts.length} posts, ${recent.length} within ${RECENCY_MINUTES}m window`); | |
| for (const post of recent) { | |
| console.log(` r/${post.subreddit} | score:${post.score} | ${post.title}`); | |
| } | |
| await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS)); | |
| } | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment