From f5382ddc37ec67a77780d247cf70d533c222a29d Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Thu, 5 Jul 2018 02:00:45 +0200 Subject: [PATCH] No longer using imgur API for individual images. Only saving EXIF data to JPEGs. Always using global exiftool instance. --- src/fetch/content.js | 8 +++++- src/methods/imgurAlbum.js | 2 +- src/methods/imgurImage.js | 51 +++++++++++++++++++-------------------- src/save/meta.js | 23 +++--------------- 4 files changed, 37 insertions(+), 47 deletions(-) diff --git a/src/fetch/content.js b/src/fetch/content.js index 07adb00..7a809c4 100644 --- a/src/fetch/content.js +++ b/src/fetch/content.js @@ -28,6 +28,10 @@ async function getStreams(item, post) { } async function addMeta(filepath, item, post, user, ep) { + if (item.type !== 'image/jpeg') { + return false; + } + const meta = Object.entries(config.library.meta).reduce((acc, [key, value]) => { const interpolatedValue = interpolate(value, user, post, item); @@ -35,8 +39,10 @@ async function addMeta(filepath, item, post, user, ep) { }, {}); if (Object.keys(meta).length > 0) { - await saveMeta(filepath, meta, ep); + return saveMeta(filepath, meta, ep); } + + return false; } function getFilepath(item, post, user) { diff --git a/src/methods/imgurAlbum.js b/src/methods/imgurAlbum.js index 2d8b6fa..fbcccec 100644 --- a/src/methods/imgurAlbum.js +++ b/src/methods/imgurAlbum.js @@ -36,7 +36,7 @@ function imgurAlbum(post) { 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, + datetime: new Date(item.datetime * 1000), original: item })) }; diff --git a/src/methods/imgurImage.js b/src/methods/imgurImage.js index 09f646a..10f87cb 100644 --- a/src/methods/imgurImage.js +++ b/src/methods/imgurImage.js @@ -1,32 +1,31 @@ - 'use strict'; +'use strict'; -const util = require('util'); -const config = require('config'); const fetch = require('node-fetch'); -function imgurImage(post) { - return fetch(`https://api.imgur.com/3/image/${post.host.id}`, { - headers: { - 'Authorization': `Client-ID ${config.methods.imgur.clientId}` - } - }).then(res => res.json()).then(res => { - if(res.status !== 200) { - throw new Error(`Could not fetch info for imgur image '${post.host.id}': '${res.data.error}'`); - } +async function imgurImage(post) { + const res = await fetch(`https://imgur.com/${post.host.id}`); + const html = await res.text(); - return { - album: null, - items: [{ - id: res.data.id, - url: res.data.animated ? res.data.mp4 : res.data.link, - title: res.data.title, - description: res.data.description, - type: res.data.animated ? 'video/mp4' : res.data.type, - datetime: new Date(res.data.datetime * 1000), - original: res.data - }] - }; - }); -}; + if (res.status !== 200) { + throw new Error(`Could not fetch info for imgur image '${post.host.id}': '${res.data.error}'`); + } + + const dataString = html.replace(/\s+/g, ' ').match(/}}, item:(.*)}; var PREBID_TIMEOUT/)[1]; + const data = JSON.parse(dataString); + + const item = { + album: null, + items: [{ + id: data.hash, + url: data.animated ? `https://i.imgur.com/${post.host.id}.mp4` : `https://i.imgur.com/${post.host.id}${data.ext}`, + title: data.title, + description: data.description, + type: data.animated ? 'video/mp4' : data.mimetype, + datetime: new Date(data.timestamp || data.datetime), + }], + }; + + return item; +} module.exports = imgurImage; diff --git a/src/save/meta.js b/src/save/meta.js index fd3e286..ac34205 100644 --- a/src/save/meta.js +++ b/src/save/meta.js @@ -1,24 +1,9 @@ 'use strict'; -const exiftool = require('node-exiftool'); -const exiftoolBin = require('dist-exiftool'); +async function saveMeta(filepath, meta, ep) { + await ep.writeMetadata(filepath, meta, ['overwrite_original']); -function saveMeta(filepath, meta, globalExifTool) { - const ep = globalExifTool || new exiftool.ExiftoolProcess(exiftoolBin); - - return Promise.resolve().then(() => { - if(!globalExifTool) { - return ep.open(); - } - }).then(() => { - return ep.writeMetadata(filepath, meta, ['overwrite_original']); - }).then(() => { - console.log('\x1b[36m%s\x1b[0m', `Wrote metadata to '${filepath}'`); - }).then(() => { - if(!globalExifTool) { - return ep.close(); - } - }); -}; + console.log('\x1b[36m%s\x1b[0m', `Wrote metadata to '${filepath}'`); +} module.exports = saveMeta;