File size: 3,665 Bytes
22bf4eb f535bab 22bf4eb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import axios from "axios";
import { sendSuccess, ErrorResponses } from "../../lib/response-helper.js";
class Youtube {
constructor() {
this.API_URL = "https://thesocialcat.com/api/youtube-download";
this.HEADERS = {
"accept": "*/*",
"accept-language": "id-ID",
"content-type": "application/json",
"Referer": "https://thesocialcat.com/tools/youtube-video-downloader",
"Referrer-Policy": "strict-origin-when-cross-origin"
};
this.CREATED_BY = "Ditzzy";
this.NOTE = "Thank you for using this scrape, I hope you appreciate me for making this scrape by not deleting wm";
}
wrapResponse(data) {
return {
created_by: this.CREATED_BY,
note: this.NOTE,
results: data
};
}
async download(url, format) {
try {
const config = {
url: this.API_URL,
headers: this.HEADERS,
method: "POST",
data: {
format,
url
}
};
const { data } = await axios.request(config);
return this.wrapResponse(data);
} catch (e) {
throw new Error(`Error downloading YouTube content: ${e}`);
}
}
}
/** @type {import("../../types/plugin").ApiPluginHandler} */
const handler = {
name: "YouTube Downloader",
description: "Download YouTube media with resolution up to 1080p and support audio",
version: "1.0.0",
method: "GET",
category: ["downloader"],
alias: ["youtube", "yt"],
tags: ["social-media", "video", "downloader"],
parameters: {
query: [
{
name: "url",
type: "string",
required: true,
description: "Your YouTube URL",
example: "https://youtu.be/zawDTvoXT8k?si=FgZnxXzMXJI8jfkB"
},
{
name: "format",
type: "string",
required: true,
description: "Format to download",
example: "720p",
enum: ["144p", "240p", "360p", "480p", "720p", "1080p", "audio"],
default: "720p"
}
],
body: [],
headers: []
},
responses: {
200: {
status: 200,
description: "Successfully retrieved YouTube video data",
example: {
status: 200,
author: "Ditzzy",
note: "Thank you for using this API!",
results: {}
}
},
400: {
status: 400,
description: "Invalid YouTube URL provided",
example: {
status: 400,
message: "Invalid URL - must be a valid YouTube URL"
}
},
404: {
status: 404,
description: "Missing required parameter",
example: {
status: 404,
message: "Missing required parameter: ..."
}
},
500: {
status: 500,
description: "Server error or YouTube API unavailable",
example: {
status: 500,
message: "An error occurred, please try again later."
}
}
},
exec: async (req, res) => {
const { url } = req.query
const { format } = req.query
if (!url) return ErrorResponses.missingParameter(res, "url");
if (!format) return ErrorResponses.missingParameter(res, "format");
const regex = /^(https?:\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com|youtu\.be)(\/(embed\/|v\/|watch\?v=|watch\?.+&v=|shorts\/)?)([\w-]{11})(\S+)?$/;
if (!regex.test(url)) return ErrorResponses.invalidUrl(res, "Invalid URL - must be a valid YouTube URL");
const yt = new Youtube();
try {
const download = await yt.download(url, format);
return sendSuccess(res, download.results);
} catch (e) {
console.error("Error:", e);
return ErrorResponses.serverError(res);
}
}
}
export default handler; |