2024-06-04 23:06:47 +00:00
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 } vtuber -- vtuber instance from strapi
* @ returns { String } filePath -- path on disk where the image was saved
* /
2024-06-13 01:38:11 +00:00
export async function getImage ( vtuber ) {
if ( ! vtuber ) throw new Error ( 'first arg must be vtuber instance' ) ;
2024-06-04 23:06:47 +00:00
const { twitter : twitterUrl , fanslyId : fanslyId } = vtuber . attributes
const twitterUsername = twitterUrl && twitter . regex . username . exec ( twitterUrl ) . at ( 1 )
let img ;
if ( twitterUrl ) {
2024-06-13 01:38:11 +00:00
img = await twitter . data . image ( twitterUsername )
2024-06-04 23:06:47 +00:00
} else if ( fanslyId ) {
2024-06-13 01:38:11 +00:00
img = await fansly . data . image ( fanslyId )
2024-06-04 23:06:47 +00:00
} else {
2024-06-13 01:38:11 +00:00
const msg = ` while attempting to get vtuber image, there was neither twitterUrl nor fanslyId listed. One of these must exist for us to download an image. \n vtuber= ${ JSON . stringify ( vtuber , null , 2 ) } `
2024-06-04 23:06:47 +00:00
console . error ( msg )
throw new Error ( msg )
}
return img
}