From 5dce74588bd9677399e0a0ba68e2f4d206332bb1 Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Mon, 2 Jul 2018 23:15:06 +0200 Subject: [PATCH] Improved missing gfycat handling. Added 'preview' marker to index. --- src/fetch/info.js | 2 +- src/methods/gfycat.js | 43 ++++++++++++++++++++-------------------- src/save/writeToIndex.js | 28 ++++++++++++++++---------- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/fetch/info.js b/src/fetch/info.js index 01e1bbe..e91e993 100644 --- a/src/fetch/info.js +++ b/src/fetch/info.js @@ -21,7 +21,7 @@ const attachContentInfo = users => { if(config.fetch.archives.preview && post.preview) { console.log(`Found preview images for unavailable source '${post.url}' (${post.permalink})`); - return [...accPosts, {...post, content: await methods.redditPreview(post)}]; + return [...accPosts, {...post, previewFallback: true, content: await methods.redditPreview(post)}]; } return accPosts; diff --git a/src/methods/gfycat.js b/src/methods/gfycat.js index 1746276..4383d92 100644 --- a/src/methods/gfycat.js +++ b/src/methods/gfycat.js @@ -1,26 +1,27 @@ - 'use strict'; +'use strict'; -const util = require('util'); -const config = require('config'); const fetch = require('node-fetch'); -function gfycat(post) { - return fetch(`https://gfycat.com/cajax/get/${post.host.id}`).then(res => res.json()).then(res => { - return { - album: null, - items: [{ - id: res.gfyItem.gfyName, - url: res.gfyItem.webmUrl, - title: res.gfyItem.title, - description: res.gfyItem.description, - type: 'video/webm', - datetime: new Date(res.gfyItem.createDate * 1000), - original: res.gfyItem - }] - }; - }).catch(error => { - console.error(error); - }); -}; +async function gfycat(post) { + const res = await fetch(`https://gfycat.com/cajax/get/${post.host.id}`); + const data = await res.json(); + + if (data.error) { + throw new Error(data.error); + } + + return { + album: null, + items: [{ + id: data.gfyItem.gfyName, + url: data.gfyItem.webmUrl, + title: data.gfyItem.title, + description: data.gfyItem.description, + type: 'video/webm', + datetime: new Date(data.gfyItem.createDate * 1000), + original: data.gfyItem, + }], + }; +} module.exports = gfycat; diff --git a/src/save/writeToIndex.js b/src/save/writeToIndex.js index 29be71c..19f75fc 100644 --- a/src/save/writeToIndex.js +++ b/src/save/writeToIndex.js @@ -10,16 +10,24 @@ async function writeToIndex(posts, profilePaths, user) { const filename = interpolate(config.library.index.file, user, null, false); const now = new Date(); - const newAndUpdatedEntries = posts.concat(user.indexed.updated).map(post => ({ - id: post.id, - subreddit: post.subreddit, - permalink: post.permalink, - url: post.url, - hostId: post.host.id, - date: post.datetime, - indexed: now, - title: post.title, - })); + const newAndUpdatedEntries = posts.concat(user.indexed.updated).map((post) => { + const entryPost = { + id: post.id, + subreddit: post.subreddit, + permalink: post.permalink, + url: post.url, + hostId: post.host.id, + date: post.datetime, + indexed: now, + title: post.title, + }; + + if (post.previewFallback) { + entryPost.preview = true; + } + + return entryPost; + }); const data = { profile: {