From b7bcb79d63fa48317d7a8f33c52b46dddbbb745d Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Fri, 20 Apr 2018 04:40:37 +0200 Subject: [PATCH] Added single item album extraction with configurable display variable, as well as a host/source name variable. --- README.md | 5 +++++ config/default.js | 6 ++++-- dissectLink.js | 9 ++++++++- interpolate.js | 6 ++++-- methods/eroshare.js | 4 +++- methods/imgurAlbum.js | 9 ++++++--- 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d5b697c..7d5b9ba 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Path patterns dictate where and how a file will be saved. Various variables and * `$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 +* `$host`: Name of the source the content was hosted on ##### Album * `$albumId`: The ID of the media host album @@ -45,6 +46,7 @@ Path patterns dictate where and how a file will be saved. Various variables and * `$itemDescription`: The description of the individual image or video * `$itemDate`: The submission date of the individual image or video, formatted by the `dateformat` configuration described below * `$itemIndex`: The index of the individual image or video in an album, offset by the `indexOffset` configuration described below +* `$extracted`: When extracting single album items is enabled and the item has been extracted, this variable will display the value of `extractedLabel` as described below in case the item was the only item in an album * `$ext`: The extension of the medium. Must typically be included, but may be omitted for self (text) posts on Unix systems ##### `dateFormat` @@ -58,3 +60,6 @@ Arrays start at 0, but as to not tire myself out debating the matter, you may of ##### `slashSubstitute` The patterns represent Unix file paths, and a `/` therefore indicates a new directory. You may freely use directories in your paths, but titles or descriptions may contain a `/` that is not supposed to create a new directory. All instances of `/` in a variable value will be replaced with the configured slash substitute. + +##### `album.extractSingleItem` and `extractedLabel` +Some albums contain only one image or video. By setting `album.extractSingleItem` to `true` (default), the item will be saved in accordance to the individual item patterns rather than the album patterns. An extracted item will inherit the title and description of the album if it has none of its own. Extracted items can be marked with the `$extracted` boolean variable, of which the display value is set with `extractedLabel`. diff --git a/config/default.js b/config/default.js index bc7d036..48abded 100644 --- a/config/default.js +++ b/config/default.js @@ -4,13 +4,15 @@ module.exports = { video: 'output/$postUser/$postDate - $itemId - $postTitle$ext', text: 'output/$postUser/$postDate - $postId - $postTitle', album: { + extractSingleItem: true, image: 'output/$postUser/$postDate - $albumId - $postTitle/$itemIndex - $itemId$ext', video: 'output/$postUser/$postDate - $albumId - $postTitle/$itemIndex - $itemId$ext' }, - dateformat: 'YYYYMMDD', + dateFormat: 'YYYYMMDD', titleLength: 200, indexOffset: 1, - slashSubstitute: '#' + slashSubstitute: '#', + extractedLabel: 'extracted-' }, reddit: { sort: 'top', diff --git a/dissectLink.js b/dissectLink.js index 46bfcc9..4903387 100644 --- a/dissectLink.js +++ b/dissectLink.js @@ -4,21 +4,27 @@ const urlPattern = require('url-pattern'); const hosts = [{ method: 'self', + label: 'self', pattern: new urlPattern('http(s)\\://(www.)reddit.com/r/:subreddit/comments/:id/:uri/') }, { method: 'reddit', + label: 'reddit', pattern: new urlPattern('http(s)\\://i.redd.it/:id.:ext') }, { method: 'imgurImage', + label: 'imgur', pattern: new urlPattern('http(s)\\://(i.)imgur.com/:id(.:ext)(?:num)') }, { method: 'imgurAlbum', + label: 'imgur', pattern: new urlPattern('http(s)\\://(m.)imgur.com/:type/:id') }, { method: 'gfycat', + label: 'gfycat', pattern: new urlPattern('http(s)\\://(:server.)gfycat.com/:id(.:ext)') }, { method: 'eroshare', + label: 'eroshare', pattern: new urlPattern('http(s)\\://eroshare.com/:id') }]; @@ -33,7 +39,8 @@ module.exports = function dissectLink(url) { if(match) { return Object.assign(match, { url: url, - method: host.method + method: host.method, + label: host.label }); } diff --git a/interpolate.js b/interpolate.js index 6f759aa..3b839f8 100644 --- a/interpolate.js +++ b/interpolate.js @@ -11,14 +11,15 @@ const extensions = { }; function interpolate(path, post, item) { - const dateFormat = config.patterns.dateformat || 'YYYYMMDD'; + const dateFormat = config.patterns.dateFormat || 'YYYYMMDD'; const vars = { $postId: post.id, $postTitle: (post.title || '').slice(0, config.patterns.titleLength), $postUser: post.user, $postDate: dateFns.format(post.datetime, dateFormat), - $postIndex: post.index + config.patterns.indexOffset + $postIndex: post.index + config.patterns.indexOffset, + $host: post.host.label }; if(post.content.album) { @@ -37,6 +38,7 @@ function interpolate(path, post, item) { $itemDescription: item.description, $itemDate: dateFns.format(item.datetime, dateFormat), $itemIndex: item.index + config.patterns.indexOffset, + $extracted: item.extracted ? config.patterns.extractedLabel : '', $ext: extensions[item.type] }); } diff --git a/methods/eroshare.js b/methods/eroshare.js index 4d1680e..972d1cc 100644 --- a/methods/eroshare.js +++ b/methods/eroshare.js @@ -13,15 +13,17 @@ function eroshare(post) { return Promise.reject(`Unable to recover Eroshare video '${post.host.id}' :(`); }).then(res => { const data = JSON.parse(res.match(/var album = .*/)[0].slice(12, -1)); + const extract = config.patterns.album.extractSingleItem && data.items.length === 1; return { - album: { + album: extract ? null : { id: data.slug, title: data.title, datetime: new Date(data.created_at) }, items: data.items.map(item => { return { + extracted: extract, id: item.slug, url: item.type === 'Image' ? item.url_full_protocol : item.url_mp4, title: data.title, diff --git a/methods/imgurAlbum.js b/methods/imgurAlbum.js index ee49479..d697d9b 100644 --- a/methods/imgurAlbum.js +++ b/methods/imgurAlbum.js @@ -10,8 +10,10 @@ function imgurAlbum(post) { 'Authorization': `Client-ID ${config.methods.imgur.clientId}` } }).then(res => res.json()).then(res => { + const extract = config.patterns.album.extractSingleItem && res.data.images.length === 1; + return { - album: { + album: extract ? null : { id: res.data.id, url: res.data.link, title: res.data.title, @@ -20,10 +22,11 @@ function imgurAlbum(post) { original: res.data }, items: res.data.images.map(item => ({ + extracted: extract, id: item.id, url: item.animated ? item.mp4 : item.link, - title: item.title, - description: item.description, + title: item.title || (extract ? res.data.title : null), + description: item.description || (extract ? res.data.description : null), type: item.animated ? 'video/mp4' : item.type, datetime: item.datetime * 1000, original: item