Serve the flair images from own server

This commit is contained in:
StevenNMeza 2020-12-21 12:52:48 +01:00
parent fb202af1e5
commit 9c4cda7b42
6 changed files with 36 additions and 27 deletions

View File

@ -34,7 +34,7 @@ const config = {
shorts: 60 * 60 * 24 * 31 shorts: 60 * 60 * 24 * 31
}, },
post_comments_sort: 'confidence', // "confidence" is the default sorting in Reddit. Must be one of: confidence, top, new, controversial, old, random, qa, live. post_comments_sort: 'confidence', // "confidence" is the default sorting in Reddit. Must be one of: confidence, top, new, controversial, old, random, qa, live.
valid_media_domains: ['preview.redd.it', 'external-preview.redd.it', 'i.redd.it', 'v.redd.it', 'a.thumbs.redditmedia.com', 'b.thumbs.redditmedia.com', 'thumbs.gfycat.com', 'i.ytimg.com'], 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'],
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.` 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.`
}; };

View File

@ -198,7 +198,7 @@ module.exports = function(request, fs) {
} }
} }
this.formatLinkFlair = (post) => { this.formatLinkFlair = async (post) => {
const wrap = (inner) => `<span class="flair">${inner}</span>` const wrap = (inner) => `<span class="flair">${inner}</span>`
if (post.link_flair_text === null) if (post.link_flair_text === null)
@ -213,7 +213,7 @@ module.exports = function(request, fs) {
if (fragment.e === 'text') if (fragment.e === 'text')
flair += fragment.t flair += fragment.t
else if (fragment.e === 'emoji') else if (fragment.e === 'emoji')
flair += `<span class="emoji" style="background-image: url(${fragment.u})"></span>` flair += `<span class="emoji" style="background-image: url(${await downloadAndSave(fragment.u, 'flair_')})"></span>`
} }
return wrap(flair) return wrap(flair)
} }
@ -221,7 +221,7 @@ module.exports = function(request, fs) {
return '' return ''
} }
this.formatUserFlair = (post) => { this.formatUserFlair = async (post) => {
// Generate the entire HTML here for consistency in both pug and HTML // Generate the entire HTML here for consistency in both pug and HTML
const wrap = (inner) => `<span class="flair">${inner}</span>` const wrap = (inner) => `<span class="flair">${inner}</span>`
@ -238,7 +238,7 @@ module.exports = function(request, fs) {
if (fragment.e === 'text') if (fragment.e === 'text')
flair += fragment.t // `t` is the text flair += fragment.t // `t` is the text
else if (fragment.e === 'emoji') else if (fragment.e === 'emoji')
flair += `<span class="emoji" style="background-image: url(${fragment.u})"></span>` // `u` is the emoji URL flair += `<span class="emoji" style="background-image: url(${await downloadAndSave(fragment.u, 'flair_')})"></span>` // `u` is the emoji URL
} }
return wrap(flair) return wrap(flair)
} }

View File

