import dotenv from 'dotenv' dotenv.config({ path: '../../.env' }) import twitter from './twitter.js' import fansly from './fansly.js' /** * Acquire a vtuber image from the www * * Sources preference * 1. Twitter * 2. Fansly * * Our task is to download an avatar image of the vtuber. * A slug is good for pulling a record from the database. From there, we can see any social medias such as Twitter or Fansly. * Twitter is preferred. * * We depend on one of these social media URLs. If there is neither Twitter or fansly listed, we throw an error. * * @param {Object} limiter -- instance of node-rate-limiter * @param {Object} vtuber -- vtuber instance from strapi * @returns {String} filePath -- path on disk where the image was saved */ export async function getImage(limiter, vtuber) { if (!limiter) throw new Error('first arg must be node-rate-limiter instace'); if (!vtuber) throw new Error('second arg must be vtuber instance'); await limiter.removeTokens(1); const { twitter: twitterUrl, fanslyId: fanslyId } = vtuber.attributes const twitterUsername = twitterUrl && twitter.regex.username.exec(twitterUrl).at(1) let img; if (twitterUrl) { img = await twitter.data.image(limiter, twitterUsername) } else if (fanslyId) { img = await fansly.data.image(limiter, fanslyId) } else { const msg = 'while attempting to get vtuber image, there was neither twitter nor fansly listed. One of these must exist for us to download an image.' console.error(msg) throw new Error(msg) } return img }