Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
fix(youtube): show hidden likes as hidden, and expire cached stats
spacesdrived7104c87a3a7
diff --git a/backend/lib/cache.js b/backend/lib/cache.jsindex ce657d6..d61ba04 100644--- a/backend/lib/cache.js+++ b/backend/lib/cache.js@@ -2,14 +2,24 @@// If redis is null (Upstash not configured) or the call fails, all operations// are silent no-ops - cache failures must never break the main request.+// Platform stats change constantly, so cached analytics expire and refetch rather than going stale.+const ANALYTICS_TTL_SECONDS = 6 * 60 * 60;++// Bump when the cached video shape changes so entries written by older code read as a miss.+// v2: likeCount and commentCount are null when YouTube hides them, instead of 0.+const VIDEOS_CACHE_VERSION = 2;+export async function getVideosCache(redis, userId, accountId) {if (!redis) return null;- try { return (await redis.get(`videos:${userId}:${accountId}`)) || null; } catch { return null; }+ try {+ const cached = await redis.get(`videos:${userId}:${accountId}`);+ return cached?.version === VIDEOS_CACHE_VERSION ? cached : null;+ } catch { return null; }}export async function setVideosCache(redis, userId, accountId, data) {if (!redis) return;- try { await redis.set(`videos:${userId}:${accountId}`, data); } catch { /* non-fatal */ }+ try { await redis.set(`videos:${userId}:${accountId}`, { ...data, version: VIDEOS_CACHE_VERSION }, { ex: ANALYTICS_TTL_SECONDS }); } catch { /* non-fatal */ }}export async function deleteVideosCache(redis, userId, accountId) {@@ -30,7 +40,7 @@ export async function getIGCache(redis, userId, accountId) {export async function setIGCache(redis, userId, accountId, data) {if (!redis) return;- try { await redis.set(`ig:${userId}:${accountId}`, { ...data, version: IG_CACHE_VERSION }); } catch { /* non-fatal */ }+ try { await redis.set(`ig:${userId}:${accountId}`, { ...data, version: IG_CACHE_VERSION }, { ex: ANALYTICS_TTL_SECONDS }); } catch { /* non-fatal */ }}export async function deleteIGCache(redis, userId, accountId) {@@ -45,7 +55,7 @@ export async function getRedditCache(redis, userId, accountId) {export async function setRedditCache(redis, userId, accountId, data) {if (!redis) return;- try { await redis.set(`reddit:${userId}:${accountId}`, data); } catch { /* non-fatal */ }+ try { await redis.set(`reddit:${userId}:${accountId}`, data, { ex: ANALYTICS_TTL_SECONDS }); } catch { /* non-fatal */ }}export async function deleteRedditCache(redis, userId, accountId) {diff --git a/backend/services/youtube.js b/backend/services/youtube.jsindex 68ad8fc..1c26bec 100644--- a/backend/services/youtube.js+++ b/backend/services/youtube.js@@ -15,6 +15,12 @@ async function ytFetch(endpoint, params, apiKey) {return data;}+function optionalCount(value) {+ if (value == null) return null;+ const n = parseInt(value, 10);+ return Number.isFinite(n) ? n : null;+}+function extractChannelId(input) {if (!input) return null;input = input.trim();@@ -138,8 +144,9 @@ export async function fetchAllVideos(playlistId, apiKey, maxPages = 10) {licensedContent: v.contentDetails.licensedContent || false,projection: v.contentDetails.projection || 'rectangular',viewCount: parseInt(v.statistics.viewCount) || 0,- likeCount: parseInt(v.statistics.likeCount) || 0,- commentCount: parseInt(v.statistics.commentCount) || 0,+ // YouTube omits a count the owner hid (likes) or switched off (comments); null keeps that apart from zero.+ likeCount: optionalCount(v.statistics.likeCount),+ commentCount: optionalCount(v.statistics.commentCount),privacyStatus: v.status?.privacyStatus || 'public',embeddable: v.status?.embeddable || false,madeForKids: v.status?.madeForKids || false,@@ -203,8 +210,8 @@ export async function fetchTrackedYouTubeData(videoUrl, apiKey) {thumbnail: s.thumbnails?.medium?.url || s.thumbnails?.default?.url || '',publishedAt: s.publishedAt,viewCount: parseInt(st.viewCount || '0', 10),- likeCount: parseInt(st.likeCount || '0', 10),- commentCount: parseInt(st.commentCount || '0', 10),+ likeCount: optionalCount(st.likeCount),+ commentCount: optionalCount(st.commentCount),isShort,permalink: isShort? `https://www.youtube.com/shorts/${videoId}`diff --git a/frontend/src/features/analytics/channel/ChannelAnalytics.jsx b/frontend/src/features/analytics/channel/ChannelAnalytics.jsxindex 08dab5a..01f90ca 100644--- a/frontend/src/features/analytics/channel/ChannelAnalytics.jsx+++ b/frontend/src/features/analytics/channel/ChannelAnalytics.jsx@@ -23,7 +23,7 @@ import { AccountAvatar } from '@/components/platform';import { SERIES, axisProps, axisTick, gridProps, legendStyle, tooltipCursor, tooltipStyle } from '@/lib/chart';import { useAppContext } from '../../../context/AppContext';import { api } from '../../../services/api';-import { fmtNum, fmtNumFull, fmtDate, fmtPercent, fmtDuration, CATEGORY_MAP } from '../../../utils/formatters';+import { fmtNum, fmtNumFull, fmtDate, fmtPercent, fmtDuration, CATEGORY_MAP, fmtCount } from '../../../utils/formatters';import { cn } from '@/lib/utils';const watchUrl = id => `https://www.youtube.com/watch?v=${id}`;@@ -70,8 +70,8 @@ function VideoTable({ videos }) {<a href={watchUrl(v.videoId)} target="_blank" rel="noopener noreferrer" className="block truncate text-sm font-medium text-ink hover:underline">{v.title}</a></TableCell><TableCell className="text-right font-semibold text-ink">{fmtNum(v.viewCount)}</TableCell>- <TableCell className="text-right text-ink-muted">{fmtNum(v.likeCount)}</TableCell>- <TableCell className="text-right text-ink-muted">{fmtNum(v.commentCount)}</TableCell>+ <TableCell className="text-right text-ink-muted">{fmtCount(v.likeCount)}</TableCell>+ <TableCell className="text-right text-ink-muted">{fmtCount(v.commentCount)}</TableCell><TableCell className="text-right"><Badge variant={parseFloat(v.engagementRate) > 5 ? 'success' : 'secondary'}>{fmtPercent(v.engagementRate)}</Badge></TableCell>@@ -283,7 +283,7 @@ export default function ChannelAnalytics() {image={video.thumbnail}title={video.title}topLeft={<MediaChip>{label}</MediaChip>}- meta={<span>{fmtNum(video.viewCount)} views, {fmtNum(video.likeCount)} likes</span>}+ meta={<span>{fmtNum(video.viewCount)} views, {video.likeCount == null ? 'likes hidden' : `${fmtNum(video.likeCount)} likes`}</span>}/>))}</div>diff --git a/frontend/src/features/analytics/shorts/ShortsExplorer.jsx b/frontend/src/features/analytics/shorts/ShortsExplorer.jsxindex d43d1ea..875bc0f 100644--- a/frontend/src/features/analytics/shorts/ShortsExplorer.jsx+++ b/frontend/src/features/analytics/shorts/ShortsExplorer.jsx@@ -13,7 +13,7 @@ import MediaCard, { MediaChip } from '@/components/MediaCard';import { EmptyState } from '@/components/StateViews';import { VIEW_TIERS, filterByTier, matchesText, pageSlice } from '@/lib/library';import { useChannelVideos } from '../videos/useChannelVideos';-import { fmtNum, fmtDuration } from '../../../utils/formatters';+import { fmtNum, fmtDuration, fmtCount } from '../../../utils/formatters';import { cn } from '@/lib/utils';const ITEMS_PER_PAGE = 24;@@ -152,8 +152,8 @@ export default function ShortsExplorer() {bottomRight={<MediaChip>{fmtDuration(v.durationSeconds)}</MediaChip>}meta={<>- <span className="flex items-center gap-1"><ThumbsUp />{fmtNum(v.likeCount)}</span>- <span className="flex items-center gap-1"><MessageSquare />{fmtNum(v.commentCount)}</span>+ <span className="flex items-center gap-1"><ThumbsUp />{fmtCount(v.likeCount)}</span>+ <span className="flex items-center gap-1"><MessageSquare />{fmtCount(v.commentCount)}</span></>}/>diff --git a/frontend/src/features/analytics/videos/VideoExplorer.jsx b/frontend/src/features/analytics/videos/VideoExplorer.jsxindex 4b44c6d..cca88e3 100644--- a/frontend/src/features/analytics/videos/VideoExplorer.jsx+++ b/frontend/src/features/analytics/videos/VideoExplorer.jsx@@ -19,7 +19,7 @@ import MediaCard, { MediaChip } from '@/components/MediaCard';import { EmptyState } from '@/components/StateViews';import { VIEW_TIERS, filterByTier, matchesText, pageSlice } from '@/lib/library';import { useChannelVideos } from './useChannelVideos';-import { fmtNum, fmtDate, fmtDuration, fmtPercent, timeAgo } from '../../../utils/formatters';+import { fmtNum, fmtDate, fmtDuration, fmtPercent, timeAgo, fmtCount } from '../../../utils/formatters';import { cn } from '@/lib/utils';const ITEMS_PER_PAGE = 25;@@ -232,8 +232,8 @@ export default function VideoExplorer() {</TableCell>{channels.length > 1 && <TableCell className="max-w-36 truncate text-small text-ink-muted">{v.channelTitle}</TableCell>}<TableCell className="text-right font-semibold text-ink">{fmtNum(v.viewCount)}</TableCell>- <TableCell className="text-right text-ink-muted">{fmtNum(v.likeCount)}</TableCell>- <TableCell className="text-right text-ink-muted">{fmtNum(v.commentCount)}</TableCell>+ <TableCell className="text-right text-ink-muted">{fmtCount(v.likeCount)}</TableCell>+ <TableCell className="text-right text-ink-muted">{fmtCount(v.commentCount)}</TableCell><TableCell className="text-right"><Badge variant={parseFloat(v.engagementRate) > 5 ? 'success' : 'secondary'}>{fmtPercent(v.engagementRate)}</Badge></TableCell>@@ -264,8 +264,8 @@ export default function VideoExplorer() {bottomRight={v.durationSeconds > 0 && <MediaChip>{fmtDuration(v.durationSeconds)}</MediaChip>}meta={<>- <span className="flex items-center gap-1"><ThumbsUp />{fmtNum(v.likeCount)}</span>- <span className="flex items-center gap-1"><MessageSquare />{fmtNum(v.commentCount)}</span>+ <span className="flex items-center gap-1"><ThumbsUp />{fmtCount(v.likeCount)}</span>+ <span className="flex items-center gap-1"><MessageSquare />{fmtCount(v.commentCount)}</span><span className="ml-auto">{channels.length > 1 ? v.channelTitle : timeAgo(v.publishedAt)}</span></>}diff --git a/frontend/src/utils/formatters.js b/frontend/src/utils/formatters.jsindex 6313d8d..608cd95 100644--- a/frontend/src/utils/formatters.js+++ b/frontend/src/utils/formatters.js@@ -7,6 +7,11 @@ export function fmtNum(n) {return n.toLocaleString();}+// For counts a platform can hide (YouTube likes, disabled comments): null means hidden, not zero.+export function fmtCount(n) {+ return n == null ? 'Hidden' : fmtNum(n);+}+export function fmtNumFull(n) {if (n == null) return '0';return Number(n).toLocaleString();