2024-10-28 15:34:22 +00:00
|
|
|
// index.js
|
|
|
|
const express = require('express');
|
|
|
|
const ytSearch = require('yt-search');
|
|
|
|
const { exec } = require('child_process');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2024-10-28 15:58:13 +00:00
|
|
|
const cors = require('cors');
|
2024-10-28 15:34:22 +00:00
|
|
|
const app = express();
|
|
|
|
const port = 4286;
|
|
|
|
|
2024-10-28 15:58:13 +00:00
|
|
|
//use cors
|
|
|
|
app.use(cors());
|
|
|
|
|
2024-10-28 15:34:22 +00:00
|
|
|
// Middleware to parse JSON
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
|
|
// Search endpoint using yt-search
|
|
|
|
app.get('/search', async (req, res) => {
|
|
|
|
const query = req.query.q;
|
|
|
|
|
|
|
|
if (!query) {
|
|
|
|
return res.status(400).json({ error: 'Query parameter "q" is required.' });
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await ytSearch(query);
|
|
|
|
const videos = result.videos.slice(0, 1000); // Get up to 1000 videos
|
|
|
|
|
|
|
|
// Format videos for response
|
|
|
|
const formattedVideos = videos.map(v => {
|
|
|
|
const views = String(v.views).padStart(10, ' ');
|
|
|
|
return {
|
|
|
|
views: views,
|
|
|
|
title: v.title,
|
|
|
|
timestamp: v.timestamp,
|
|
|
|
author: v.author.name,
|
|
|
|
url: v.url
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
res.json(formattedVideos);
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({ error: 'An error occurred while searching for videos.' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-28 16:32:05 +00:00
|
|
|
function getRandomHex() {
|
|
|
|
// Generate a random integer between 100 and 99999
|
|
|
|
const randomNumber = Math.floor(Math.random() * (99999 - 100 + 1)) + 100;
|
|
|
|
|
|
|
|
// Convert the number to a hexadecimal string and return it
|
|
|
|
return randomNumber.toString(16);
|
|
|
|
}
|
|
|
|
|
2024-10-28 15:34:22 +00:00
|
|
|
// Video download endpoint
|
|
|
|
app.get('/download', (req, res) => {
|
|
|
|
const videoUrl = req.query.url;
|
|
|
|
|
|
|
|
if (!videoUrl) {
|
2024-10-28 16:32:05 +00:00
|
|
|
console.error('Error: Query parameter "url" is required.');
|
2024-10-28 15:34:22 +00:00
|
|
|
return res.status(400).json({ error: 'Query parameter "url" is required.' });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the output file path
|
2024-10-28 16:50:23 +00:00
|
|
|
var outputPath = path.join('/tmp/downloads', `${getRandomHex()}`);
|
2024-10-28 15:34:22 +00:00
|
|
|
|
2024-10-28 15:58:13 +00:00
|
|
|
// Create /tmp/downloads directory if it doesn't exist
|
2024-10-28 16:32:05 +00:00
|
|
|
try {
|
|
|
|
fs.mkdirSync(path.join('/tmp/downloads'), { recursive: true });
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error creating directory:', err);
|
|
|
|
return res.status(500).json({ error: 'Failed to create download directory.', details: err.message });
|
|
|
|
}
|
2024-10-28 15:34:22 +00:00
|
|
|
|
|
|
|
// Use yt-dlp to download the video
|
|
|
|
exec(`yt-dlp -o "${outputPath}" "${videoUrl}"`, (error, stdout, stderr) => {
|
|
|
|
if (error) {
|
2024-10-28 16:32:05 +00:00
|
|
|
console.error('Error executing yt-dlp:', stderr);
|
2024-10-28 15:34:22 +00:00
|
|
|
return res.status(500).json({ error: 'An error occurred while downloading the video.', details: stderr });
|
|
|
|
}
|
|
|
|
|
2024-10-28 16:32:05 +00:00
|
|
|
console.log('yt-dlp output:', stdout);
|
|
|
|
|
2024-10-28 16:50:23 +00:00
|
|
|
const downloadedFilePath = `${outputPath}.webm`;
|
2024-10-28 16:22:43 +00:00
|
|
|
|
2024-10-28 16:32:05 +00:00
|
|
|
console.log('Downloaded file path:', downloadedFilePath);
|
|
|
|
|
|
|
|
if (!downloadedFilePath || !fs.existsSync(downloadedFilePath)) {
|
2024-10-28 16:50:23 +00:00
|
|
|
console.log(`${downloadedFilePath}`)
|
2024-10-28 16:32:05 +00:00
|
|
|
console.error('Error: Downloaded file not found or invalid path.');
|
|
|
|
return res.status(500).json({ error: 'Downloaded file not found or invalid path.' });
|
2024-10-28 15:34:22 +00:00
|
|
|
}
|
|
|
|
|
2024-10-28 16:32:05 +00:00
|
|
|
// Send the file
|
2024-10-28 16:50:23 +00:00
|
|
|
res.sendFile(downloadedFilePath, (err) => {
|
2024-10-28 16:32:05 +00:00
|
|
|
if (err) {
|
|
|
|
console.error('Error sending the file:', err);
|
|
|
|
return res.status(500).json({ error: 'Failed to send the file.', details: err.message });
|
|
|
|
}
|
2024-10-28 15:34:22 +00:00
|
|
|
|
2024-10-28 16:32:05 +00:00
|
|
|
// Optionally, delete the file after sending it
|
|
|
|
fs.unlink(downloadedFilePath, (err) => {
|
2024-10-28 15:34:22 +00:00
|
|
|
if (err) {
|
2024-10-28 16:32:05 +00:00
|
|
|
console.error('Failed to delete the file:', err);
|
2024-10-28 15:34:22 +00:00
|
|
|
}
|
|
|
|
});
|
2024-10-28 16:32:05 +00:00
|
|
|
});
|
2024-10-28 15:34:22 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-10-28 16:32:05 +00:00
|
|
|
|
2024-10-28 15:34:22 +00:00
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Server running at http://localhost:${port}`);
|
|
|
|
});
|
2024-10-28 15:58:55 +00:00
|
|
|
|