Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
@@ -45,6 +45,90 @@ app.post('/imagetopdf', async (req, res) => {
|
|
45 |
}
|
46 |
})
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
const PORT = process.env.PORT || 7860
|
49 |
app.listen(PORT, () => console.log('App running on port', PORT))
|
50 |
|
|
|
45 |
}
|
46 |
})
|
47 |
|
48 |
+
app.all('/yt', async (req, res) => {
|
49 |
+
if (!['GET', 'POST'].includes(req.method)) return res.status(405).json({ success: false, message: 'Method Not Allowed' })
|
50 |
+
|
51 |
+
try {
|
52 |
+
const url = req.method !== 'GET' ? req.body.url : req.query.url
|
53 |
+
if (!url) return res.json({ success: false, message: 'Input parameter url' })
|
54 |
+
if (!/^https?:\/\//.test(url)) return res.json({ success: false, message: 'Invalid url' })
|
55 |
+
|
56 |
+
const response = await fetch(`https://srvcdn3.2convert.me/api/json?url=${url}`, { headers: { referer: 'https://y2mate.is/' }})
|
57 |
+
const json = await response.json()
|
58 |
+
if (json.error) return res.json({ success: false, message: json.message || json.errorMessage })
|
59 |
+
|
60 |
+
const { duration } = json.formats
|
61 |
+
const result = {
|
62 |
+
...json.formats,
|
63 |
+
lengthSeconds: +duration,
|
64 |
+
duration: new Date(duration * 1000).toUTCString().split(' ')[4],
|
65 |
+
video: {},
|
66 |
+
audio: {}
|
67 |
+
}
|
68 |
+
|
69 |
+
for (const type of ['video', 'audio']) {
|
70 |
+
json.formats[type].forEach((data) => {
|
71 |
+
const { quality: q, fileType, needConvert, fileSize, url: dlUrl } = data
|
72 |
+
const quality = type === 'video' ? q : `${q}kbps`
|
73 |
+
|
74 |
+
result[type][quality] = {
|
75 |
+
quality,
|
76 |
+
fileType,
|
77 |
+
fileSizeH: formatSize(fileSize),
|
78 |
+
fileSize,
|
79 |
+
needConvert,
|
80 |
+
url: !needConvert ? dlUrl : `https://${process.env.SPACE_HOST}${req.path}/convert?hash=${dlUrl}`
|
81 |
+
}
|
82 |
+
})
|
83 |
+
}
|
84 |
+
|
85 |
+
res.json({ success: true, result })
|
86 |
+
} catch (e) {
|
87 |
+
console.log(e)
|
88 |
+
e = String(e)
|
89 |
+
res.status(500).json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
|
90 |
+
}
|
91 |
+
})
|
92 |
+
|
93 |
+
app.get('/yt/convert', async (req, res) => {
|
94 |
+
try {
|
95 |
+
const { hash } = req.query
|
96 |
+
if (!hash) return res.json({ success: false, message: 'Invalid hash' })
|
97 |
+
|
98 |
+
const createPayload = (payload) => ({
|
99 |
+
method: 'POST',
|
100 |
+
body: new URLSearchParams(Object.entries(payload)),
|
101 |
+
headers: { referer: 'https://y2mate.is/' }
|
102 |
+
})
|
103 |
+
|
104 |
+
const reqTaskId = await fetch('https://srvcdn3.2convert.me/api/json', createPayload({ hash }))
|
105 |
+
if (!reqTaskId.ok) return res.json({ success: false, message: 'Hash not found' })
|
106 |
+
|
107 |
+
const reqTaskJson = await reqTaskId.json()
|
108 |
+
if (reqTaskJson.error) return res.json({ success: false, message: reqTaskJson.message })
|
109 |
+
|
110 |
+
let downloadUrl
|
111 |
+
while (!downloadUrl) {
|
112 |
+
const { taskId } = reqTaskJson
|
113 |
+
const fetchTaskId = await fetch('https://srvcdn3.2convert.me/api/json/task', createPayload({ taskId }))
|
114 |
+
const fetchTaskJson = await fetchTaskId.json()
|
115 |
+
|
116 |
+
if (fetchTaskJson.status === 'finished') {
|
117 |
+
downloadUrl = fetchTaskJson.download
|
118 |
+
break
|
119 |
+
}
|
120 |
+
|
121 |
+
await new Promise(resolve => setTimeout(resolve, 1000))
|
122 |
+
}
|
123 |
+
|
124 |
+
res.redirect(downloadUrl)
|
125 |
+
} catch (e) {
|
126 |
+
console.log(e)
|
127 |
+
e = String(e)
|
128 |
+
res.status(500).json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
|
129 |
+
}
|
130 |
+
})
|
131 |
+
|
132 |
const PORT = process.env.PORT || 7860
|
133 |
app.listen(PORT, () => console.log('App running on port', PORT))
|
134 |
|