import TwitterApi , { ApiResponseError , UserFollowingV2Paginator } from 'twitter-api-v2' ; import { Db } from 'mongodb' ; const twitterClient = new TwitterApi ( process . env . TWITTER_BEARER_TOKEN || '' ) ; const roClient = twitterClient . readOnly ; export const fetchTwitterUser = async ( db , twitterName ) => { const user = await getTwitterUser ( twitterName ) ; if ( ! user ) { return ; } const avatarUrl = user ?. profile_image_url ?. replace ( '_normal' , '' ) ; const { id , description , public_metrics = { } } = user ; const { followers_count , following_count , tweet_count , listed_count } = public_metrics ; const publicMetrics = { followers : followers_count , following : following_count , tweet : tweet_count , listed : listed_count } ; return { twitterName , avatarUrl , id , description , publicMetrics } ; } ; export const getTwitterUser = async ( twitterName ) => { try { const data = await roClient . v2 . userByUsername ( twitterName , { 'user.fields' : [ 'public_metrics' , 'profile_image_url' , 'description' ] } ) ; const user = data && data . data ; return user ; } catch ( error ) { console . log ( '// getTwitterUser error' ) ; console . log ( error . rateLimit ) ; const resetTime = new Date ( error . rateLimit . reset * 1000 ) ; console . log ( resetTime ) ; console . log ( error . data ) ; return ; } } ; export const getTwitterFollowings = async ( twitterId ) => { let followings = [ ] ; try { const result = await roClient . v2 . following ( twitterId , { asPaginator : true , max_results : 1000 } ) ; for await ( const following of result ) { followings . push ( following ) ; } } catch ( error ) { console . log ( '// getTwitterFollowings error' ) ; if ( error instanceof ApiResponseError && error . rateLimitError && error . rateLimit ) { console . log ( `You just hit the rate limit! Limit for this endpoint is ${ error . rateLimit . limit } requests!` ) ; console . log ( `Request counter will reset at timestamp ${ error . rateLimit . reset }.` ) ; } else { console . log ( error ) ; console . log ( error ?. data ?. errors ) ; } } const followingsUsernames = followings . map ( f => f . username ) ; return followingsUsernames ; } ;