wudysoft commited on
Commit
099c1bc
·
verified ·
1 Parent(s): a59e178

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +67 -21
app.js CHANGED
@@ -1,19 +1,28 @@
1
  import express from 'express';
2
  import { chromium } from 'playwright';
3
  import cors from 'cors';
 
4
  import dotenv from 'dotenv';
 
5
 
6
  dotenv.config();
7
  const app = express();
8
  app.use(express.json());
9
  app.use(cors());
 
 
 
10
 
 
11
  app.get('/mediafire', async (req, res) => {
12
- const { url } = req.query;
13
- if (!url) return res.status(400).json({ success: false, message: 'URL is required' });
 
 
 
14
 
15
  try {
16
- const downloadInfo = await fetchDownloadInfo(url);
17
  return res.json(downloadInfo);
18
  } catch (error) {
19
  console.error('Error:', error);
@@ -21,44 +30,81 @@ app.get('/mediafire', async (req, res) => {
21
  }
22
  });
23
 
24
- async function fetchDownloadInfo(url) {
25
  const browser = await chromium.launch({ headless: true });
26
- const context = await browser.newContext({ userAgent: 'Mozilla/5.0 (Linux; Android 6.0; iris50)' });
 
 
27
  const page = await context.newPage();
28
 
29
  try {
30
  await page.goto(url);
31
- const info = await page.evaluate(() => {
32
- const getMeta = () => Array.from(document.querySelectorAll('meta')).reduce((acc, meta) => {
 
 
 
 
 
 
 
 
 
 
33
  const name = meta.getAttribute('name') || meta.getAttribute('property');
34
  const content = meta.getAttribute('content');
35
  if (name && content) acc[name.split(':')[1]] = content;
36
  return acc;
37
  }, {});
38
-
39
- const fileName = document.querySelector('.dl-btn-label')?.textContent.trim() || '';
40
- const downloadLink = document.querySelector('#downloadButton')?.href || '';
41
- const fileSize = document.querySelector('#downloadButton')?.textContent.match(/\(([^)]+)\)/)?.[1] || '';
42
-
43
- return { fileName, downloadLink, fileSize, ...getMeta() };
 
44
  });
45
 
46
- if (!info.downloadLink.startsWith('https://down')) {
47
- await page.goto(info.downloadLink);
48
- const updatedLink = await page.evaluate(() => document.querySelector('#downloadButton')?.href || '');
49
- info.downloadLink = updatedLink;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  }
51
 
52
- return info;
53
  } catch (error) {
54
  console.error('Error:', error.message);
55
  return { success: false, message: error.message };
56
  } finally {
57
- await browser.close();
 
 
58
  }
59
  }
60
 
61
  const PORT = process.env.PORT || 7860;
62
- app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
63
 
64
- process.on('SIGINT', () => process.exit(0));
 
 
 
 
 
 
 
 
 
1
  import express from 'express';
2
  import { chromium } from 'playwright';
3
  import cors from 'cors';
4
+ import axios from 'axios';
5
  import dotenv from 'dotenv';
6
+ import os from 'os';
7
 
8
  dotenv.config();
9
  const app = express();
10
  app.use(express.json());
11
  app.use(cors());
12
+ app.get('/', async (req, res) => {
13
+ return res.status(200).json({ success: true, message: 'DOWNLOADER' });
14
+ });
15
 
16
+ // Main API route to handle MediaFire URL via GET
17
  app.get('/mediafire', async (req, res) => {
18
+ const { url } = req.query; // URL comes from query string
19
+
20
+ if (!url) {
21
+ return res.status(400).json({ success: false, message: 'URL is required' });
22
+ }
23
 
24
  try {
25
+ const downloadInfo = await mediafire(url);
26
  return res.json(downloadInfo);
27
  } catch (error) {
28
  console.error('Error:', error);
 
30
  }
31
  });
32
 
33
+ async function mediafire(url) {
34
  const browser = await chromium.launch({ headless: true });
35
+ const context = await browser.newContext({
36
+ userAgent: 'Mozilla/5.0 (Linux; Android 6.0; iris50) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36',
37
+ });
38
  const page = await context.newPage();
39
 
40
  try {
41
  await page.goto(url);
42
+
43
+ let downloadInfo = await page.evaluate(() => {
44
+ const fileNameElement = document.querySelector('.dl-btn-label');
45
+ const fileName = fileNameElement ? fileNameElement.textContent.trim() : '';
46
+ const downloadLinkElement = document.querySelector('#downloadButton');
47
+ const downloadLink = downloadLinkElement ? downloadLinkElement.href : '';
48
+ const fileSizeText = downloadLinkElement ? downloadLinkElement.textContent : '';
49
+ const sizeMatch = fileSizeText.match(/\(([^)]+)\)/);
50
+ const fileSize = sizeMatch ? sizeMatch[1] : '';
51
+
52
+ // Ambil informasi meta
53
+ const metaTags = Array.from(document.querySelectorAll('meta')).reduce((acc, meta) => {
54
  const name = meta.getAttribute('name') || meta.getAttribute('property');
55
  const content = meta.getAttribute('content');
56
  if (name && content) acc[name.split(':')[1]] = content;
57
  return acc;
58
  }, {});
59
+
60
+ return {
61
+ fileName,
62
+ downloadLink,
63
+ fileSize,
64
+ meta: metaTags,
65
+ };
66
  });
67
 
68
+ // Jika tautan unduhan tidak valid, tutup browser dan buka tautan baru
69
+ if (!downloadInfo.downloadLink.startsWith('https://down')) {
70
+ await browser.close(); // Menutup browser sebelum membuka halaman baru
71
+
72
+ const newBrowser = await chromium.launch({ headless: true });
73
+ const newContext = await newBrowser.newContext({
74
+ userAgent: 'Mozilla/5.0 (Linux; Android 6.0; iris50) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36',
75
+ });
76
+ const newPage = await newContext.newPage();
77
+
78
+ await newPage.goto(downloadInfo.downloadLink);
79
+
80
+ const updatedInfo = await newPage.evaluate(() => {
81
+ const downloadLink = document.querySelector('#downloadButton')?.href || '';
82
+ return { downloadLink };
83
+ });
84
+
85
+ downloadInfo.downloadLink = updatedInfo.downloadLink;
86
+ await newBrowser.close(); // Menutup browser setelah selesai
87
  }
88
 
89
+ return downloadInfo;
90
  } catch (error) {
91
  console.error('Error:', error.message);
92
  return { success: false, message: error.message };
93
  } finally {
94
+ if (browser) {
95
+ await browser.close(); // Menutup browser setelah selesai
96
+ }
97
  }
98
  }
99
 
100
  const PORT = process.env.PORT || 7860;
 
101
 
102
+ app.listen(PORT, async () => {
103
+ console.log(`Server running on port ${PORT}`);
104
+ //await utils.initialize();
105
+ });
106
+
107
+ process.on('SIGINT', async () => {
108
+ //await utils.close();
109
+ process.exit(0);
110
+ });