Added limit and sort parameters, post index variable and relevant usage documentation.

This commit is contained in:
DebaucheryLibrarian 2024-09-11 05:16:53 +02:00
parent e9081a2412
commit 591d0054b3
4 changed files with 21 additions and 13 deletions

View File

@ -6,6 +6,10 @@
## Usage ## Usage
`node app.js --user={username}` `node app.js --user={username}`
### Optional parameters
* `--limit={number}`: Maximum amount posts to fetch content from
* `--sort={method}`: How posts should be sorted while fetched. This affects the `$postIndex` variable, and in combination with a `--limit` decides what posts will be included
## Configuration ## Configuration
The default configuration aims to be sensible, and the application may be used without any further tweaking. However, this application came to life because I was not satisfied by the organizatory facilities of similar applications, thus a multitude of options are at your disposal as described in this document. The default configuration aims to be sensible, and the application may be used without any further tweaking. However, this application came to life because I was not satisfied by the organizatory facilities of similar applications, thus a multitude of options are at your disposal as described in this document.
@ -17,12 +21,11 @@ Path patterns dictate where and how a file will be saved. Various variables and
* `$postTitle`: The title of the reddit post * `$postTitle`: The title of the reddit post
* `$postUser`: The user that submitted the post, almost always equivalent to the `--user` command line argument * `$postUser`: The user that submitted the post, almost always equivalent to the `--user` command line argument
* `$postDate`: The submission date of the reddit post, formatted by the `dateformat` configuration described below * `$postDate`: The submission date of the reddit post, formatted by the `dateformat` configuration described below
* `$postIndex`: The index of the post according to the sort method
* `$albumId`: The ID of the media host album * `$albumId`: The ID of the media host album
* `$albumTitle`: The title of the media host album * `$albumTitle`: The title of the media host album
* `$albumDescription`: The description of the media host album * `$albumDescription`: The description of the media host album
* `$albumDate`: The submission date of the media host album, formatted by the `dateformat` configuration described below * `$albumDate`: The submission date of the media host album, formatted by the `dateformat` configuration described below
* `$itemId`: The ID of the individual image or video * `$itemId`: The ID of the individual image or video
* `$itemTitle`: The title of the individual image or video * `$itemTitle`: The title of the individual image or video
* `$itemDescription`: The description of the individual image or video * `$itemDescription`: The description of the individual image or video

8
app.js
View File

@ -8,14 +8,16 @@ const methods = require('./methods/methods.js');
const dissectLink = require('./dissectLink.js'); const dissectLink = require('./dissectLink.js');
const fetchContent = require('./fetchContent.js'); const fetchContent = require('./fetchContent.js');
const reddit = new snoowrap(config.reddit); const reddit = new snoowrap(config.reddit.api);
reddit.getUser(yargs.user).getSubmissions({ reddit.getUser(yargs.user).getSubmissions({
limit: Infinity sort: yargs.sort || config.reddit.sort,
limit: yargs.limit || config.reddit.limit
}).then(submissions => { }).then(submissions => {
const curatedPosts = submissions.map(submission => { const curatedPosts = submissions.map((submission, index) => {
return { return {
id: submission.id, id: submission.id,
index: index,
title: submission.title, title: submission.title,
text: submission.selftext, text: submission.selftext,
user: submission.author.name, user: submission.author.name,

View File

@ -32,14 +32,14 @@ function textPostToStream(item) {
}); });
}; };
function fetchItem(item, index, post, attempt) { function fetchItem(item, post, attempt) {
function retry(error) { function retry(error) {
console.log(error); console.log(error);
if(attempt < 3) { if(attempt < 3) {
console.log('Retrying...'); console.log('Retrying...');
return fetchItem(item, index, post, ++attempt); return fetchItem(item, post, ++attempt);
} }
}; };
@ -58,16 +58,18 @@ module.exports = function(posts) {
return Promise.all(posts.map(post => { return Promise.all(posts.map(post => {
return Promise.resolve().then(() => { return Promise.resolve().then(() => {
return Promise.all(post.content.items.map((item, index) => { return Promise.all(post.content.items.map((item, index) => {
item.index = index;
if(item.self) { if(item.self) {
return textPostToStream(item); return textPostToStream(item);
} }
return fetchItem(item, index, post, 0); return fetchItem(item, post, 0);
})); }));
}).then(items => { }).then(items => {
return Promise.all(items.map((item, index) => { return Promise.all(items.map(item => {
const type = item.type.split('/')[0]; const type = item.type.split('/')[0];
const filepath = post.content.album ? interpolate(config.patterns.album[type], post, item, index) : interpolate(config.patterns[type], post, item, index); const filepath = post.content.album ? interpolate(config.patterns.album[type], post, item) : interpolate(config.patterns[type], post, item);
return Promise.resolve().then(() => { return Promise.resolve().then(() => {
return fs.ensureDir(path.dirname(filepath)); return fs.ensureDir(path.dirname(filepath));

View File

@ -10,14 +10,15 @@ const extensions = {
'video/webm': '.webm' 'video/webm': '.webm'
}; };
function interpolate(path, post, item, index) { function interpolate(path, post, item) {
const dateFormat = config.patterns.dateformat || 'YYYYMMDD'; const dateFormat = config.patterns.dateformat || 'YYYYMMDD';
const vars = { const vars = {
$postId: post.id, $postId: post.id,
$postTitle: post.title, $postTitle: post.title,
$postUser: post.user, $postUser: post.user,
$postDate: dateFns.format(post.datetime, dateFormat) $postDate: dateFns.format(post.datetime, dateFormat),
$postIndex: post.index + config.patterns.indexOffset
}; };
if(post.content.album) { if(post.content.album) {
@ -35,7 +36,7 @@ function interpolate(path, post, item, index) {
$itemTitle: item.title, $itemTitle: item.title,
$itemDescription: item.description, $itemDescription: item.description,
$itemDate: dateFns.format(item.datetime, dateFormat), $itemDate: dateFns.format(item.datetime, dateFormat),
$itemIndex: index + config.patterns.indexOffset, $itemIndex: item.index + config.patterns.indexOffset,
$ext: extensions[item.type] $ext: extensions[item.type]
}); });
} }