Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import snoowrap from 'snoowrap';
- import fs from 'fs';
- // Create CSV header
- let csvRows = [];
- let submissionCount = 0;
- csvRows.push(['Subreddit', 'Interaction Count'].join(','));
- const r = new snoowrap({
- userAgent: 'Node.js script for subreddit analysis',
- clientId: 'YOUR_CLIENT_ID',
- clientSecret: 'YOUR_CLIENT_SECRET',
- username: 'REDDIT_USERNAME',
- password: 'REDDIT_PASSWORD'
- });
- async function main() {
- const subredditName = process.argv[2];
- if (!subredditName) {
- console.error('Please provide a subreddit name as an argument.');
- process.exit(1);
- }
- try {
- // Fetch the last 50 submissions from the subreddit
- const subreddit = r.getSubreddit(subredditName);
- const submissions = await subreddit.getNew({ limit: 50 });
- // Object to store subreddit interaction counts
- const subredditCounts = {};
- // Set to keep track of processed users to avoid duplicates
- const processedUsers = new Set();
- for (const submission of submissions) {
- submissionCount = submissionCount + 1;
- console.log(`Processing submission ${submissionCount}: ${submission.title}`);
- // Process the submission author (OP)
- if (submission.author && submission.author.name !== '[deleted]') {
- const authorName = submission.author.name;
- if (!processedUsers.has(authorName)) {
- processedUsers.add(authorName);
- await processUser(authorName, subredditCounts);
- }
- }
- // Fetch comments for the submission
- let comments;
- try {
- comments = await submission.expandReplies({ limit: Infinity, depth: Infinity });
- } catch (err) {
- console.error(`Error fetching comments for submission ${submission.id}:`, err.message);
- continue;
- }
- // Process commenters
- if (comments && comments.comments) {
- for (const comment of comments.comments) {
- if (comment.author && comment.author.name !== '[deleted]') {
- const commenterName = comment.author.name;
- if (!processedUsers.has(commenterName)) {
- processedUsers.add(commenterName);
- await processUser(commenterName, subredditCounts);
- }
- }
- }
- }
- }
- // Convert subredditCounts to CSV rows
- for (const [subreddit, count] of Object.entries(subredditCounts)) {
- csvRows.push([subreddit, count].join(','));
- }
- // Save the CSV file after processing
- saveCSV(`${subredditName}_interactions.csv`, csvRows);
- } catch (error) {
- console.error('An error occurred:', error.message);
- }
- }
- // Function to process a user and update subredditCounts
- async function processUser(username, subredditCounts) {
- const user = r.getUser(username);
- try {
- // Fetch the user's recent comments and submissions
- const [comments, userSubmissions] = await Promise.all([
- user.getComments({ limit: Infinity }),
- user.getSubmissions({ limit: Infinity })
- ]);
- // Count the subreddits from comments
- comments.forEach((comment) => {
- const subreddit = comment.subreddit.display_name;
- subredditCounts[subreddit] = (subredditCounts[subreddit] || 0) + 1;
- });
- // Count the subreddits from submissions
- userSubmissions.forEach((submission) => {
- const subreddit = submission.subreddit.display_name;
- subredditCounts[subreddit] = (subredditCounts[subreddit] || 0) + 1;
- });
- } catch (userError) {
- console.error(`Error fetching data for user ${username}:`, userError.message);
- }
- }
- // Function to save CSV to file
- function saveCSV(filename, rows) {
- const csvContent = rows.join('\n');
- fs.writeFileSync(filename, csvContent);
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement