// Name: Export Reddit Bookmarks // Description: Grab all Reddit bookmarks from the past week and save as JSON // Author: johnlindquist import "@johnlindquist/kit" // Get Reddit API credentials const REDDIT_CLIENT_ID = await env("REDDIT_CLIENT_ID", { hint: "Get from https://www.reddit.com/prefs/apps - create a 'script' app", }) const REDDIT_CLIENT_SECRET = await env("REDDIT_CLIENT_SECRET", { hint: "Secret from your Reddit app", secret: true, }) const REDDIT_USERNAME = await env("REDDIT_USERNAME") const REDDIT_PASSWORD = await env("REDDIT_PASSWORD", { hint: "Your Reddit password", secret: true, }) // Get Reddit access token const getAccessToken = async () => { const auth = Buffer.from(`${REDDIT_CLIENT_ID}:${REDDIT_CLIENT_SECRET}`).toString('base64') const response = await post('https://www.reddit.com/api/v1/access_token', `grant_type=password&username=${REDDIT_USERNAME}&password=${REDDIT_PASSWORD}`, { headers: { 'Authorization': `Basic ${auth}`, 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'ScriptKit:RedditBookmarks:1.0.0 (by /u/scriptkit)' } } ) return response.data.access_token } // Get bookmarks from Reddit const getBookmarks = async (accessToken) => { const oneWeekAgo = Date.now() / 1000 - (7 * 24 * 60 * 60) // 7 days in seconds let bookmarks = [] let after = null while (true) { const url = `https://oauth.reddit.com/user/${REDDIT_USERNAME}/saved` const params = new URLSearchParams({ limit: '100', ...(after && { after }) }) const response = await get(`${url}?${params}`, { headers: { 'Authorization': `Bearer ${accessToken}`, 'User-Agent': 'ScriptKit:RedditBookmarks:1.0.0 (by /u/scriptkit)' } }) const posts = response.data.data.children if (posts.length === 0) break // Filter posts from the past week const recentPosts = posts.filter(post => post.data.created_utc >= oneWeekAgo) if (recentPosts.length === 0) break // No more recent posts bookmarks.push(...recentPosts.map(post => ({ id: post.data.id, title: post.data.title, url: post.data.url, permalink: `https://reddit.com${post.data.permalink}`, subreddit: post.data.subreddit, author: post.data.author, score: post.data.score, created_utc: post.data.created_utc, created_date: new Date(post.data.created_utc * 1000).toISOString(), selftext: post.data.selftext || null, is_self: post.data.is_self, num_comments: post.data.num_comments }))) after = response.data.data.after if (!after) break } return bookmarks } try { setStatus({ message: "Getting Reddit access token...", status: "busy" }) const accessToken = await getAccessToken() setStatus({ message: "Fetching bookmarks...", status: "busy" }) const bookmarks = await getBookmarks(accessToken) if (bookmarks.length === 0) { await div(md("# No bookmarks found from the past week")) exit() } // Save to Downloads directory const downloadsPath = home("Downloads") const timestamp = formatDate(new Date(), "yyyy-MM-dd-HH-mm-ss") const filename = `reddit-bookmarks-${timestamp}.json` const filepath = path.join(downloadsPath, filename) setStatus({ message: "Saving bookmarks...", status: "busy" }) await writeFile(filepath, JSON.stringify(bookmarks, null, 2)) setStatus({ message: "Complete!", status: "success" }) await div(md(` # Reddit Bookmarks Exported Found **${bookmarks.length}** bookmarks from the past week. Saved to: \`${filename}\` ## Recent Bookmarks: ${bookmarks.slice(0, 5).map(bookmark => `- [${bookmark.title}](${bookmark.url}) (r/${bookmark.subreddit})` ).join('\n')} ${bookmarks.length > 5 ? `\n...and ${bookmarks.length - 5} more` : ''} `)) await revealFile(filepath) } catch (error) { console.error("Error:", error) await div(md(` # Error Fetching Bookmarks ${error.message} Make sure your Reddit API credentials are correct and you have the necessary permissions. `)) }