Skip to content

Commit

Permalink
feat: support youtube url redirect from url address
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 7, 2023
1 parent c00bb64 commit 95a03f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 10 additions & 5 deletions pages/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useToast } from "~/hooks/use-toast";
import { useSummarize } from "~/hooks/useSummarize";
import { VideoService } from "~/lib/types";
import { extractUrl } from "~/utils/extractUrl";
import { getValidatedUrl } from "~/utils/getValidatedUrl";
import { getVideoIdFromUrl } from "~/utils/getVideoIdFromUrl";
import getVideoId from "get-video-id";

export const Home: NextPage = () => {
Expand All @@ -42,14 +42,17 @@ export const Home: NextPage = () => {
useEffect(() => {
// https://www.youtube.com/watch?v=DHhOgWPKIKU
// todo: support redirect from www.youtube.jimmylv.cn/watch?v=DHhOgWPKIKU
const validatedUrl = getValidatedUrl(
const validatedUrl = getVideoIdFromUrl(
router.isReady,
currentVideoUrl,
urlState
urlState,
searchParams
);

console.log("========validatedUrl========", validatedUrl);

validatedUrl && generateSummary(validatedUrl);
}, [router.isReady, urlState]);
}, [router.isReady, urlState, searchParams]);

const validateUrl = (url?: string) => {
// note: auto refactor by ChatGPT
Expand All @@ -65,6 +68,7 @@ export const Home: NextPage = () => {
return;
}

// 来自输入框
if (!url) {
// -> '/video/BV12Y4y127rj'
const curUrl = String(videoUrl.split(".com")[1]);
Expand All @@ -75,6 +79,8 @@ export const Home: NextPage = () => {
};
const generateSummary = async (url?: string) => {
resetSummary();
validateUrl(url);

const videoUrl = url || currentVideoUrl;
const { id, service } = getVideoId(videoUrl);
if (service === VideoService.Youtube && id) {
Expand All @@ -86,7 +92,6 @@ export const Home: NextPage = () => {
return;
}

validateUrl(url);
const bvId = extractUrl(videoUrl);
if (!bvId) {
return;
Expand Down
8 changes: 6 additions & 2 deletions utils/getValidatedUrl.tsx → utils/getVideoIdFromUrl.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export function getValidatedUrl(
export function getVideoIdFromUrl(
isReady: boolean,
currentVideoUrl: string,
urlState?: string | string[]
urlState?: string | string[],
searchParams?: URLSearchParams
): string | undefined {
const isValidatedUrl =
isReady &&
Expand All @@ -11,6 +12,9 @@ export function getValidatedUrl(
urlState.every((subslug: string) => typeof subslug === "string");

if (isValidatedUrl) {
if (urlState[0] === "watch") {
return "https://youtube.com/watch?v=" + searchParams?.get("v");
}
return `https://bilibili.com/${(urlState as string[]).join("/")}`;
}
}

1 comment on commit 95a03f9

@vercel
Copy link

@vercel vercel bot commented on 95a03f9 Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.