Skip to content

Commit e1ccb8f

Browse files
authored
v3.1.0-beta.2
2 parents b69a2b4 + 5979aa7 commit e1ccb8f

6 files changed

Lines changed: 79 additions & 35 deletions

File tree

components/custom/GetterInput.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { searchQuery } from "@/functions/getYoutubeUrl";
34
import clsx from "clsx";
45
import { ClipboardCopy } from "lucide-react";
56
import { useRouter, useSearchParams } from "next/navigation";
@@ -40,11 +41,10 @@ export const GetterInput = () => {
4041
};
4142
};
4243

43-
const submitUrl = (url: string) => {
44-
if (url.length === 0) {
45-
return;
46-
}
47-
router.push(`/fetch?videoUrl=${url}`);
44+
const submitUrl = async (url: string) => {
45+
const getUrl = await searchQuery(url);
46+
47+
router.push(`/fetch?videoUrl=${getUrl}`);
4848
};
4949

5050
return (
@@ -59,18 +59,18 @@ export const GetterInput = () => {
5959
<div className="relative my-4 w-full">
6060
<input
6161
type="text"
62-
placeholder="Enter video url"
62+
placeholder="https://www.youtube.com/watch?v= - https://youtu.be/ - keywords"
6363
id="video-url"
6464
name="video-url"
65-
className="block w-full rounded-full border border-[#081721] bg-[#081721] p-2.5 text-white focus:border-blue-500 focus:ring-blue-500"
65+
className="block w-full rounded-md border border-[#081721] bg-[#081721] p-2.5 text-white focus:border-blue-500 focus:ring-blue-500"
6666
value={url}
6767
onChange={(e) => setUrl(e.target.value)}
6868
/>
6969
<button
7070
type="button"
7171
id="clipboard-copy"
7272
className={clsx(
73-
"absolute inset-y-0 right-0 flex items-center overflow-hidden rounded-r-full bg-secondary pl-2 pr-3.5 transition-all",
73+
"absolute inset-y-0 right-0 flex items-center overflow-hidden rounded-r-md bg-secondary px-4 transition-all",
7474
permission ? "opacity-100" : "bg-secondary/25",
7575
"hover:pointer-events-auto hover:cursor-pointer hover:bg-secondary/60 hover:opacity-100"
7676
)}
@@ -90,7 +90,7 @@ export const GetterInput = () => {
9090
<button
9191
type="submit"
9292
id="search-button"
93-
className="border-1 m-auto mx-auto rounded-full border border-solid border-transparent bg-[#205D83] px-5 py-2.5 text-center text-lg font-medium text-white transition-all duration-200 ease-in-out hover:cursor-pointer hover:border-[#205D83] hover:bg-[#102F42] hover:ring-[#205D83] focus:outline-none focus:ring-2 focus:ring-blue-300 sm:w-auto disabled:opacity-50"
93+
className="border-1 m-auto mx-auto rounded-md border border-solid border-transparent bg-[#205D83] px-5 py-2.5 text-center text-lg font-medium text-white transition-all duration-200 ease-in-out hover:cursor-pointer hover:border-[#205D83] hover:bg-[#102F42] hover:ring-[#205D83] focus:outline-none focus:ring-2 focus:ring-blue-300 sm:w-auto disabled:opacity-50"
9494
disabled={url.length === 0}
9595
>
9696
{false ? "Loading..." : "Search"}

components/custom/VideoSelect.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ export const VideoSelect = () => {
110110
return (
111111
<section className="py-8" id="error-search">
112112
<div className="mx-auto my-2 flex h-auto min-h-40 w-11/12 rounded-lg border-2 border-dashed border-[#102F42]">
113-
<p className="m-auto mx-auto text-center font-bold text-red-800 md:text-xl">
114-
{error ? error : "An error occured"}
113+
<p className="m-auto mx-auto text-center font-bold text-white md:text-xl">
114+
ERROR: {error ? error : "An error occured"}
115115
</p>
116116
</div>
117117
</section>
@@ -246,7 +246,7 @@ export const VideoSelect = () => {
246246
)}
247247

248248
{downloadError && (
249-
<p className="my-auto mx-auto text-center font-bold text-red-700 md:text-xl">
249+
<p className="my-auto mx-auto text-center font-bold text-white md:text-xl">
250250
{downloadError}
251251
</p>
252252
)}

functions/getYoutubeUrl.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use server";
2+
3+
import { yt_validate } from "@/lib/serverUtils";
4+
import ytsr from "@distube/ytsr";
5+
6+
export const searchQuery = async (query: string) => {
7+
if (await yt_validate(query)) {
8+
return query;
9+
}
10+
11+
const search = await ytsr(query, { limit: 1 });
12+
13+
if (search.items.length === 0) {
14+
throw new Error("No video found");
15+
}
16+
17+
const video = search.items[0];
18+
return video.url;
19+
};

lib/serverUtils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,27 @@ export async function sanitizeFilename(filename: string) {
6767
.replace(/\s+/g, "_")
6868
.slice(0, 255);
6969
}
70+
71+
const video_id_pattern = /^[a-zA-Z\d_-]{11,12}$/;
72+
const video_pattern =
73+
/^((?:https?:)?\/\/)?(?:(?:www|m|music)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|shorts\/|embed\/|live\/|v\/)?)([\w\-]+)(\S+)?$/;
74+
export async function yt_validate(url: string): Promise<"video" | false> {
75+
const url_ = url.trim();
76+
if (url_.indexOf("list=") === -1) {
77+
if (url_.startsWith("https")) {
78+
if (url_.match(video_pattern)) {
79+
let id: string;
80+
if (url_.includes("youtu.be/"))
81+
id = url_.split("youtu.be/")[1].split(/(\?|\/|&)/)[0];
82+
else if (url_.includes("youtube.com/embed/"))
83+
id = url_.split("youtube.com/embed/")[1].split(/(\?|\/|&)/)[0];
84+
else if (url_.includes("youtube.com/shorts/"))
85+
id = url_.split("youtube.com/shorts/")[1].split(/(\?|\/|&)/)[0];
86+
else id = url_.split("watch?v=")[1]?.split(/(\?|\/|&)/)[0];
87+
if (id?.match(video_id_pattern)) return "video";
88+
else return false;
89+
} else return false;
90+
}
91+
}
92+
return false;
93+
}

package-lock.json

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stroygetter",
3-
"version": "3.1.0-beta.1",
3+
"version": "3.1.0-beta.2",
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@distube/ytdl-core": "^4.16.0",
14+
"@distube/ytsr": "^2.0.4",
1415
"@ffmpeg-installer/ffmpeg": "^1.1.0",
1516
"@icons-pack/react-simple-icons": "^10.2.0",
1617
"@next/third-parties": "^15.1.3",

0 commit comments

Comments
 (0)