From 4a0246279dd21ad6fedcfcecadea8175c46480f2 Mon Sep 17 00:00:00 2001 From: lorcan-codes <126797224+lorcan-codes@users.noreply.github.com> Date: Fri, 21 Jul 2023 20:40:44 +0500 Subject: [PATCH 1/3] Improve youtubeVideoIdFromURL and add a test for it --- workspaces/cms-utils/src/index.test.ts | 23 +++++++++++++++++++++++ workspaces/cms-utils/src/index.ts | 5 ++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 workspaces/cms-utils/src/index.test.ts 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); } From a0ea9d8220bec6af89c24e459c8903d53ba76791 Mon Sep 17 00:00:00 2001 From: lorcan-codes <126797224+lorcan-codes@users.noreply.github.com> Date: Fri, 21 Jul 2023 20:40:44 +0500 Subject: [PATCH 2/3] Improve youtubeVideoIdFromURL and add a test for it --- workspaces/cms-utils/src/index.test.ts | 23 +++++++++++++++++++++++ workspaces/cms-utils/src/index.ts | 5 ++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 workspaces/cms-utils/src/index.test.ts 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); } From 301db9373c872e56f70d5c9a0f041d9846ee41b3 Mon Sep 17 00:00:00 2001 From: lorcan-codes <126797224+lorcan-codes@users.noreply.github.com> Date: Fri, 21 Jul 2023 21:04:14 +0500 Subject: [PATCH 3/3] Update affected post && remove YT widget leftover script --- workspaces/cms-scripts/src/fixdata.ts | 47 --------------------------- 1 file changed, 47 deletions(-) delete mode 100644 workspaces/cms-scripts/src/fixdata.ts 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", - }); - } -}