Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
import { VIEW_TIERS, filterByTier, matchesText } from '../../../lib/library.ts';
import { fmtNum } from '../../../utils/formatters.ts';
import type { LibraryVideo } from '../videos/videoLibrary.ts';
export type ShortsSortField = 'viewCount' | 'likeCount' | 'commentCount' | 'engagementRate' | 'publishedAt';
export interface ShortsSortOption {
value: ShortsSortField;
label: string;
}
export interface ShortsQuery {
channelFilter: string;
search: string;
tier: string;
sortField: ShortsSortField;
}
const SHORT_MAX_SECONDS = 60;
export const SHORTS_SORT_OPTIONS: ShortsSortOption[] = [
{ value: 'viewCount', label: 'Most viewed' },
{ value: 'likeCount', label: 'Most liked' },
{ value: 'commentCount', label: 'Most comments' },
{ value: 'engagementRate', label: 'Best engagement rate' },
{ value: 'publishedAt', label: 'Newest first' },
];
// a sort read from the address falls back to most viewed when it is not one of the options
export function shortsSortOf(value: string): ShortsSortField {
return SHORTS_SORT_OPTIONS.find(option => option.value === value)?.value ?? 'viewCount';
}
export function isShort(video: Pick<LibraryVideo, 'durationSeconds'>): boolean {
return video.durationSeconds > 0 && video.durationSeconds <= SHORT_MAX_SECONDS;
}
export function shortsOf(videos: LibraryVideo[]): LibraryVideo[] {
return videos.filter(isShort);
}
function sortValue(item: LibraryVideo, field: ShortsSortField): number {
if (field === 'publishedAt') return new Date(item.publishedAt).getTime() || 0;
return Number(item[field] ?? 0);
}
export function filterAndSortShorts(shorts: LibraryVideo[], { channelFilter, search, tier, sortField }: ShortsQuery): LibraryVideo[] {
let result = channelFilter === 'all' ? shorts : shorts.filter(video => video.channelId === channelFilter);
result = result.filter(video => matchesText([video.title], search));
result = filterByTier(result, VIEW_TIERS, tier, 'viewCount');
return [...result].sort((a, b) => sortValue(b, sortField) - sortValue(a, sortField));
}
export function shortsDescription(loading: boolean, shortCount: number, channelCount: number): string {
if (loading) return 'Loading Shorts from your channels';
return `${fmtNum(shortCount)} Shorts across ${channelCount} channel${channelCount === 1 ? '' : 's'}`;
}