93 lines
3.2 KiB
JavaScript
93 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
const config = require('config');
|
|
const Promise = require('bluebird');
|
|
const UrlPattern = require('url-pattern');
|
|
|
|
const interpolate = require('../interpolate.js');
|
|
const fetchItem = require('../fetch/item.js');
|
|
const textToStream = require('./textToStream.js');
|
|
const save = require('./save.js');
|
|
|
|
async function saveProfileImage(user, args) {
|
|
if (!args.redownloadProfile && user.indexed.profile.image) {
|
|
console.log('\x1b[33m%s\x1b[0m', `Ignoring already present profile image for '${user.name}' (https://reddit.com/user/${user.name})`);
|
|
|
|
return user.indexed.profile.image;
|
|
}
|
|
|
|
if (config.library.profile.image && !user.fallback && !user.deleted) {
|
|
const image = user.profile ? user.profile.image : user.image;
|
|
|
|
if (config.library.profile.avoidAvatar && new UrlPattern('http(s)\\://(www.)redditstatic.com/avatars/:id(.:ext)(?:query)').match(image)) {
|
|
console.log('\x1b[33m%s\x1b[0m', `Ignoring standard avatar profile image for '${user.name}' (https://reddit.com/user/${user.name})`);
|
|
|
|
return null;
|
|
}
|
|
|
|
const filepath = interpolate(
|
|
config.library.profile.image,
|
|
{
|
|
// pass profile image as item to interpolate extension variable
|
|
url: image,
|
|
},
|
|
null,
|
|
null,
|
|
null,
|
|
user,
|
|
);
|
|
|
|
try {
|
|
const stream = await fetchItem(image, 0, { permalink: `https://reddit.com/user/${user.name}` });
|
|
const targets = await save(filepath, stream);
|
|
|
|
return targets[0];
|
|
} catch (error) {
|
|
console.log('\x1b[33m%s\x1b[0m', `Could not save profile image for '${user.name}': ${error} (https://reddit.com/user/${user.name})`);
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
async function saveProfileDescription(user, args) {
|
|
if (!args.redownloadProfile && user.indexed.profile.description) {
|
|
console.log('\x1b[33m%s\x1b[0m', `Ignoring already present profile description for '${user.name}' (https://reddit.com/user/${user.name})`);
|
|
|
|
return user.indexed.profile.description;
|
|
}
|
|
|
|
if (config.library.profile.description && !user.fallback && !user.deleted) {
|
|
if (user.profile && user.profile.description) {
|
|
const filepath = interpolate(config.library.profile.description, null, null, null, null, user);
|
|
const stream = textToStream(user.profile.description);
|
|
|
|
try {
|
|
const targets = await save(filepath, stream);
|
|
|
|
return targets[0];
|
|
} catch (error) {
|
|
console.log('\x1b[33m%s\x1b[0m', `Could not save profile description for '${user.name}': ${error} (https://reddit.com/user/${user.name})`);
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
console.log('\x1b[33m%s\x1b[0m', `No profile description for '${user.name}' (https://reddit.com/user/${user.name})`);
|
|
|
|
return null;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
async function saveProfileDetails(user, args) {
|
|
const [image, description] = await Promise.all([saveProfileImage(user, args), saveProfileDescription(user, args)]);
|
|
|
|
return { image, description };
|
|
}
|
|
|
|
module.exports = saveProfileDetails;
|