diff --git a/workspaces/cms-scripts/src/fixdata.ts b/workspaces/cms-scripts/src/fixdata.ts deleted file mode 100644 index 9e5a388e5b..0000000000 --- a/workspaces/cms-scripts/src/fixdata.ts +++ /dev/null @@ -1,47 +0,0 @@ -import fs from "fs/promises"; -import * as path from "path"; -import * as dotenv from "dotenv"; -import { google } from "googleapis"; -import YAML from "yaml"; - -process.chdir(path.resolve(__dirname, "../../..")); - -const dotenvFiles = [".env.local", ".env"]; - -dotenvFiles.forEach((path) => dotenv.config({ path })); - -import { yaml } from "./utils"; -import { youtubeVideoIdFromURL } from "@starknet-io/cms-utils/src"; - -const resourceName = "posts"; -const filenames = await fs.readdir(`_data/${resourceName}`); - -for (const filename of filenames) { - const filepath = path.join("_data", resourceName, filename); - - const data = await yaml(filepath); - - if (data.video) { - const id = youtubeVideoIdFromURL(data.video.url); - - if (id) { - data.video.id = id; - - const youtube = google.youtube({ - version: "v3", - auth: process.env.YOUTUBE_API_KEY, - }); - - const res = await youtube.videos.list({ - id: [id], - part: ["snippet", "contentDetails"], - }); - - data.video.data = res.data?.items?.[0]; - } - - await fs.writeFile(filepath, YAML.stringify(data), { - encoding: "utf8", - }); - } -} diff --git a/workspaces/cms-utils/src/index.test.ts b/workspaces/cms-utils/src/index.test.ts new file mode 100644 index 0000000000..5fa94cc205 --- /dev/null +++ b/workspaces/cms-utils/src/index.test.ts @@ -0,0 +1,23 @@ +import { youtubeVideoIdFromURL } from './index'; // replace with your actual file path +import { expect, test } from 'vitest'; + +test('youtubeVideoIdFromURL', () => { + // Testing full YouTube URLs + expect(youtubeVideoIdFromURL('https://www.youtube.com/watch?v=dQw4w9WgXcQ')).toBe('dQw4w9WgXcQ'); + expect(youtubeVideoIdFromURL('https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley')).toBe('dQw4w9WgXcQ'); + + + // Testing URLs with 'live' in the path + expect(youtubeVideoIdFromURL('https://www.youtube.com/live/FWvP5bh0yIM')).toBe('FWvP5bh0yIM'); + expect(youtubeVideoIdFromURL('https://www.youtube.com/live/FWvP5bh0yIM?feature=share')).toBe('FWvP5bh0yIM'); + + // Testing no 'www' + expect(youtubeVideoIdFromURL('https://youtube.com/live/NL-skz5VW_0')).toBe('NL-skz5VW_0'); + + // Testing shortened URLs + expect(youtubeVideoIdFromURL('https://youtu.be/dQw4w9WgXcQ')).toBe('dQw4w9WgXcQ'); + + // Testing URLs with 'embed' in the path + expect(youtubeVideoIdFromURL('https://www.youtube.com/embed/FWvP5bh0yIM')).toBe('FWvP5bh0yIM'); + +}); diff --git a/workspaces/cms-utils/src/index.ts b/workspaces/cms-utils/src/index.ts index 19d5c96f55..7e8bd87d84 100644 --- a/workspaces/cms-utils/src/index.ts +++ b/workspaces/cms-utils/src/index.ts @@ -5,12 +5,15 @@ export function youtubeVideoIdFromURL(url: string): string | undefined | void { try { const obj = new URL(url); - if (obj.hostname === "www.youtube.com") { + if (obj.hostname === "www.youtube.com" || obj.hostname === "youtube.com") { if (obj.searchParams.get("v")) return obj.searchParams.get("v")!; if (obj.pathname.startsWith("/live/")) { return obj.pathname.replace("/live/", ""); } + if(obj.pathname.startsWith("/embed/")) { + return obj.pathname.replace("/embed/", ""); + } } else if (obj.hostname === "youtu.be") { return obj.pathname.slice(1); }