Create routes/youtube/index.js
Browse files- routes/youtube/index.js +91 -0
routes/youtube/index.js
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { t } from 'elysia'
|
2 |
+
import yts from 'yt-search'
|
3 |
+
|
4 |
+
const fetchCobaltAPI = async (body) => {
|
5 |
+
const response = await fetch('https://c.blahaj.ca/', {
|
6 |
+
method: 'POST',
|
7 |
+
body: JSON.stringify(body),
|
8 |
+
headers: {
|
9 |
+
Accept: 'application/json',
|
10 |
+
'Content-Type': 'application/json'
|
11 |
+
}
|
12 |
+
})
|
13 |
+
return response.json()
|
14 |
+
}
|
15 |
+
|
16 |
+
const ytIdRegex =
|
17 |
+
/(?:http(?:s|):\/\/|)(?:(?:www\.|)?youtube(?:\-nocookie|)\.com\/(?:shorts\/)?(?:watch\?.*(?:|\&)v=|embed\/|live\/|v\/)?|youtu\.be\/)([-_0-9A-Za-z]{11})/
|
18 |
+
|
19 |
+
const handleDownload = async (ctx) => {
|
20 |
+
const { quality, type, url } = ctx.request.method !== 'GET' ? ctx.body : ctx.query
|
21 |
+
if (!url) return ctx.error(400, { message: 'Missing url' })
|
22 |
+
|
23 |
+
const payload = { filenameStyle: 'pretty', url }
|
24 |
+
if (type === 'audio') {
|
25 |
+
payload.audioBitrate = quality || '128'
|
26 |
+
payload.downloadMode = 'audio'
|
27 |
+
} else payload.videoQuality = quality || '720'
|
28 |
+
|
29 |
+
const result = await fetchCobaltAPI(payload)
|
30 |
+
if (!result.url) return ctx.error(500, result.error)
|
31 |
+
|
32 |
+
return ctx.redirect(result.url)
|
33 |
+
}
|
34 |
+
|
35 |
+
const handleSearch = async (ctx) => {
|
36 |
+
const { query } = ctx.request.method !== 'GET' ? ctx.body : ctx.query
|
37 |
+
if (!query) return ctx.error(400, { message: 'Missing query' })
|
38 |
+
|
39 |
+
const result = await yts(query)
|
40 |
+
if (!result.all?.length) return ctx.error(500, { message: 'Video unavailable' })
|
41 |
+
|
42 |
+
return {
|
43 |
+
success: true,
|
44 |
+
data: result
|
45 |
+
}
|
46 |
+
}
|
47 |
+
|
48 |
+
const handleSearchAndDownload = async (ctx) => {
|
49 |
+
const { query } = ctx.request.method !== 'GET' ? ctx.body : ctx.query
|
50 |
+
if (!query) return ctx.error(400, { message: 'Missing query' })
|
51 |
+
|
52 |
+
const id = ytIdRegex.exec(query)?.[1]
|
53 |
+
let result = await yts(id ? { videoId: id } : query)
|
54 |
+
result = result.videos ? result.videos[0] : result
|
55 |
+
if (!result?.url) return ctx.error(500, { message: 'Video unavailable' })
|
56 |
+
|
57 |
+
const { origin, pathname } = new URL(ctx.url)
|
58 |
+
const dlUrl = `${origin + pathname}/download?url=${result.url}`
|
59 |
+
|
60 |
+
return {
|
61 |
+
success: true,
|
62 |
+
data: {
|
63 |
+
...result,
|
64 |
+
download: {
|
65 |
+
audio: `${dlUrl}&type=audio`,
|
66 |
+
video: `${dlUrl}&type=video`
|
67 |
+
}
|
68 |
+
}
|
69 |
+
}
|
70 |
+
}
|
71 |
+
|
72 |
+
const schemaJSON = {
|
73 |
+
download: t.Object({
|
74 |
+
url: t.String(),
|
75 |
+
type: t.Optional(t.String()),
|
76 |
+
quality: t.Optional(t.String())
|
77 |
+
}),
|
78 |
+
search: t.Object({ query: t.String() })
|
79 |
+
}
|
80 |
+
|
81 |
+
export default (app) => app
|
82 |
+
.group('', {
|
83 |
+
detail: { tags: ['youtube'] }
|
84 |
+
}, (app) => app
|
85 |
+
.get('', handleSearchAndDownload, { query: schemaJSON.search })
|
86 |
+
.post('', handleSearchAndDownload, { body: schemaJSON.search })
|
87 |
+
.get('/download', handleDownload, { query: schemaJSON.download })
|
88 |
+
.post('/download', handleDownload, { body: schemaJSON.download })
|
89 |
+
.get('/search', handleSearch, { query: schemaJSON.search })
|
90 |
+
.post('/search', handleSearch, { body: schemaJSON.search })
|
91 |
+
)
|