initial commit

This commit is contained in:
The Ghost of FOSS' Past 2024-10-28 10:34:22 -05:00
commit e88ed7ebb2
4 changed files with 1652 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

96
index.js Normal file
View file

@ -0,0 +1,96 @@
// index.js
const express = require('express');
const ytSearch = require('yt-search');
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 4286;
// 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.' });
}
});
// Video download endpoint
app.get('/download', (req, res) => {
const videoUrl = req.query.url;
if (!videoUrl) {
return res.status(400).json({ error: 'Query parameter "url" is required.' });
}
// Set the output file path
const outputPath = path.join(__dirname, 'downloads', '%(title)s.%(ext)s');
// Create downloads directory if it doesn't exist
fs.mkdirSync(path.join(__dirname, 'downloads'), { recursive: true });
// Use yt-dlp to download the video
exec(`yt-dlp -o "${outputPath}" "${videoUrl}"`, (error, stdout, stderr) => {
if (error) {
return res.status(500).json({ error: 'An error occurred while downloading the video.', details: stderr });
}
// Parse the output to get the file name
const filename = stdout.split('\n').find(line => line.includes('Destination'))?.split(' ')[1];
if (!filename) {
return res.status(500).json({ error: 'Downloaded file not found.' });
}
// Full path of the downloaded file
const downloadedFilePath = path.join(__dirname, 'downloads', filename);
// Check if the file exists and send it
if (fs.existsSync(downloadedFilePath)) {
res.download(downloadedFilePath, (err) => {
if (err) {
return res.status(500).json({ error: 'Failed to send the file.', details: err.message });
}
// Optionally, delete the file after sending it
fs.unlink(downloadedFilePath, (err) => {
if (err) {
console.error('Failed to delete the file:', err);
}
});
});
} else {
res.status(404).json({ error: 'Downloaded file not found.' });
}
});
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});

1537
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

18
package.json Normal file
View file

@ -0,0 +1,18 @@
{
"name": "uber_tuber",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "SEE LICENSE IN LICENSE",
"description": "",
"dependencies": {
"express": "^4.21.1",
"fs": "^0.0.1-security",
"path": "^0.12.7",
"yt-search": "^2.12.1"
}
}