Update index.js
Browse files
index.js
CHANGED
|
@@ -37,6 +37,136 @@ app.use((req, res, next) => {
|
|
| 37 |
next()
|
| 38 |
})
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
app.use('/file', express.static(tmpFolder))
|
| 41 |
|
| 42 |
app.all('/', (req, res) => {
|
|
|
|
| 37 |
next()
|
| 38 |
})
|
| 39 |
|
| 40 |
+
const DOWNLOAD_DIR = path.join(__dirname, 'downloads');
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
// Buat folder downloads jika belum ada
|
| 44 |
+
if (!fs.existsSync(DOWNLOAD_DIR)) {
|
| 45 |
+
fs.mkdirSync(DOWNLOAD_DIR, { recursive: true });
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// Fungsi pembersih file lama (> 24 jam)
|
| 49 |
+
function cleanupDownloads() {
|
| 50 |
+
console.log('Running cleanup...');
|
| 51 |
+
fs.readdir(DOWNLOAD_DIR, (err, files) => {
|
| 52 |
+
if (err) throw err;
|
| 53 |
+
|
| 54 |
+
const now = Date.now();
|
| 55 |
+
const oneDay = 24 * 60 * 60 * 1000;
|
| 56 |
+
|
| 57 |
+
files.forEach(file => {
|
| 58 |
+
const filePath = path.join(DOWNLOAD_DIR, file);
|
| 59 |
+
fs.stat(filePath, (err, stat) => {
|
| 60 |
+
if (err) return console.error(err);
|
| 61 |
+
|
| 62 |
+
if (now - stat.mtime.getTime() > oneDay) {
|
| 63 |
+
fs.unlink(filePath, err => {
|
| 64 |
+
if (err) console.error(`Error deleting ${filePath}:`, err);
|
| 65 |
+
else console.log(`Deleted old file: ${filePath}`);
|
| 66 |
+
});
|
| 67 |
+
}
|
| 68 |
+
});
|
| 69 |
+
});
|
| 70 |
+
});
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
// Jadwalkan cleanup setiap hari pukul 00:00
|
| 74 |
+
cron.schedule('0 0 * * *', cleanupDownloads);
|
| 75 |
+
|
| 76 |
+
// Fungsi buat nama file unik
|
| 77 |
+
function generateUniqueFilename() {
|
| 78 |
+
const timestamp = Date.now();
|
| 79 |
+
const randomStr = Math.random().toString(36).substring(2, 8);
|
| 80 |
+
return `video_${timestamp}_${randomStr}.mp4`;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
app.get('/download', async (req, res) => {
|
| 84 |
+
const { url } = req.query;
|
| 85 |
+
|
| 86 |
+
if (!url) {
|
| 87 |
+
return res.status(400).send('Please provide a Douyin URL as query parameter (?url=...)');
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
try {
|
| 91 |
+
// Step 1: Ambil link video dari DLPanda
|
| 92 |
+
const encodedUrl = encodeURIComponent(url);
|
| 93 |
+
const dlPandaUrl = `https://dlpanda.com/en?url=${encodedUrl}&token=G7eRpMaa`;
|
| 94 |
+
|
| 95 |
+
const response = await axios.get(dlPandaUrl, {
|
| 96 |
+
headers: {
|
| 97 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 98 |
+
}
|
| 99 |
+
});
|
| 100 |
+
|
| 101 |
+
const $ = cheerio.load(response.data);
|
| 102 |
+
let videoUrl = $('video source').attr('src') || $('a[download][target="_blank"]').attr('href');
|
| 103 |
+
|
| 104 |
+
if (!videoUrl) {
|
| 105 |
+
throw new Error('Video URL not found in the page');
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
if (videoUrl.startsWith('//')) {
|
| 109 |
+
videoUrl = 'https:' + videoUrl;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
// Step 2: Download videonya
|
| 113 |
+
const filename = generateUniqueFilename();
|
| 114 |
+
const filePath = path.join(DOWNLOAD_DIR, filename);
|
| 115 |
+
|
| 116 |
+
const videoResponse = await axios({
|
| 117 |
+
method: 'get',
|
| 118 |
+
url: videoUrl,
|
| 119 |
+
responseType: 'stream',
|
| 120 |
+
headers: {
|
| 121 |
+
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1',
|
| 122 |
+
'Accept': '*/*',
|
| 123 |
+
'Range': 'bytes=0-',
|
| 124 |
+
'Referer': 'https://www.douyin.com/'
|
| 125 |
+
}
|
| 126 |
+
});
|
| 127 |
+
|
| 128 |
+
const writer = fs.createWriteStream(filePath);
|
| 129 |
+
videoResponse.data.pipe(writer);
|
| 130 |
+
|
| 131 |
+
writer.on('finish', () => {
|
| 132 |
+
console.log(`Download completed: ${filename}`);
|
| 133 |
+
const fileUrl = `${req.protocol}://${req.get('host')}/${filename}`;
|
| 134 |
+
res.json({ url: fileUrl });
|
| 135 |
+
});
|
| 136 |
+
|
| 137 |
+
writer.on('error', (err) => {
|
| 138 |
+
console.error('Error writing file:', err);
|
| 139 |
+
res.status(500).send('Error saving file');
|
| 140 |
+
});
|
| 141 |
+
|
| 142 |
+
} catch (error) {
|
| 143 |
+
console.error('Error:', error.message);
|
| 144 |
+
res.status(500).send(`Error: ${error.message}`);
|
| 145 |
+
}
|
| 146 |
+
});
|
| 147 |
+
|
| 148 |
+
// Route publik file video: http://node.shiaori.xyz:19141/filename.mp4
|
| 149 |
+
app.get('/:filename', (req, res) => {
|
| 150 |
+
const filename = req.params.filename;
|
| 151 |
+
const filePath = path.join(DOWNLOAD_DIR, filename);
|
| 152 |
+
|
| 153 |
+
if (!fs.existsSync(filePath)) {
|
| 154 |
+
return res.status(404).send('File not found');
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
res.sendFile(filePath);
|
| 158 |
+
});
|
| 159 |
+
|
| 160 |
+
// Route debugging (opsional)
|
| 161 |
+
app.get('/files', (req, res) => {
|
| 162 |
+
fs.readdir(DOWNLOAD_DIR, (err, files) => {
|
| 163 |
+
if (err) {
|
| 164 |
+
return res.status(500).send('Error reading directory');
|
| 165 |
+
}
|
| 166 |
+
res.json(files);
|
| 167 |
+
});
|
| 168 |
+
});
|
| 169 |
+
|
| 170 |
app.use('/file', express.static(tmpFolder))
|
| 171 |
|
| 172 |
app.all('/', (req, res) => {
|