Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

simplify connection

Steven Tey70298d96a02a
diff --git a/apps/web/app/(ee)/api/cron/online-presence/youtube/route.ts b/apps/web/app/(ee)/api/cron/online-presence/youtube/route.ts
index 37fa17b35e..e6f83596a1 100644
--- a/apps/web/app/(ee)/api/cron/online-presence/youtube/route.ts
+++ b/apps/web/app/(ee)/api/cron/online-presence/youtube/route.ts
@@ -8,7 +8,7 @@ export const dynamic = "force-dynamic";
/*
This route is used to update youtube stats for youtubeVerified partners
- Runs once a day at 12:00 AM UTC
+ Runs once a day at 12:00 AM UTC (cron expression: 0 0 * * *)
*/
export async function GET(req: Request) {
try {
diff --git a/apps/web/app/(ee)/api/partners/online-presence/callback/route.ts b/apps/web/app/(ee)/api/partners/online-presence/callback/route.ts
index 421f8fd53a..7c4800743d 100644
--- a/apps/web/app/(ee)/api/partners/online-presence/callback/route.ts
+++ b/apps/web/app/(ee)/api/partners/online-presence/callback/route.ts
@@ -118,14 +118,8 @@ export async function GET(req: Request) {
[provider]: partner[provider],
[verifiedColumn]: new Date(),
...(provider === "youtube" &&
- metadata && {
+ metadata?.channelId && {
youtubeChannelId: metadata.channelId,
- youtubeSubscriberCount: parseInt(
- metadata.subscriberCount || "0",
- 10,
- ),
- youtubeVideoCount: parseInt(metadata.videoCount || "0", 10),
- youtubeViewCount: parseInt(metadata.viewCount || "0", 10),
}),
},
});
diff --git a/apps/web/lib/actions/partners/online-presence-providers.ts b/apps/web/lib/actions/partners/online-presence-providers.ts
index 7281301304..8ecf1e8ec9 100644
--- a/apps/web/lib/actions/partners/online-presence-providers.ts
+++ b/apps/web/lib/actions/partners/online-presence-providers.ts
@@ -35,11 +35,16 @@ export const ONLINE_PRESENCE_PROVIDERS: Record<string, OnlinePresenceProvider> =
`https://www.googleapis.com/youtube/v3/channels?part=snippet&mine=true&access_token=${accessToken}`,
).then((r) => r.json());
- const channelId = channelResponse?.items?.[0]?.id;
- const handle = channelResponse?.items?.[0]?.snippet?.customUrl;
+ const data = channelResponse?.items?.[0];
- const { subscriberCount, viewCount, videoCount } =
- channelResponse?.items?.[0]?.statistics;
+ if (!data) {
+ return {
+ verified: false,
+ };
+ }
+
+ const channelId = data.id;
+ const handle = data.snippet.customUrl;
return {
verified:
@@ -48,9 +53,6 @@ export const ONLINE_PRESENCE_PROVIDERS: Record<string, OnlinePresenceProvider> =
metadata: {
channelId,
handle,
- subscriberCount,
- videoCount,
- viewCount,
},
};
},
diff --git a/apps/web/scripts/backfill-youtube-stats.ts b/apps/web/scripts/backfill-youtube-stats.ts
new file mode 100644
index 0000000000..f2904a62df
--- /dev/null
+++ b/apps/web/scripts/backfill-youtube-stats.ts
@@ -0,0 +1,97 @@
+import { prisma } from "@dub/prisma";
+import "dotenv-flow/config";
+import { google } from "googleapis";
+
+const youtube = google.youtube("v3");
+
+// First, we need to get the channel IDs from the handles
+async function getChannelIds(handles: string[]) {
+ const channelIds = await Promise.all(
+ handles.map(async (handle) => {
+ const response = await youtube.channels.list({
+ key: process.env.YOUTUBE_API_KEY,
+ part: ["id"],
+ forHandle: handle,
+ });
+ return response.data.items?.[0]?.id;
+ }),
+ );
+ return channelIds.filter(Boolean) as string[];
+}
+
+async function main() {
+ if (!process.env.YOUTUBE_API_KEY) {
+ throw new Error("Missing YouTube API key");
+ }
+
+ const youtubeVerifiedPartners = await prisma.partner.findMany({
+ where: {
+ youtube: {
+ not: null,
+ },
+ youtubeVerifiedAt: {
+ not: null,
+ },
+ youtubeChannelId: null,
+ },
+ select: {
+ id: true,
+ youtube: true,
+ },
+ take: 10,
+ });
+
+ try {
+ // First get the channel IDs
+ const channelIds = await getChannelIds(
+ youtubeVerifiedPartners.map((partner) => partner.youtube as string),
+ );
+ console.log("Found channel IDs:", channelIds);
+
+ // Then get the statistics for all channels
+ const response = await youtube.channels.list({
+ key: process.env.YOUTUBE_API_KEY,
+ part: ["statistics", "snippet"],
+ id: channelIds,
+ });
+
+ const stats = response.data.items;
+
+ if (!stats) {
+ return;
+ }
+
+ for (const stat of stats) {
+ const partner = youtubeVerifiedPartners.find(
+ (p) => p.youtube === stat.snippet?.customUrl,
+ );
+
+ if (!partner || !stat.statistics) {
+ continue;
+ }
+
+ const { viewCount, subscriberCount, videoCount } = stat.statistics;
+
+ await prisma.partner.update({
+ where: { id: partner.id },
+ data: {
+ youtubeChannelId: stat.id,
+ youtubeSubscriberCount: parseInt(subscriberCount || "0", 10),
+ youtubeVideoCount: parseInt(videoCount || "0", 10),
+ youtubeViewCount: parseInt(viewCount || "0", 10),
+ },
+ });
+
+ console.log(`Updated ${partner.youtube} with stats`, {
+ youtubeChannelId: stat.id,
+ youtubeSubscriberCount: parseInt(subscriberCount || "0", 10),
+ youtubeVideoCount: parseInt(videoCount || "0", 10),
+ youtubeViewCount: parseInt(viewCount || "0", 10),
+ });
+ }
+ } catch (error) {
+ console.error("Error fetching channel data:", error);
+ }
+}
+
+main();
diff --git a/apps/web/scripts/get-subscriber-count.ts b/apps/web/scripts/get-subscriber-count.ts
deleted file mode 100644
index 8b127d84cf..0000000000
--- a/apps/web/scripts/get-subscriber-count.ts
+++ /dev/null
@@ -1,54 +0,0 @@
-import "dotenv-flow/config";
-import { google } from "googleapis";
-
-const youtube = google.youtube("v3");
-
-// First, we need to get the channel IDs from the handles
-async function getChannelIds(handles: string[]) {
- const channelIds = await Promise.all(
- handles.map(async (handle) => {
- const response = await youtube.channels.list({
- key: process.env.YOUTUBE_API_KEY,
- part: ["id"],
- forHandle: handle,
- });
- return response.data.items?.[0]?.id;
- }),
- );
- return channelIds.filter(Boolean) as string[];
-}
-
-async function main() {
- if (!process.env.YOUTUBE_API_KEY) {
- throw new Error("Missing YouTube API key");
- }
-
- const handles = ["efficientapp", "stey", "hubermanlab"];
-
- try {
- // First get the channel IDs
- const channelIds = await getChannelIds(handles);
- console.log("Found channel IDs:", channelIds);
-
- // Then get the statistics for all channels
- const response = await youtube.channels.list({
- key: process.env.YOUTUBE_API_KEY,
- part: ["statistics", "snippet"],
- id: channelIds,
- });
-
- // Log data for each channel
- response.data.items?.forEach((channel) => {
- console.log(
- `\nChannel: @${channel.snippet?.customUrl || channel.snippet?.title}`,
- );
- console.log("Subscriber count:", channel.statistics?.subscriberCount);
- console.log("View count:", channel.statistics?.viewCount);
- console.log("Video count:", channel.statistics?.videoCount);
- });
- } catch (error) {
- console.error("Error fetching channel data:", error);
- }
-}
-
-main();
diff --git a/apps/web/vercel.json b/apps/web/vercel.json
index 0d4f1694e7..bb76d746c2 100644
--- a/apps/web/vercel.json
+++ b/apps/web/vercel.json
@@ -43,6 +43,10 @@
{
"path": "/api/cron/payouts/reminders/program-owners",
"schedule": "0 13 25-31,1-5 * *"
+ },
+ {
+ "path": "/api/cron/online-presence/youtube",
+ "schedule": "0 0 * * *"
}
],
"functions": {