Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

2.1 KB
Raw Blame
import { newRowId } from '../lib/ids.ts';
import type { Account, NewAccount } from '../../shared/types/domain.ts';
import type { YouTubeChannelData } from '../../shared/types/youtube.ts';
import type { InstagramProfile } from '../../shared/types/instagram.ts';

// a token without a known lifetime is stored with no expiry
export function tokenExpiryIso(expiresIn: number): string | null {
    return expiresIn > 0
        ? new Date(Date.now() + expiresIn * 1000).toISOString()
        : null;
}

export function buildYouTubeAccount(channelData: YouTubeChannelData): NewAccount {
    return {
        id: newRowId(),
        channelId: channelData.channelId,
        title: channelData.title,
        description: channelData.description,
        customUrl: channelData.customUrl,
        publishedAt: channelData.publishedAt,
        country: channelData.country,
        thumbnails: channelData.thumbnails,
        subscriberCount: channelData.subscriberCount,
        viewCount: channelData.viewCount,
        videoCount: channelData.videoCount,
        hiddenSubscriberCount: channelData.hiddenSubscriberCount,
        uploadsPlaylistId: channelData.uploadsPlaylistId,
        keywords: channelData.keywords,
        topicCategories: channelData.topicCategories,
        madeForKids: channelData.madeForKids,
        addedAt: new Date().toISOString(),
        lastUpdated: new Date().toISOString(),
    };
}

export function buildInstagramAccount(profile: InstagramProfile, accessToken: string, expiresIn: number): Account {
    return {
        id: newRowId(),
        platform: 'instagram',
        igUserId: profile.igUserId,
        username: profile.username,
        title: profile.name || profile.username,
        biography: profile.biography,
        website: profile.website,
        profilePictureUrl: profile.profilePictureUrl,
        followersCount: profile.followersCount,
        followsCount: profile.followsCount,
        mediaCount: profile.mediaCount,
        accessToken,
        tokenExpiresAt: tokenExpiryIso(expiresIn),
        addedAt: new Date().toISOString(),
        lastUpdated: new Date().toISOString(),
    };
}