From 591d0054b3facf333d04364ede58badf40380da0 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 11 Sep 2024 05:16:53 +0200 Subject: [PATCH] Added limit and sort parameters, post index variable and relevant usage documentation. --- README.md | 7 +++++-- app.js | 8 +++++--- fetchContent.js | 12 +++++++----- interpolate.js | 7 ++++--- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c03be79..0c1bf88 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ ## Usage `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 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 * `$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 - +* `$postIndex`: The index of the post according to the sort method * `$albumId`: The ID of the media host album * `$albumTitle`: The title 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 - * `$itemId`: The ID of the individual image or video * `$itemTitle`: The title of the individual image or video * `$itemDescription`: The description of the individual image or video diff --git a/app.js b/app.js index 8c24671..41b1870 100644 --- a/app.js +++ b/app.js @@ -8,14 +8,16 @@ const methods = require('./methods/methods.js'); const dissectLink = require('./dissectLink.js'); const fetchContent = require('./fetchContent.js'); -const reddit = new snoowrap(config.reddit); +const reddit = new snoowrap(config.reddit.api); reddit.getUser(yargs.user).getSubmissions({ - limit: Infinity + sort: yargs.sort || config.reddit.sort, + limit: yargs.limit || config.reddit.limit }).then(submissions => { - const curatedPosts = submissions.map(submission => { + const curatedPosts = submissions.map((submission, index) => { return { id: submission.id, + index: index, title: submission.title, text: submission.selftext, user: submission.author.name, diff --git a/fetchContent.js b/fetchContent.js index 7b06424..9014912 100644 --- a/fetchContent.js +++ b/fetchContent.js @@ -32,14 +32,14 @@ function textPostToStream(item) { }); }; -function fetchItem(item, index, post, attempt) { +function fetchItem(item, post, attempt) { function retry(error) { console.log(error); if(attempt < 3) { 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.resolve().then(() => { return Promise.all(post.content.items.map((item, index) => { + item.index = index; + if(item.self) { return textPostToStream(item); } - return fetchItem(item, index, post, 0); + return fetchItem(item, post, 0); })); }).then(items => { - return Promise.all(items.map((item, index) => { + return Promise.all(items.map(item => { 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 fs.ensureDir(path.dirname(filepath)); diff --git a/interpolate.js b/interpolate.js index ef0605d..415680d 100644 --- a/interpolate.js +++ b/interpolate.js @@ -10,14 +10,15 @@ const extensions = { 'video/webm': '.webm' }; -function interpolate(path, post, item, index) { +function interpolate(path, post, item) { const dateFormat = config.patterns.dateformat || 'YYYYMMDD'; const vars = { $postId: post.id, $postTitle: post.title, $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) { @@ -35,7 +36,7 @@ function interpolate(path, post, item, index) { $itemTitle: item.title, $itemDescription: item.description, $itemDate: dateFns.format(item.datetime, dateFormat), - $itemIndex: index + config.patterns.indexOffset, + $itemIndex: item.index + config.patterns.indexOffset, $ext: extensions[item.type] }); }