From ba853342bd2baa0a6a8df11b4d34e18b5e584e04 Mon Sep 17 00:00:00 2001 From: Max Richter Date: Wed, 2 Aug 2023 15:56:33 +0200 Subject: [PATCH] feat: add ability to scrape youtube video --- components/Youtube.tsx | 24 +------- lib/cache/image.ts | 4 +- lib/env.ts | 1 + lib/openai.ts | 8 ++- lib/string.ts | 23 ++++++++ lib/youtube.ts | 86 +++++++++++++++++++++++++++++ routes/api/articles/create/index.ts | 71 +++++++++++++++++++++--- routes/articles/[name].tsx | 3 +- routes/articles/index.tsx | 8 ++- routes/movies/[name].tsx | 1 + 10 files changed, 193 insertions(+), 36 deletions(-) create mode 100644 lib/youtube.ts diff --git a/components/Youtube.tsx b/components/Youtube.tsx index e70748a..d33d174 100644 --- a/components/Youtube.tsx +++ b/components/Youtube.tsx @@ -1,30 +1,10 @@ -export const isYoutubeLink = (link: string) => { - try { - const url = new URL(link); - return ["youtu.be", "youtube.com"].includes(url.hostname); - } catch (err) { - console.log(err); - return false; - } -}; - -function extractYoutubeId(link: string) { - const url = new URL(link); - if (url.searchParams.has("v")) { - const id = url.searchParams.get("v"); - - if (id?.length && id.length > 4) { - return id; - } - } - - return url.pathname.replace(/^\//, ""); -} +import { extractYoutubeId } from "@lib/string.ts"; export const YoutubePlayer = ({ link }: { link: string }) => { const id = extractYoutubeId(link); return (