@ -1,12 +1,12 @@
module.exports = function(tools) { module.exports = function(tools) {
const config = require('../config') const config = require('../config')
const {spawn} = require('child_process') const {spawn} = require('child_process')
const fs = require('fs') const fs = require('fs')
this.downloadAndSave = (url, file_prefix = '', gifmp4, isYouTubeThumbnail) => { this.downloadAndSave = (url, file_prefix = '', gifmp4, isYouTubeThumbnail) => {
/** /**
* This function downloads media (video or image) to disk. * This function downloads media (video or image) to disk.
* Returns a localized URL * Returns a localized URL
* *
* For example for images: * For example for images:
* https://external-preview.redd.it/DiaeK_j5fqpBqbatvo7GZzbHNJY2oxEym93B_3.jpg * https://external-preview.redd.it/DiaeK_j5fqpBqbatvo7GZzbHNJY2oxEym93B_3.jpg
* => * =>
@ -32,21 +32,23 @@ module.exports = function(tools) {
if(gifmp4) { if(gifmp4) {
file_ext = 'mp4' file_ext = 'mp4'
} else { } else {
if(!pathname.includes('.')) { if (file_prefix === 'flair_') {
/** // Flair emojis end in the name without a file extension
file_ext = 'png'
} else if(!pathname.includes('.')) { /**
* Sometimes reddit API returns video without extension, like * Sometimes reddit API returns video without extension, like
* "DASH_480" and not "DASH_480.mp4". * "DASH_480" and not "DASH_480.mp4".
*/ */
file_ext = 'mp4' file_ext = 'mp4'
has_extension = false has_extension = false
} else { } else {
file_ext = pathname.substring(pathname.lastIndexOf('.') + 1) file_ext = pathname.substring(pathname.lastIndexOf('.') + 1)
} }
} }
if(file_prefix === 'thumb_') if(file_prefix === 'thumb_')
dir = 'thumbs/' dir = 'thumbs/'
if(file_prefix === 'flair') if(file_prefix === 'flair_')
dir = 'flairs/' dir = 'flairs/'
if(valid_video_extensions.includes(file_ext) || gifmp4) { if(valid_video_extensions.includes(file_ext) || gifmp4) {
@ -130,7 +132,14 @@ module.exports = function(tools) {
if(temp_url.searchParams.get('width')) { if(temp_url.searchParams.get('width')) {
width = temp_url.searchParams.get('width') width = temp_url.searchParams.get('width')
} }
filename = `${file_prefix}w:${temp_url.searchParams.get('width')}_${temp_url.pathname.split('/').slice(-1)}` if(file_prefix === 'flair_') {
// Flair emojis have a full path of `UUID/name`,
// so we need to incorporate the UUID to avoid duplicates
// since names alone are not unique across all of reddit
filename = `${pathname.slice(1).replace('/', '_')}.png` // Only first replacement is fine
} else {
filename = `${file_prefix}w:${temp_url.searchParams.get('width')}_${temp_url.pathname.split('/').slice(-1)}`
}
} }
path = `./dist/pics/${dir}${filename}` path = `./dist/pics/${dir}${filename}`
if(!fs.existsSync(path)) { if(!fs.existsSync(path)) {

View File

@ -33,8 +33,8 @@ module.exports = function(fetch) {
images: null, images: null,
crosspost: false, crosspost: false,
selftext: unescape(post.selftext_html), selftext: unescape(post.selftext_html),
link_flair: formatLinkFlair(post), link_flair: await formatLinkFlair(post),
user_flair: formatUserFlair(post) user_flair: await formatUserFlair(post)
} }
let validEmbedDomains = ['gfycat.com', 'youtube.com'] let validEmbedDomains = ['gfycat.com', 'youtube.com']
@ -87,7 +87,7 @@ module.exports = function(fetch) {
selftext: unescape(post.selftext_html), selftext: unescape(post.selftext_html),
selftext_crosspost: unescape(post.crosspost.selftext_html), selftext_crosspost: unescape(post.crosspost.selftext_html),
is_crosspost: true, is_crosspost: true,
user_flair: formatUserFlair(post) user_flair: await formatUserFlair(post)
} }
} }
@ -147,7 +147,7 @@ module.exports = function(fetch) {
edited: comment.edited, edited: comment.edited,
replies: [], replies: [],
depth: 0, depth: 0,
user_flair: formatUserFlair(comment) user_flair: await formatUserFlair(comment)
} }
} else { } else {
obj = { obj = {
@ -163,7 +163,7 @@ module.exports = function(fetch) {
if(comment.replies && kind !== 'more') { if(comment.replies && kind !== 'more') {
if(comment.replies.data) { if(comment.replies.data) {
if(comment.replies.data.children.length > 0) { if(comment.replies.data.children.length > 0) {
obj.replies = processReplies(comment.replies.data.children, post_id, 1) obj.replies = await processReplies(comment.replies.data.children, post_id, 1)
} }
} }
} }
@ -202,7 +202,7 @@ module.exports = function(fetch) {
return { post_data: post_data, comments: comments_html } return { post_data: post_data, comments: comments_html }
} }
this.processReplies = (data, post_id, depth) => { this.processReplies = async (data, post_id, depth) => {
let return_replies = [] let return_replies = []
for(var i = 0; i < data.length; i++) { for(var i = 0; i < data.length; i++) {
let kind = data[i].kind let kind = data[i].kind
@ -225,7 +225,7 @@ module.exports = function(fetch) {
edited: reply.edited, edited: reply.edited,
replies: [], replies: [],
depth: depth, depth: depth,
user_flair: formatUserFlair(reply) user_flair: await formatUserFlair(reply)
} }
} else { } else {
obj = { obj = {
@ -261,7 +261,7 @@ module.exports = function(fetch) {
distinguished: comment.edited, distinguished: comment.edited,
replies: [], replies: [],
depth: depth + 1, depth: depth + 1,
user_flair: formatUserFlair(comment) user_flair: await formatUserFlair(comment)
} }
} else { } else {
objct = { objct = {
@ -283,7 +283,7 @@ module.exports = function(fetch) {
if(comment.replies) { if(comment.replies) {
if(comment.replies.data) { if(comment.replies.data) {
if(comment.replies.data.children.length > 0) { if(comment.replies.data.children.length > 0) {
objct.replies = processReplies(comment.replies.data.children, post_id, depth ) objct.replies = await processReplies(comment.replies.data.children, post_id, depth )
} }
} }
} }

View File

@ -74,8 +74,8 @@ module.exports = function() {
stickied: data.stickied, stickied: data.stickied,
is_self_link: is_self_link, is_self_link: is_self_link,
subreddit_front: subreddit_front, subreddit_front: subreddit_front,
link_flair: formatLinkFlair(data), link_flair: await formatLinkFlair(data),
user_flair: formatUserFlair(data) user_flair: await formatUserFlair(data)
} }
ret.links.push(obj) ret.links.push(obj)
} }

View File

@ -63,7 +63,7 @@ module.exports = function() {
selftext_html: unescape(post.selftext_html), selftext_html: unescape(post.selftext_html),
num_comments: post.num_comments, num_comments: post.num_comments,
permalink: post.permalink, permalink: post.permalink,
user_flair: formatUserFlair(post) user_flair: await formatUserFlair(post)
} }
} }
if(type === 't1') { if(type === 't1') {
@ -81,7 +81,7 @@ module.exports = function() {
permalink: post.permalink, permalink: post.permalink,
link_author: post.link_author, link_author: post.link_author,
link_title: post.link_title, link_title: post.link_title,
user_flair: formatUserFlair(post) user_flair: await formatUserFlair(post)
} }
} }
posts.push(obj) posts.push(obj)