Created
December 10, 2022 05:52
-
-
Save andrewsosa/dc30a248df55afb0ad4c0a65277c9459 to your computer and use it in GitHub Desktop.
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
// Import the necessary libraries | |
const request = require('request'); | |
const async = require('async'); | |
// Set the necessary credentials and tokens | |
const appId = 'YOUR_APP_ID'; | |
const appSecret = 'YOUR_APP_SECRET'; | |
const accessToken = 'YOUR_ACCESS_TOKEN'; | |
// Set the base URL for the Reddit API | |
const baseUrl = 'https://oauth.reddit.com'; | |
// Set the username of the user whose saved posts you want to download | |
const username = 'YOUR_USERNAME'; | |
// Set the maximum number of saved posts to retrieve per page | |
const limit = 100; | |
// Set the initial URL for the first page of saved posts | |
let url = `${baseUrl}/user/${username}/saved?limit=${limit}`; | |
// Set the options for the API request | |
const options = { | |
url: url, | |
headers: { | |
'Authorization': `Bearer ${accessToken}`, | |
'User-Agent': `${appId} by ${username}` | |
} | |
}; | |
// Create an async queue to process the pages of saved posts | |
const q = async.queue((task, callback) => { | |
// Make the request to the Reddit API | |
request(task.options, (error, response, body) => { | |
if (!error && response.statusCode == 200) { | |
// Parse the response body as JSON | |
const data = JSON.parse(body); | |
// Get the list of saved posts | |
const posts = data.data.children; | |
// Iterate over the posts and retrieve the necessary information | |
posts.forEach(post => { | |
// Get the post ID and URL | |
const id = post.data.id; | |
const url = post.data.url; | |
// Do something with the post ID and URL... | |
}); | |
// Check if there are more pages of saved posts to retrieve | |
if (data.data.after) { | |
// Set the URL for the next page of saved posts | |
task.options.url = `${baseUrl}/user/${username}/saved?limit=${limit}&after=${data.data.after}`; | |
// Add the next page to the queue | |
q.push(task); | |
} | |
// Call the callback to indicate that the task is complete | |
callback(); | |
} else { | |
// Handle errors... | |
} | |
}); | |
}); | |
// Push the initial task to the queue | |
q.push({ options }); | |
// Process the queue | |
q.drain = () => { | |
// All pages of saved posts have been processed... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment