Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
refactor(api): group the platform modules by platform
spacesdrivee801cb56baa2
diff --git a/backend/services/analyticsMath.ts b/backend/lib/analyticsMath.tssimilarity index 100%rename from backend/services/analyticsMath.tsrename to backend/lib/analyticsMath.tsdiff --git a/backend/routes/accounts.ts b/backend/routes/accounts.tsindex d574be9..dc8ec24 100644--- a/backend/routes/accounts.ts+++ b/backend/routes/accounts.ts@@ -1,7 +1,7 @@import { Hono } from 'hono';import { getAccounts, getAccountById, deleteAccount } from '../lib/db.ts';import { deleteVideosCache, deleteIGCache, deleteRedditCache } from '../lib/cache.ts';-import { resolveChannelId, fetchChannelData } from '../services/youtube.ts';+import { resolveChannelId, fetchChannelData } from '../services/youtube/youtubeChannels.ts';import {addYouTubeAccount, addRedditAccount, addInstagramAccount, refreshInstagramToken,} from '../services/accountConnect.ts';diff --git a/backend/routes/analytics.ts b/backend/routes/analytics.tsindex c2961d4..93727ff 100644--- a/backend/routes/analytics.ts+++ b/backend/routes/analytics.ts@@ -2,9 +2,12 @@ import { Hono } from 'hono';import { safeAccount } from '../lib/safeAccount.ts';import { getAccounts, getAccountById, updateAccount } from '../lib/db.ts';import { getVideosCache, setVideosCache, getIGCache, setIGCache } from '../lib/cache.ts';-import { fetchAllVideos, computeVideoAnalytics } from '../services/youtube.ts';-import { fetchInstagramProfile, fetchInstagramMedia, computeInstagramAnalytics } from '../services/instagram.ts';-import { channelMetrics, accountComparison, filterAndSortVideos } from '../services/youtubeLibrary.ts';+import { fetchAllVideos } from '../services/youtube/youtubeVideos.ts';+import { computeVideoAnalytics } from '../services/youtube/youtubeAnalytics.ts';+import { fetchInstagramProfile } from '../services/instagram/instagramApi.ts';+import { fetchInstagramMedia } from '../services/instagram/instagramMedia.ts';+import { computeInstagramAnalytics } from '../services/instagram/instagramAnalytics.ts';+import { channelMetrics, accountComparison, filterAndSortVideos } from '../services/youtube/youtubeLibrary.ts';import { backfillChannelData } from '../services/accountRefresh.ts';import type { AppEnv, Redis, SupabaseClient } from '../types/app.ts';import type { Account } from '../../shared/types/domain.ts';diff --git a/backend/routes/instagramAuth.ts b/backend/routes/instagramAuth.tsindex 64ef619..1c5257a 100644--- a/backend/routes/instagramAuth.ts+++ b/backend/routes/instagramAuth.ts@@ -1,7 +1,7 @@import { verifyOAuthState } from '../lib/oauthState.ts';import {readInstagramOAuthConfig, buildAuthorizationUrl, connectInstagramFromOAuth, isOAuthConfigured,-} from '../services/instagramOAuth.ts';+} from '../services/instagram/instagramOAuth.ts';import type { AppContext } from '../types/app.ts';import type { InstagramAuthUrlResponse } from '../../shared/types/instagram.ts';diff --git a/backend/routes/publishing.ts b/backend/routes/publishing.tsindex efcd415..9016106 100644--- a/backend/routes/publishing.ts+++ b/backend/routes/publishing.ts@@ -1,10 +1,8 @@import { Hono } from 'hono';import { getAccountById } from '../lib/db.ts';-import { buildMediaParams } from '../services/instagram.ts';+import { buildMediaParams } from '../services/instagram/instagramPublishing.ts';import type { AppEnv } from '../types/app.ts';-import type {- InstagramApiContainerStatus, InstagramApiErrorBody, InstagramApiIdResponse,-} from '../services/instagram.ts';+import type { InstagramApiContainerStatus, InstagramApiErrorBody, InstagramApiIdResponse } from '../services/instagram/instagramApi.ts';import type {CloudinaryConfigResponse, InstagramContainerStatusResponse, InstagramMediaPublishRequest, InstagramMediaPublishResponse,InstagramPublishingLimit, InstagramPublishingLimitResponse, InstagramPublishRequest, InstagramPublishResponse,diff --git a/backend/routes/reddit.ts b/backend/routes/reddit.tsindex 8b546a7..3e124b0 100644--- a/backend/routes/reddit.ts+++ b/backend/routes/reddit.ts@@ -1,7 +1,7 @@import { Hono } from 'hono';import { getAccountById } from '../lib/db.ts';import { getRedditCache, setRedditCache } from '../lib/cache.ts';-import { fetchRedditProfile, fetchRedditPosts, computeRedditAnalytics } from '../services/reddit.ts';+import { fetchRedditProfile, fetchRedditPosts, computeRedditAnalytics } from '../services/reddit/redditApi.ts';import type { AppEnv } from '../types/app.ts';import type { RedditCacheData } from '../../shared/types/reddit.ts';diff --git a/backend/routes/trackedContent.ts b/backend/routes/trackedContent.tsindex 3aee61a..3c15eea 100644--- a/backend/routes/trackedContent.ts+++ b/backend/routes/trackedContent.ts@@ -8,8 +8,8 @@ import {deleteTrackedPost,getAccountById,} from '../lib/db.ts';-import { fetchTrackedPostData } from '../services/reddit.ts';-import { fetchTrackedYouTubeData } from '../services/youtube.ts';+import { fetchTrackedPostData } from '../services/reddit/redditApi.ts';+import { fetchTrackedYouTubeData } from '../services/youtube/youtubeTracked.ts';import { detectContentType, manualViewsPatch, imageUrlPatch } from '../services/trackedContent.ts';import type { AppContext, AppEnv, SupabaseClient } from '../types/app.ts';import type { TrackedPostUpdates } from '../lib/db.ts';diff --git a/backend/server.ts b/backend/server.tsindex fe27485..7347208 100644--- a/backend/server.ts+++ b/backend/server.ts@@ -16,7 +16,7 @@ import trackedContentRouter from './routes/trackedContent.ts';import overviewRouter from './routes/overview.ts';import { urlHandler, callbackHandler } from './routes/instagramAuth.ts';import { processScheduledPosts } from './utils/scheduler.ts';-import { autoRefreshInstagramTokens } from './services/instagram.ts';+import { autoRefreshInstagramTokens } from './services/instagram/instagramTokens.ts';import type { AppEnv } from './types/app.ts';import type { Env } from './types/env.ts';diff --git a/backend/services/accountConnect.ts b/backend/services/accountConnect.tsindex d896aeb..19c8e2f 100644--- a/backend/services/accountConnect.ts+++ b/backend/services/accountConnect.ts@@ -1,8 +1,8 @@import { getAccounts, getAccountById, createAccount, updateAccount } from '../lib/db.ts';-import { resolveChannelId, fetchChannelData } from './youtubeChannels.ts';-import { fetchRedditProfile } from './reddit.ts';-import { fetchInstagramProfile } from './instagramApi.ts';-import { exchangeForLongLivedToken, refreshLongLivedToken } from './instagramTokens.ts';+import { resolveChannelId, fetchChannelData } from './youtube/youtubeChannels.ts';+import { fetchRedditProfile } from './reddit/redditApi.ts';+import { fetchInstagramProfile } from './instagram/instagramApi.ts';+import { exchangeForLongLivedToken, refreshLongLivedToken } from './instagram/instagramTokens.ts';import { buildYouTubeAccount, buildRedditAccount, buildInstagramAccount } from './accountFactory.ts';import type { SupabaseClient } from '../types/app.ts';import type { Account, NewAccount } from '../../shared/types/domain.ts';diff --git a/backend/services/accountRefresh.ts b/backend/services/accountRefresh.tsindex 2222666..1fdf98a 100644--- a/backend/services/accountRefresh.ts+++ b/backend/services/accountRefresh.ts@@ -1,7 +1,7 @@import { getAccounts, getAccountById, updateAccount } from '../lib/db.ts';-import { fetchChannelData } from './youtubeChannels.ts';-import { fetchRedditProfile } from './reddit.ts';-import { fetchInstagramProfile } from './instagramApi.ts';+import { fetchChannelData } from './youtube/youtubeChannels.ts';+import { fetchRedditProfile } from './reddit/redditApi.ts';+import { fetchInstagramProfile } from './instagram/instagramApi.ts';import type { SupabaseClient } from '../types/app.ts';import type { ServiceFailure } from './accountConnect.ts';import type { Account, AccountRefreshResult } from '../../shared/types/domain.ts';diff --git a/backend/services/instagram.ts b/backend/services/instagram.tsdeleted file mode 100644index ddb1af3..0000000--- a/backend/services/instagram.ts+++ /dev/null@@ -1,14 +0,0 @@-export { fetchInstagramProfile } from './instagramApi.ts';-export { exchangeForLongLivedToken, refreshLongLivedToken, autoRefreshInstagramTokens } from './instagramTokens.ts';-export {- classifyInstagramMedia, extractInstagramViews, normalizeInstagramMedia, fetchInstagramMedia,-} from './instagramMedia.ts';-export { CONTENT_TYPES, summarizeContentTypes } from './instagramContentTypes.ts';-export { computeInstagramAnalytics } from './instagramAnalytics.ts';-export { buildMediaParams } from './instagramPublishing.ts';-export type {- GraphParams, InstagramApiError, InstagramApiErrorBody, InstagramApiIdResponse, InstagramApiContainerStatus,-} from './instagramApi.ts';-export type { InstagramApiToken, InstagramTokenResult } from './instagramTokens.ts';-export type { InstagramApiMedia, InstagramApiInsight } from './instagramMedia.ts';-export type { InstagramMediaParams } from './instagramPublishing.ts';diff --git a/backend/services/instagramAnalytics.ts b/backend/services/instagram/instagramAnalytics.tssimilarity index 98%rename from backend/services/instagramAnalytics.tsrename to backend/services/instagram/instagramAnalytics.tsindex f785e23..81f4fde 100644--- a/backend/services/instagramAnalytics.ts+++ b/backend/services/instagram/instagramAnalytics.ts@@ -1,13 +1,13 @@import {sumOf, roundedAverage, medianOf, countWithinDays, postFrequency,consistencyScore, bestToAverageRatio,-} from './analyticsMath.ts';+} from '../../lib/analyticsMath.ts';import { CONTENT_TYPES, buildContentTypeStats } from './instagramContentTypes.ts';import { summarizeTiming } from './instagramTiming.ts';import { buildHashtagAnalysis, buildCaptionLengthCorrelation } from './instagramCaptions.ts';import type {InstagramAnalytics, InstagramContentTypeStatsMap, InstagramMedia, InstagramProfile, InstagramTopPost,-} from '../../shared/types/instagram.ts';+} from '../../../shared/types/instagram.ts';export type AnalyticsProfile = Pick<InstagramProfile, 'mediaCount' | 'followersCount'>;diff --git a/backend/services/instagramApi.ts b/backend/services/instagram/instagramApi.tssimilarity index 98%rename from backend/services/instagramApi.tsrename to backend/services/instagram/instagramApi.tsindex 6d8e55a..daa3049 100644--- a/backend/services/instagramApi.ts+++ b/backend/services/instagram/instagramApi.ts@@ -1,4 +1,4 @@-import type { InstagramContainerStatusCode, InstagramProfile } from '../../shared/types/instagram.ts';+import type { InstagramContainerStatusCode, InstagramProfile } from '../../../shared/types/instagram.ts';const IG_GRAPH_URL = 'https://graph.instagram.com';const IG_API_VERSION = 'v25.0';diff --git a/backend/services/instagramCaptions.ts b/backend/services/instagram/instagramCaptions.tssimilarity index 95%rename from backend/services/instagramCaptions.tsrename to backend/services/instagram/instagramCaptions.tsindex a817fe6..838e10e 100644--- a/backend/services/instagramCaptions.ts+++ b/backend/services/instagram/instagramCaptions.ts@@ -1,7 +1,7 @@-import { sumOf, roundedAverage } from './analyticsMath.ts';+import { sumOf, roundedAverage } from '../../lib/analyticsMath.ts';import type {InstagramCaptionLengthStats, InstagramHashtagStats, InstagramMedia,-} from '../../shared/types/instagram.ts';+} from '../../../shared/types/instagram.ts';interface CaptionBucket {label: string;diff --git a/backend/services/instagramContentTypes.ts b/backend/services/instagram/instagramContentTypes.tssimilarity index 94%rename from backend/services/instagramContentTypes.tsrename to backend/services/instagram/instagramContentTypes.tsindex e0fdea8..750809f 100644--- a/backend/services/instagramContentTypes.ts+++ b/backend/services/instagram/instagramContentTypes.ts@@ -1,7 +1,7 @@-import { roundedAverage } from './analyticsMath.ts';+import { roundedAverage } from '../../lib/analyticsMath.ts';import type {InstagramContentType, InstagramContentTypeStatsMap, InstagramContentTypeSummary, InstagramMedia,-} from '../../shared/types/instagram.ts';+} from '../../../shared/types/instagram.ts';export const CONTENT_TYPES: InstagramContentType[] = ['reel', 'post', 'video'];diff --git a/backend/services/instagramMedia.ts b/backend/services/instagram/instagramMedia.tssimilarity index 99%rename from backend/services/instagramMedia.tsrename to backend/services/instagram/instagramMedia.tsindex 08935a7..e01e2d5 100644--- a/backend/services/instagramMedia.ts+++ b/backend/services/instagram/instagramMedia.ts@@ -1,6 +1,6 @@import { igFetch } from './instagramApi.ts';import type { GraphParams } from './instagramApi.ts';-import type { InstagramContentType, InstagramMedia } from '../../shared/types/instagram.ts';+import type { InstagramContentType, InstagramMedia } from '../../../shared/types/instagram.ts';const MEDIA_FIELDS = 'id,caption,media_type,media_product_type,media_url,thumbnail_url,cover_url,permalink,timestamp,like_count,comments_count';const VIEWS_FIELD = 'insights.metric(views)';diff --git a/backend/services/instagramOAuth.ts b/backend/services/instagram/instagramOAuth.tssimilarity index 94%rename from backend/services/instagramOAuth.tsrename to backend/services/instagram/instagramOAuth.tsindex fe899b8..75fc6a6 100644--- a/backend/services/instagramOAuth.ts+++ b/backend/services/instagram/instagramOAuth.ts@@ -1,10 +1,10 @@-import { createOAuthState } from '../lib/oauthState.ts';-import { createAccount, getAccounts } from '../lib/db.ts';+import { createOAuthState } from '../../lib/oauthState.ts';+import { createAccount, getAccounts } from '../../lib/db.ts';import { fetchInstagramProfile } from './instagramApi.ts';-import { buildInstagramAccount } from './accountFactory.ts';+import { buildInstagramAccount } from '../accountFactory.ts';import type { InstagramApiToken } from './instagramTokens.ts';-import type { Env } from '../types/env.ts';-import type { SupabaseClient } from '../types/app.ts';+import type { Env } from '../../types/env.ts';+import type { SupabaseClient } from '../../types/app.ts';const DEFAULT_BACKEND_URL = 'http://localhost:8787';const DEFAULT_FRONTEND_URL = 'http://localhost:5173';diff --git a/backend/services/instagramPublishing.ts b/backend/services/instagram/instagramPublishing.tssimilarity index 97%rename from backend/services/instagramPublishing.tsrename to backend/services/instagram/instagramPublishing.tsindex 033425f..07b2671 100644--- a/backend/services/instagramPublishing.ts+++ b/backend/services/instagram/instagramPublishing.ts@@ -1,4 +1,4 @@-import type { InstagramPublishRequest } from '../../shared/types/instagram.ts';+import type { InstagramPublishRequest } from '../../../shared/types/instagram.ts';// query parameters of POST /{ig-user-id}/media; URLSearchParams turns the booleans and numbers into stringsexport interface InstagramMediaParams {diff --git a/backend/services/instagramTiming.ts b/backend/services/instagram/instagramTiming.tssimilarity index 97%rename from backend/services/instagramTiming.tsrename to backend/services/instagram/instagramTiming.tsindex 5924413..77dbed4 100644--- a/backend/services/instagramTiming.ts+++ b/backend/services/instagram/instagramTiming.ts@@ -1,7 +1,7 @@-import { DAY_NAMES, roundedAverage, pickHighest, monthKey } from './analyticsMath.ts';+import { DAY_NAMES, roundedAverage, pickHighest, monthKey } from '../../lib/analyticsMath.ts';import type {InstagramDayStats, InstagramHourStats, InstagramMedia, InstagramMonthStats, InstagramTiming,-} from '../../shared/types/instagram.ts';+} from '../../../shared/types/instagram.ts';interface EngagementBucket {count: number;diff --git a/backend/services/instagramTokens.ts b/backend/services/instagram/instagramTokens.tssimilarity index 94%rename from backend/services/instagramTokens.tsrename to backend/services/instagram/instagramTokens.tsindex 07912b1..2fdc85e 100644--- a/backend/services/instagramTokens.ts+++ b/backend/services/instagram/instagramTokens.ts@@ -1,7 +1,7 @@-import { getAccounts, updateAccount } from '../lib/db.ts';+import { getAccounts, updateAccount } from '../../lib/db.ts';import { igFetch } from './instagramApi.ts';-import type { Account } from '../../shared/types/domain.ts';-import type { SupabaseClient } from '../types/app.ts';+import type { Account } from '../../../shared/types/domain.ts';+import type { SupabaseClient } from '../../types/app.ts';const DEFAULT_TOKEN_LIFETIME_S = 5184000;const REFRESH_WINDOW_MS = 15 * 24 * 60 * 60 * 1000;diff --git a/backend/services/overviewSummary.ts b/backend/services/overviewSummary.tsindex fb98eb0..46adab2 100644--- a/backend/services/overviewSummary.ts+++ b/backend/services/overviewSummary.ts@@ -1,7 +1,8 @@import { getVideosCache, setVideosCache, getIGCache, setIGCache, getRedditCache, setRedditCache } from '../lib/cache.ts';-import { fetchAllVideos } from './youtube.ts';-import { fetchInstagramMedia, summarizeContentTypes } from './instagram.ts';-import { fetchRedditProfile, fetchRedditPosts, computeRedditAnalytics } from './reddit.ts';+import { fetchAllVideos } from './youtube/youtubeVideos.ts';+import { fetchInstagramMedia } from './instagram/instagramMedia.ts';+import { summarizeContentTypes } from './instagram/instagramContentTypes.ts';+import { fetchRedditProfile, fetchRedditPosts, computeRedditAnalytics } from './reddit/redditApi.ts';import type { Redis } from '../types/app.ts';import type {Account, AccountSummary, PendingAccountSummary, RedditAccountSummary, InstagramAccountSummary,diff --git a/backend/services/redditAnalytics.ts b/backend/services/reddit/redditAnalytics.tssimilarity index 98%rename from backend/services/redditAnalytics.tsrename to backend/services/reddit/redditAnalytics.tsindex cb95069..c4d7905 100644--- a/backend/services/redditAnalytics.ts+++ b/backend/services/reddit/redditAnalytics.ts@@ -1,10 +1,10 @@import {DAY_NAMES, sumOf, roundedAverage, medianOf, countWithinDays, postFrequency,consistencyScore, bestToAverageRatio, pickHighest, monthKey,-} from './analyticsMath.ts';+} from '../../lib/analyticsMath.ts';import type {RedditAnalytics, RedditPost, RedditProfile, RedditSubredditStats, RedditMediaType, RedditMonthStats,-} from '../../shared/types/reddit.ts';+} from '../../../shared/types/reddit.ts';const MAX_SUBREDDITS = 20;diff --git a/backend/services/reddit.ts b/backend/services/reddit/redditApi.tssimilarity index 99%rename from backend/services/reddit.tsrename to backend/services/reddit/redditApi.tsindex 8454cdc..38bb159 100644--- a/backend/services/reddit.ts+++ b/backend/services/reddit/redditApi.ts@@ -1,4 +1,4 @@-import type { RedditMediaType, RedditPost, RedditProfile, RedditTrackedPostData } from '../../shared/types/reddit.ts';+import type { RedditMediaType, RedditPost, RedditProfile, RedditTrackedPostData } from '../../../shared/types/reddit.ts';export { computeRedditAnalytics } from './redditAnalytics.ts';diff --git a/backend/services/youtube.ts b/backend/services/youtube.tsdeleted file mode 100644index a33c2b9..0000000--- a/backend/services/youtube.ts+++ /dev/null@@ -1,4 +0,0 @@-export { resolveChannelId, fetchChannelData } from './youtubeChannels.ts';-export { fetchAllVideos } from './youtubeVideos.ts';-export { fetchTrackedYouTubeData } from './youtubeTracked.ts';-export { computeVideoAnalytics } from './youtubeAnalytics.ts';diff --git a/backend/services/youtubeAnalytics.ts b/backend/services/youtube/youtubeAnalytics.tssimilarity index 99%rename from backend/services/youtubeAnalytics.tsrename to backend/services/youtube/youtubeAnalytics.tsindex 3a7acda..ed3a133 100644--- a/backend/services/youtubeAnalytics.ts+++ b/backend/services/youtube/youtubeAnalytics.ts@@ -1,10 +1,10 @@-import { DAY_MS, sumOf, roundedAverage, medianOf, reportedCount } from './analyticsMath.ts';+import { DAY_MS, sumOf, roundedAverage, medianOf, reportedCount } from '../../lib/analyticsMath.ts';import { parseDuration, formatDuration, formatVideoSummary } from './youtubeFormat.ts';import {buildPublishTiming, buildCategoryDistribution, buildTagFrequency, countRecentUploads,buildUploadFrequency, weeklyConsistencyScore, buildViewsDistribution, buildMonthlyTrends, engagementRateOf,} from './youtubeTrends.ts';-import type { VideoAnalytics, YouTubeVideo, YouTubeDurationDistribution } from '../../shared/types/youtube.ts';+import type { VideoAnalytics, YouTubeVideo, YouTubeDurationDistribution } from '../../../shared/types/youtube.ts';// a stored video with the per-video rates the analytics are built fromexport interface EnrichedVideo extends YouTubeVideo {diff --git a/backend/services/youtubeApi.ts b/backend/services/youtube/youtubeApi.tssimilarity index 100%rename from backend/services/youtubeApi.tsrename to backend/services/youtube/youtubeApi.tsdiff --git a/backend/services/youtubeChannels.ts b/backend/services/youtube/youtubeChannels.tssimilarity index 98%rename from backend/services/youtubeChannels.tsrename to backend/services/youtube/youtubeChannels.tsindex 056c468..670bd28 100644--- a/backend/services/youtubeChannels.ts+++ b/backend/services/youtube/youtubeChannels.ts@@ -1,6 +1,6 @@import { ytFetch } from './youtubeApi.ts';import type { YouTubeApiParams, YouTubeApiListResponse, YouTubeApiChannel, YouTubeApiSearchResult } from './youtubeApi.ts';-import type { YouTubeChannelData } from '../../shared/types/youtube.ts';+import type { YouTubeChannelData } from '../../../shared/types/youtube.ts';const CHANNEL_ID_PATTERN = /^UC[\w-]{22}$/;const CHANNEL_URL_PATTERNS = [diff --git a/backend/services/youtubeFormat.ts b/backend/services/youtube/youtubeFormat.tssimilarity index 94%rename from backend/services/youtubeFormat.tsrename to backend/services/youtube/youtubeFormat.tsindex 2a0cb75..2484116 100644--- a/backend/services/youtubeFormat.ts+++ b/backend/services/youtube/youtubeFormat.ts@@ -1,4 +1,4 @@-import type { YouTubeVideo, YouTubeVideoSummary } from '../../shared/types/youtube.ts';+import type { YouTubeVideo, YouTubeVideoSummary } from '../../../shared/types/youtube.ts';// a stored video, optionally carrying the rates computeVideoAnalytics() addsexport type VideoSummarySource = YouTubeVideo & { engagementRate?: number; durationSeconds?: number };diff --git a/backend/services/youtubeLibrary.ts b/backend/services/youtube/youtubeLibrary.tssimilarity index 95%rename from backend/services/youtubeLibrary.tsrename to backend/services/youtube/youtubeLibrary.tsindex b6bc185..cf65c13 100644--- a/backend/services/youtubeLibrary.ts+++ b/backend/services/youtube/youtubeLibrary.ts@@ -1,6 +1,6 @@-import { DAY_MS } from './analyticsMath.ts';-import type { Account } from '../../shared/types/domain.ts';-import type { YouTubeAccountComparison, YouTubeChannelMetrics, YouTubeVideo } from '../../shared/types/youtube.ts';+import { DAY_MS } from '../../lib/analyticsMath.ts';+import type { Account } from '../../../shared/types/domain.ts';+import type { YouTubeAccountComparison, YouTubeChannelMetrics, YouTubeVideo } from '../../../shared/types/youtube.ts';// query string values from GET /api/videos/allexport interface VideoLibraryFilters {diff --git a/backend/services/youtubeTracked.ts b/backend/services/youtube/youtubeTracked.tssimilarity index 96%rename from backend/services/youtubeTracked.tsrename to backend/services/youtube/youtubeTracked.tsindex c002991..971aa8e 100644--- a/backend/services/youtubeTracked.ts+++ b/backend/services/youtube/youtubeTracked.ts@@ -1,6 +1,6 @@import { ytFetch, optionalCount } from './youtubeApi.ts';import type { YouTubeApiListResponse, YouTubeApiVideo } from './youtubeApi.ts';-import type { YouTubeTrackedVideoData } from '../../shared/types/youtube.ts';+import type { YouTubeTrackedVideoData } from '../../../shared/types/youtube.ts';// requested with part=snippet,statistics onlytype TrackedApiVideo = Pick<YouTubeApiVideo, 'id' | 'snippet' | 'statistics'>;diff --git a/backend/services/youtubeTrends.ts b/backend/services/youtube/youtubeTrends.tssimilarity index 98%rename from backend/services/youtubeTrends.tsrename to backend/services/youtube/youtubeTrends.tsindex b52c3b2..5035153 100644--- a/backend/services/youtubeTrends.ts+++ b/backend/services/youtube/youtubeTrends.ts@@ -1,8 +1,8 @@-import { DAY_NAMES, DAY_MS, monthKey, reportedCount, percentOfReportedViews } from './analyticsMath.ts';+import { DAY_NAMES, DAY_MS, monthKey, reportedCount, percentOfReportedViews } from '../../lib/analyticsMath.ts';import { formatCompact } from './youtubeFormat.ts';import type {VideoAnalytics, YouTubeVideo, YouTubeTagCount, YouTubeViewsDistribution, YouTubeMonthlyTrends, FormattedRate,-} from '../../shared/types/youtube.ts';+} from '../../../shared/types/youtube.ts';export type YouTubePublishTiming = Pick<VideoAnalytics, 'publishDayDistribution' | 'publishHourDistribution' | 'peakPublishDay' | 'peakPublishHour'>;export type YouTubeRecentUploads = Pick<VideoAnalytics, 'videosLast7Days' | 'videosLast30Days' | 'videosLast90Days' | 'videosLast365Days'>;diff --git a/backend/services/youtubeVideos.ts b/backend/services/youtube/youtubeVideos.tssimilarity index 98%rename from backend/services/youtubeVideos.tsrename to backend/services/youtube/youtubeVideos.tsindex e3edc30..d862e38 100644--- a/backend/services/youtubeVideos.ts+++ b/backend/services/youtube/youtubeVideos.ts@@ -1,6 +1,6 @@import { ytFetch, optionalCount } from './youtubeApi.ts';import type { YouTubeApiError, YouTubeApiListResponse, YouTubeApiParams, YouTubeApiPlaylistItem, YouTubeApiVideo } from './youtubeApi.ts';-import type { YouTubeVideo } from '../../shared/types/youtube.ts';+import type { YouTubeVideo } from '../../../shared/types/youtube.ts';export function normalizeYouTubeVideo(video: YouTubeApiVideo): YouTubeVideo {const { snippet, contentDetails, statistics, status } = video;diff --git a/backend/tests/services/analyticsMath.test.ts b/backend/tests/lib/analyticsMath.test.tssimilarity index 98%rename from backend/tests/services/analyticsMath.test.tsrename to backend/tests/lib/analyticsMath.test.tsindex 1e67254..dfce03f 100644--- a/backend/tests/services/analyticsMath.test.ts+++ b/backend/tests/lib/analyticsMath.test.ts@@ -5,7 +5,7 @@ import assert from 'node:assert/strict';import {DAY_NAMES, DAY_MS, sumOf, roundedAverage, medianOf, countWithinDays, postFrequency,consistencyScore, bestToAverageRatio, pickHighest, monthKey,-} from '../../services/analyticsMath.ts';+} from '../../lib/analyticsMath.ts';const NOW = Date.parse('2026-09-01T12:00:00Z');const daysAgo = (days: number) => new Date(NOW - days * DAY_MS);diff --git a/backend/tests/lib/safeAccount.test.ts b/backend/tests/lib/safeAccount.test.tsindex c88c293..7390bb0 100644--- a/backend/tests/lib/safeAccount.test.ts+++ b/backend/tests/lib/safeAccount.test.ts@@ -1,7 +1,7 @@import { test } from 'node:test';import assert from 'node:assert/strict';import { safeAccount } from '../../lib/safeAccount.ts';-import { buildMediaParams } from '../../services/instagram.ts';+import { buildMediaParams } from '../../services/instagram/instagramPublishing.ts';import type { Account } from '../../../shared/types/domain.ts';test('safeAccount strips every credential and reports cookie presence', () => {diff --git a/backend/tests/services/instagram.test.ts b/backend/tests/services/instagram/instagram.test.tssimilarity index 93%rename from backend/tests/services/instagram.test.tsrename to backend/tests/services/instagram/instagram.test.tsindex 4489411..e9f80ae 100644--- a/backend/tests/services/instagram.test.ts+++ b/backend/tests/services/instagram/instagram.test.ts@@ -1,16 +1,11 @@import { test, describe, afterEach } from 'node:test';import assert from 'node:assert/strict';-import {- classifyInstagramMedia,- extractInstagramViews,- normalizeInstagramMedia,- fetchInstagramMedia,- summarizeContentTypes,- computeInstagramAnalytics,-} from '../../services/instagram.ts';-import type { InstagramApiMedia } from '../../services/instagram.ts';-import type { AnalyticsProfile } from '../../services/instagramAnalytics.ts';-import type { InstagramMedia } from '../../../shared/types/instagram.ts';+import { classifyInstagramMedia, extractInstagramViews, normalizeInstagramMedia, fetchInstagramMedia } from '../../../services/instagram/instagramMedia.ts';+import { summarizeContentTypes } from '../../../services/instagram/instagramContentTypes.ts';+import { computeInstagramAnalytics } from '../../../services/instagram/instagramAnalytics.ts';+import type { InstagramApiMedia } from '../../../services/instagram/instagramMedia.ts';+import type { AnalyticsProfile } from '../../../services/instagram/instagramAnalytics.ts';+import type { InstagramMedia } from '../../../../shared/types/instagram.ts';const viewsInsight = (value: unknown) => ({ data: [{ name: 'views', period: 'lifetime', values: [{ value }] }] }) as unknown as InstagramApiMedia['insights'];diff --git a/backend/tests/services/instagramAnalytics.test.ts b/backend/tests/services/instagram/instagramAnalytics.test.tssimilarity index 86%rename from backend/tests/services/instagramAnalytics.test.tsrename to backend/tests/services/instagram/instagramAnalytics.test.tsindex 033157b..22cbdc2 100644--- a/backend/tests/services/instagramAnalytics.test.ts+++ b/backend/tests/services/instagram/instagramAnalytics.test.ts@@ -1,10 +1,10 @@process.env.TZ = 'UTC';import { test, describe, before, after, mock } from 'node:test';-import { computeInstagramAnalytics } from '../../services/instagramAnalytics.ts';-import type { AnalyticsProfile } from '../../services/instagramAnalytics.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { FIXED_NOW, richMedia, zeroEngagementMedia, generatedMedia } from '../fixtures/instagramFixtures.ts';+import { computeInstagramAnalytics } from '../../../services/instagram/instagramAnalytics.ts';+import type { AnalyticsProfile } from '../../../services/instagram/instagramAnalytics.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { FIXED_NOW, richMedia, zeroEngagementMedia, generatedMedia } from '../../fixtures/instagramFixtures.ts';describe('computeInstagramAnalytics golden output', () => {before(() => mock.timers.enable({ apis: ['Date'], now: FIXED_NOW }));diff --git a/backend/tests/services/instagramApi.test.ts b/backend/tests/services/instagram/instagramApi.test.tssimilarity index 91%rename from backend/tests/services/instagramApi.test.tsrename to backend/tests/services/instagram/instagramApi.test.tsindex a6f2512..1cd46ff 100644--- a/backend/tests/services/instagramApi.test.ts+++ b/backend/tests/services/instagram/instagramApi.test.ts@@ -1,8 +1,8 @@import { test, describe, afterEach } from 'node:test';-import { fetchInstagramProfile } from '../../services/instagramApi.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { installFetch, reply } from '../fixtures/fakeFetch.ts';-import type { FetchMock } from '../fixtures/fakeFetch.ts';+import { fetchInstagramProfile } from '../../../services/instagram/instagramApi.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { installFetch, reply } from '../../fixtures/fakeFetch.ts';+import type { FetchMock } from '../../fixtures/fakeFetch.ts';let fetchMock: FetchMock | null = null;afterEach(() => fetchMock?.restore());diff --git a/backend/tests/services/instagramContentTypes.test.ts b/backend/tests/services/instagram/instagramContentTypes.test.tssimilarity index 71%rename from backend/tests/services/instagramContentTypes.test.tsrename to backend/tests/services/instagram/instagramContentTypes.test.tsindex 64b3a2e..cc27478 100644--- a/backend/tests/services/instagramContentTypes.test.ts+++ b/backend/tests/services/instagram/instagramContentTypes.test.ts@@ -1,8 +1,8 @@import { test, describe } from 'node:test';-import { CONTENT_TYPES, summarizeContentTypes } from '../../services/instagramContentTypes.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { richMedia, generatedMedia } from '../fixtures/instagramFixtures.ts';-import type { InstagramMedia } from '../../../shared/types/instagram.ts';+import { CONTENT_TYPES, summarizeContentTypes } from '../../../services/instagram/instagramContentTypes.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { richMedia, generatedMedia } from '../../fixtures/instagramFixtures.ts';+import type { InstagramMedia } from '../../../../shared/types/instagram.ts';describe('summarizeContentTypes golden output', () => {test('summarizes media of every type, unknown types and missing counts', () => {diff --git a/backend/tests/services/instagramMedia.test.ts b/backend/tests/services/instagram/instagramMedia.test.tssimilarity index 89%rename from backend/tests/services/instagramMedia.test.tsrename to backend/tests/services/instagram/instagramMedia.test.tsindex ed4a037..de1480e 100644--- a/backend/tests/services/instagramMedia.test.ts+++ b/backend/tests/services/instagram/instagramMedia.test.ts@@ -1,9 +1,9 @@import { test, describe, afterEach } from 'node:test';-import { classifyInstagramMedia, extractInstagramViews, normalizeInstagramMedia, fetchInstagramMedia } from '../../services/instagramMedia.ts';-import type { InstagramApiMedia } from '../../services/instagramMedia.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { installFetch, reply } from '../fixtures/fakeFetch.ts';-import type { FetchMock } from '../fixtures/fakeFetch.ts';+import { classifyInstagramMedia, extractInstagramViews, normalizeInstagramMedia, fetchInstagramMedia } from '../../../services/instagram/instagramMedia.ts';+import type { InstagramApiMedia } from '../../../services/instagram/instagramMedia.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { installFetch, reply } from '../../fixtures/fakeFetch.ts';+import type { FetchMock } from '../../fixtures/fakeFetch.ts';let fetchMock: FetchMock | null = null;afterEach(() => fetchMock?.restore());diff --git a/backend/tests/services/instagramPublishing.test.ts b/backend/tests/services/instagram/instagramPublishing.test.tssimilarity index 88%rename from backend/tests/services/instagramPublishing.test.tsrename to backend/tests/services/instagram/instagramPublishing.test.tsindex 399a7fc..8aa930d 100644--- a/backend/tests/services/instagramPublishing.test.ts+++ b/backend/tests/services/instagram/instagramPublishing.test.ts@@ -1,7 +1,7 @@import { test, describe } from 'node:test';-import { buildMediaParams } from '../../services/instagramPublishing.ts';-import { assertGolden } from '../fixtures/golden.ts';-import type { InstagramPublishMediaType } from '../../../shared/types/instagram.ts';+import { buildMediaParams } from '../../../services/instagram/instagramPublishing.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import type { InstagramPublishMediaType } from '../../../../shared/types/instagram.ts';const fullPost = {mediaUrl: 'https://cdn.example/file.jpg',diff --git a/backend/tests/services/instagramTokens.test.ts b/backend/tests/services/instagram/instagramTokens.test.tssimilarity index 90%rename from backend/tests/services/instagramTokens.test.tsrename to backend/tests/services/instagram/instagramTokens.test.tsindex 1e8ec4b..4c40de1 100644--- a/backend/tests/services/instagramTokens.test.ts+++ b/backend/tests/services/instagram/instagramTokens.test.ts@@ -1,11 +1,11 @@import { test, describe, before, after, afterEach, mock } from 'node:test';-import { exchangeForLongLivedToken, refreshLongLivedToken, autoRefreshInstagramTokens } from '../../services/instagramTokens.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { installFetch, reply } from '../fixtures/fakeFetch.ts';-import type { FetchMock } from '../fixtures/fakeFetch.ts';-import { fakeSupabase, accountRow } from '../fixtures/fakeSupabase.ts';-import { FIXED_NOW } from '../fixtures/instagramFixtures.ts';-import type { SupabaseClient } from '../../types/app.ts';+import { exchangeForLongLivedToken, refreshLongLivedToken, autoRefreshInstagramTokens } from '../../../services/instagram/instagramTokens.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { installFetch, reply } from '../../fixtures/fakeFetch.ts';+import type { FetchMock } from '../../fixtures/fakeFetch.ts';+import { fakeSupabase, accountRow } from '../../fixtures/fakeSupabase.ts';+import { FIXED_NOW } from '../../fixtures/instagramFixtures.ts';+import type { SupabaseClient } from '../../../types/app.ts';const DAY_MS = 86400000;let fetchMock: FetchMock | null = null;diff --git a/backend/tests/services/redditAnalytics.test.ts b/backend/tests/services/reddit/redditAnalytics.test.tssimilarity index 84%rename from backend/tests/services/redditAnalytics.test.tsrename to backend/tests/services/reddit/redditAnalytics.test.tsindex c673e05..68fd180 100644--- a/backend/tests/services/redditAnalytics.test.ts+++ b/backend/tests/services/reddit/redditAnalytics.test.ts@@ -1,10 +1,10 @@process.env.TZ = 'UTC';import { test, describe, before, after, mock } from 'node:test';-import { computeRedditAnalytics } from '../../services/redditAnalytics.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { FIXED_NOW, richPosts, zeroScorePosts, generatedPosts } from '../fixtures/redditFixtures.ts';-import type { RedditProfile } from '../../../shared/types/reddit.ts';+import { computeRedditAnalytics } from '../../../services/reddit/redditAnalytics.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { FIXED_NOW, richPosts, zeroScorePosts, generatedPosts } from '../../fixtures/redditFixtures.ts';+import type { RedditProfile } from '../../../../shared/types/reddit.ts';const profile = { username: 'ribbon', totalKarma: 900 } as RedditProfile;diff --git a/backend/tests/services/reddit.test.ts b/backend/tests/services/reddit/redditApi.test.tssimilarity index 92%rename from backend/tests/services/reddit.test.tsrename to backend/tests/services/reddit/redditApi.test.tsindex 42ba0f6..3e36240 100644--- a/backend/tests/services/reddit.test.ts+++ b/backend/tests/services/reddit/redditApi.test.ts@@ -1,9 +1,9 @@import { test, describe, before, after, afterEach, mock } from 'node:test';-import { fetchRedditProfile, fetchRedditPosts, fetchTrackedPostData } from '../../services/reddit.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { installFetch, reply } from '../fixtures/fakeFetch.ts';-import type { FetchMock } from '../fixtures/fakeFetch.ts';-import { FIXED_NOW, rawRedditPosts } from '../fixtures/redditFixtures.ts';+import { fetchRedditProfile, fetchRedditPosts, fetchTrackedPostData } from '../../../services/reddit/redditApi.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { installFetch, reply } from '../../fixtures/fakeFetch.ts';+import type { FetchMock } from '../../fixtures/fakeFetch.ts';+import { FIXED_NOW, rawRedditPosts } from '../../fixtures/redditFixtures.ts';let fetchMock: FetchMock | null = null;afterEach(() => fetchMock?.restore());diff --git a/backend/tests/services/youtube.test.ts b/backend/tests/services/youtube/youtube.test.tssimilarity index 95%rename from backend/tests/services/youtube.test.tsrename to backend/tests/services/youtube/youtube.test.tsindex 8018233..77f3172 100644--- a/backend/tests/services/youtube.test.ts+++ b/backend/tests/services/youtube/youtube.test.ts@@ -1,6 +1,7 @@import { test, afterEach } from 'node:test';import assert from 'node:assert/strict';-import { resolveChannelId, fetchTrackedYouTubeData } from '../../services/youtube.ts';+import { resolveChannelId } from '../../../services/youtube/youtubeChannels.ts';+import { fetchTrackedYouTubeData } from '../../../services/youtube/youtubeTracked.ts';const realFetch = globalThis.fetch;afterEach(() => { globalThis.fetch = realFetch; });diff --git a/backend/tests/services/youtubeAnalytics.test.ts b/backend/tests/services/youtube/youtubeAnalytics.test.tssimilarity index 86%rename from backend/tests/services/youtubeAnalytics.test.tsrename to backend/tests/services/youtube/youtubeAnalytics.test.tsindex 6307571..bb9e84b 100644--- a/backend/tests/services/youtubeAnalytics.test.ts+++ b/backend/tests/services/youtube/youtubeAnalytics.test.ts@@ -1,9 +1,9 @@process.env.TZ = 'UTC';import { test, describe, before, after, mock } from 'node:test';-import { computeVideoAnalytics } from '../../services/youtubeAnalytics.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { FIXED_NOW, richVideos, zeroViewVideos, generatedVideos } from '../fixtures/youtubeFixtures.ts';+import { computeVideoAnalytics } from '../../../services/youtube/youtubeAnalytics.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { FIXED_NOW, richVideos, zeroViewVideos, generatedVideos } from '../../fixtures/youtubeFixtures.ts';describe('computeVideoAnalytics golden output', () => {before(() => mock.timers.enable({ apis: ['Date'], now: FIXED_NOW }));diff --git a/backend/tests/services/youtubeChannels.test.ts b/backend/tests/services/youtube/youtubeChannels.test.tssimilarity index 89%rename from backend/tests/services/youtubeChannels.test.tsrename to backend/tests/services/youtube/youtubeChannels.test.tsindex ee8377f..9f1d5cf 100644--- a/backend/tests/services/youtubeChannels.test.ts+++ b/backend/tests/services/youtube/youtubeChannels.test.ts@@ -1,9 +1,9 @@import { test, describe, afterEach } from 'node:test';-import { resolveChannelId, fetchChannelData } from '../../services/youtubeChannels.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { installFetch, reply } from '../fixtures/fakeFetch.ts';-import type { FetchMock } from '../fixtures/fakeFetch.ts';-import type { YouTubeApiError } from '../../services/youtubeApi.ts';+import { resolveChannelId, fetchChannelData } from '../../../services/youtube/youtubeChannels.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { installFetch, reply } from '../../fixtures/fakeFetch.ts';+import type { FetchMock } from '../../fixtures/fakeFetch.ts';+import type { YouTubeApiError } from '../../../services/youtube/youtubeApi.ts';let fetchMock: FetchMock | null = null;afterEach(() => fetchMock?.restore());diff --git a/backend/tests/services/youtubeTracked.test.ts b/backend/tests/services/youtube/youtubeTracked.test.tssimilarity index 85%rename from backend/tests/services/youtubeTracked.test.tsrename to backend/tests/services/youtube/youtubeTracked.test.tsindex a956fa2..3cab598 100644--- a/backend/tests/services/youtubeTracked.test.ts+++ b/backend/tests/services/youtube/youtubeTracked.test.ts@@ -1,9 +1,9 @@import { test, describe, before, after, afterEach, mock } from 'node:test';-import { fetchTrackedYouTubeData } from '../../services/youtubeTracked.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { installFetch, reply } from '../fixtures/fakeFetch.ts';-import type { FetchMock } from '../fixtures/fakeFetch.ts';-import { FIXED_NOW } from '../fixtures/youtubeFixtures.ts';+import { fetchTrackedYouTubeData } from '../../../services/youtube/youtubeTracked.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { installFetch, reply } from '../../fixtures/fakeFetch.ts';+import type { FetchMock } from '../../fixtures/fakeFetch.ts';+import { FIXED_NOW } from '../../fixtures/youtubeFixtures.ts';let fetchMock: FetchMock | null = null;afterEach(() => fetchMock?.restore());diff --git a/backend/tests/services/youtubeVideos.test.ts b/backend/tests/services/youtube/youtubeVideos.test.tssimilarity index 88%rename from backend/tests/services/youtubeVideos.test.tsrename to backend/tests/services/youtube/youtubeVideos.test.tsindex 751265a..7e72bd8 100644--- a/backend/tests/services/youtubeVideos.test.ts+++ b/backend/tests/services/youtube/youtubeVideos.test.ts@@ -1,10 +1,10 @@import { test, describe, afterEach } from 'node:test';-import { fetchAllVideos } from '../../services/youtubeVideos.ts';-import { assertGolden } from '../fixtures/golden.ts';-import { installFetch, reply } from '../fixtures/fakeFetch.ts';-import type { FetchMock, FakeReply } from '../fixtures/fakeFetch.ts';-import { rawPlaylistItem, rawVideos } from '../fixtures/youtubeFixtures.ts';-import type { YouTubeApiError } from '../../services/youtubeApi.ts';+import { fetchAllVideos } from '../../../services/youtube/youtubeVideos.ts';+import { assertGolden } from '../../fixtures/golden.ts';+import { installFetch, reply } from '../../fixtures/fakeFetch.ts';+import type { FetchMock, FakeReply } from '../../fixtures/fakeFetch.ts';+import { rawPlaylistItem, rawVideos } from '../../fixtures/youtubeFixtures.ts';+import type { YouTubeApiError } from '../../../services/youtube/youtubeApi.ts';let fetchMock: FetchMock | null = null;afterEach(() => fetchMock?.restore());diff --git a/backend/utils/scheduler.ts b/backend/utils/scheduler.tsindex 3f615b4..9ab6105 100644--- a/backend/utils/scheduler.ts+++ b/backend/utils/scheduler.ts@@ -1,8 +1,8 @@// fetch is a global in Cloudflare Workers.import { getDuePosts, updatePost, getAccountById, claimPost } from '../lib/db.ts';-import { buildMediaParams } from '../services/instagram.ts';-import type { InstagramMediaParams } from '../services/instagramPublishing.ts';-import type { InstagramApiContainerStatus, InstagramApiIdResponse } from '../services/instagramApi.ts';+import { buildMediaParams } from '../services/instagram/instagramPublishing.ts';+import type { InstagramMediaParams } from '../services/instagram/instagramPublishing.ts';+import type { InstagramApiContainerStatus, InstagramApiIdResponse } from '../services/instagram/instagramApi.ts';import type { ScheduledPost, ScheduledPublishFailure, ScheduledPublishRun } from '../../shared/types/domain.ts';import type { InstagramPublishMediaType } from '../../shared/types/instagram.ts';import type { SupabaseClient } from '../types/app.ts';