From 12a4de42cf81b2772333d10ccd11bd5f199b9e71 Mon Sep 17 00:00:00 2001 From: teddit Date: Fri, 19 Feb 2021 20:29:39 +0100 Subject: [PATCH] add gfycat.com support (fixes #15) --- config.js.template | 3 ++- inc/downloadAndSave.js | 6 +++++- inc/processJsonPost.js | 9 +++++++-- inc/processPostMedia.js | 21 +++++---------------- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/config.js.template b/config.js.template index f54006c..a9c81c8 100644 --- a/config.js.template +++ b/config.js.template @@ -38,7 +38,7 @@ const config = { searches: 600, sidebar: 60 * 60 * 24 * 7, // 7 days shorts: 60 * 60 * 24 * 31, - wikis: 60* 60 * 24 * 7, + wikis: 60 * 60 * 24 * 7, subreddits_explore: { front: 60 * 60 * 24 * 1, new_page: 60 @@ -51,6 +51,7 @@ const config = { reddit: true, // old.reddit.com and reddit.com --> instance domain (config.domain) }, valid_media_domains: ['preview.redd.it', 'external-preview.redd.it', 'i.redd.it', 'v.redd.it', 'a.thumbs.redditmedia.com', 'b.thumbs.redditmedia.com', 'emoji.redditmedia.com', 'thumbs.gfycat.com', 'i.ytimg.com'], + valid_embed_video_domains: ['gfycat.com', 'youtube.com'], reddit_api_error_text: `Seems like your instance is either blocked (e.g. due to API rate limiting), reddit is currently down, or your API key is expired and not renewd properly. This can also happen for other reasons.` }; diff --git a/inc/downloadAndSave.js b/inc/downloadAndSave.js index 0582d8e..2afaaab 100644 --- a/inc/downloadAndSave.js +++ b/inc/downloadAndSave.js @@ -35,7 +35,8 @@ module.exports = function(tools) { if (file_prefix === 'flair_') { // Flair emojis end in the name without a file extension file_ext = 'png' - } else if(!pathname.includes('.')) { /** + } else if(!pathname.includes('.')) { + /** * Sometimes reddit API returns video without extension, like * "DASH_480" and not "DASH_480.mp4". */ @@ -57,6 +58,9 @@ module.exports = function(tools) { resolve('') } else { let filename = `${temp_url.pathname.substr(1).split('/')[0]}.${file_ext}` + if(temp_url.hostname === 'thumbs.gfycat.com') + filename = `${temp_url.pathname.substr(1).split('/')[0]}` + let path = `./static/vids/${dir}${filename}` let temp_path = `./static/vids/${dir}temp_${filename}` if(!fs.existsSync(path)) { diff --git a/inc/processJsonPost.js b/inc/processJsonPost.js index b0524e7..9906181 100644 --- a/inc/processJsonPost.js +++ b/inc/processJsonPost.js @@ -39,12 +39,17 @@ module.exports = function(fetch) { user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '') } - let validEmbedDomains = ['gfycat.com', 'youtube.com'] + let valid_embed_video_domains = ['gfycat.com'] let has_gif = false let gif_to_mp4 = null let reddit_video = null + let embed_video = false + + if(post.media) + if(valid_embed_video_domains.includes(post.media.type)) + embed_video = true - if(post.preview) { + if(post.preview && !embed_video) { if(post.preview.reddit_video_preview) { if(post.preview.reddit_video_preview.is_gif) { has_gif = true diff --git a/inc/processPostMedia.js b/inc/processPostMedia.js index cbda0e2..caea4fc 100644 --- a/inc/processPostMedia.js +++ b/inc/processPostMedia.js @@ -3,28 +3,17 @@ module.exports = function() { this.processPostMedia = (obj, post, post_media, has_gif, reddit_video, gif_to_mp4) => { return new Promise(resolve => { (async () => { - let valid_embed_domains = ['gfycat.com', 'youtube.com'] if(post_media || has_gif) { if(!has_gif) { - if(valid_embed_domains.includes(post_media.type)) { + if(config.valid_embed_video_domains.includes(post_media.type)) { if(post_media.type === 'gfycat.com') { obj.has_media = true + let video_url = post_media.oembed.thumbnail_url + video_url = video_url.replace('size_restricted.gif', 'mobile.mp4') obj.media = { - source: await downloadAndSave(post_media.oembed.thumbnail_url), + source: await downloadAndSave(video_url), height: post_media.oembed.thumbnail_height, - width: post_media.oembed.thumbnail_width, - duration: null, - is_gif: null, - not_hosted_in_reddit: true, - embed_src: null - } - try { - let str = post_media.oembed.html - let r = /iframe.*?src=\"(.*?)\"/; - let src = r.exec(str)[1] - obj.media.embed_src = cleanUrl(src) - } catch(error) { - console.error(`Error while trying to get src link from embed html.`, error) + width: post_media.oembed.thumbnail_width } } if(post_media.type === 'youtube.com') {