ripunzel/app.js

48 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-09-11 03:16:53 +00:00
'use strict';
const config = require('config');
const util = require('util');
2024-09-11 03:16:53 +00:00
const yargs = require('yargs').argv;
const snoowrap = require('snoowrap');
const methods = require('./methods/methods.js');
const dissectLink = require('./dissectLink.js');
const fetchContent = require('./fetchContent.js');
2024-09-11 03:16:53 +00:00
const reddit = new snoowrap(config.reddit.api);
2024-09-11 03:16:53 +00:00
reddit.getUser(yargs.user).getSubmissions({
sort: yargs.sort || config.reddit.sort,
limit: yargs.limit || config.reddit.limit
2024-09-11 03:16:53 +00:00
}).then(submissions => {
const curatedPosts = submissions.map((submission, index) => {
2024-09-11 03:16:53 +00:00
return {
id: submission.id,
index: index,
2024-09-11 03:16:53 +00:00
title: submission.title,
text: submission.selftext,
user: submission.author.name,
permalink: submission.permalink,
url: submission.url,
datetime: submission.created_utc * 1000,
subreddit: submission.subreddit.display_name,
host: dissectLink(submission.url)
2024-09-11 03:16:53 +00:00
};
});
return Promise.all(curatedPosts.reduce((acc, post) => {
if(post.host && methods[post.host.method]) {
acc = acc.concat(methods[post.host.method](post).then(content => {
post.content = content;
return post;
}));
} else {
console.log('\x1b[33m%s\x1b[0m', `"${post.title}": '${post.url}' not supported :(`);
}
return acc;
}, []));
}).then(posts => fetchContent(posts)).catch(error => {
console.error(error);
2024-09-11 03:16:53 +00:00
});