wudysoft commited on
Commit
a59e178
·
verified ·
1 Parent(s): 13c80f0

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +22 -111
app.js CHANGED
@@ -1,26 +1,19 @@
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
 
13
-
14
- // Main API route to handle MediaFire URL via GET
15
  app.get('/mediafire', async (req, res) => {
16
- const { url } = req.query; // URL comes from query string
17
-
18
- if (!url) {
19
- return res.status(400).json({ success: false, message: 'URL is required' });
20
- }
21
 
22
  try {
23
- const downloadInfo = await mediafire(url);
24
  return res.json(downloadInfo);
25
  } catch (error) {
26
  console.error('Error:', error);
@@ -28,126 +21,44 @@ app.get('/mediafire', async (req, res) => {
28
  }
29
  });
30
 
31
- async function mediafire(url) {
32
  const browser = await chromium.launch({ headless: true });
33
- const context = await browser.newContext({
34
- userAgent: 'Mozilla/5.0 (Linux; Android 6.0; iris50) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36',
35
- });
36
  const page = await context.newPage();
37
 
38
  try {
39
  await page.goto(url);
40
-
41
- let downloadInfo = await page.evaluate(() => {
42
- const fileNameElement = document.querySelector('.dl-btn-label');
43
- const fileName = fileNameElement ? fileNameElement.textContent.trim() : '';
44
- const downloadLinkElement = document.querySelector('#downloadButton');
45
- const downloadLink = downloadLinkElement ? downloadLinkElement.href : '';
46
- const fileSizeText = downloadLinkElement ? downloadLinkElement.textContent : '';
47
- const sizeMatch = fileSizeText.match(/\(([^)]+)\)/);
48
- const fileSize = sizeMatch ? sizeMatch[1] : '';
49
-
50
- // Ambil informasi meta
51
- const metaTags = Array.from(document.querySelectorAll('meta')).reduce((acc, meta) => {
52
  const name = meta.getAttribute('name') || meta.getAttribute('property');
53
  const content = meta.getAttribute('content');
54
- if (name && content) acc[name] = content;
55
  return acc;
56
  }, {});
57
-
58
- return {
59
- fileName,
60
- downloadLink,
61
- fileSize,
62
- meta: metaTags,
63
- };
64
  });
65
 
66
- // Jika tautan unduhan tidak valid, tutup browser dan buka tautan baru
67
- if (!downloadInfo.downloadLink.startsWith('https://down')) {
68
- await browser.close(); // Menutup browser sebelum membuka halaman baru
69
-
70
- const newBrowser = await chromium.launch({ headless: true });
71
- const newContext = await newBrowser.newContext({
72
- userAgent: 'Mozilla/5.0 (Linux; Android 6.0; iris50) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36',
73
- });
74
- const newPage = await newContext.newPage();
75
-
76
- await newPage.goto(downloadInfo.downloadLink);
77
-
78
- const updatedInfo = await newPage.evaluate(() => {
79
- const downloadLink = document.querySelector('#downloadButton')?.href || '';
80
- return { downloadLink };
81
- });
82
-
83
- downloadInfo.downloadLink = updatedInfo.downloadLink;
84
- await newBrowser.close(); // Menutup browser setelah selesai
85
  }
86
 
87
- return downloadInfo;
88
  } catch (error) {
89
  console.error('Error:', error.message);
90
  return { success: false, message: error.message };
91
  } finally {
92
- if (browser) {
93
- await browser.close(); // Menutup browser setelah selesai
94
- }
95
  }
96
  }
97
 
98
- app.get('/shorten', async (req, res) => {
99
- const url = req.query.url;
100
-
101
- if (!url) {
102
- return res.status(400).json({ error: 'Parameter URL tidak boleh kosong. Gunakan format: /shorten?url=<URL>' });
103
- }
104
-
105
- let browser;
106
- try {
107
- // Luncurkan browser
108
- browser = await chromium.launch({ headless: true });
109
- const context = await browser.newContext();
110
- const page = await context.newPage();
111
-
112
- // Buka halaman s.id Shortener
113
- await page.goto('https://home.s.id/shortener');
114
-
115
- // Tunggu elemen input URL muncul
116
- await page.waitForSelector('input[name="url"]');
117
-
118
- // Isi input URL
119
- await page.fill('input[name="url"]', url);
120
-
121
- // Klik tombol "Short It!"
122
- await page.click('button:has-text("Short it!")');
123
-
124
- // Tunggu hasil shortlink muncul (sesuai elemen di halaman)
125
- await page.waitForSelector('input[name="shorturl"]');
126
-
127
- // Ambil shortlink
128
- const shortLink = await page.inputValue('input[name="shorturl"]');
129
-
130
- // Kirim respons ke client
131
- res.status(200).json({ originalUrl: url, shortLink });
132
- } catch (error) {
133
- console.error('Error during URL shortening:', error);
134
- res.status(500).json({ error: 'Terjadi kesalahan saat membuat shortlink' });
135
- } finally {
136
- // Tutup browser
137
- if (browser) {
138
- await browser.close();
139
- }
140
- }
141
- });
142
-
143
  const PORT = process.env.PORT || 7860;
 
144
 
145
- app.listen(PORT, async () => {
146
- console.log(`Server running on port ${PORT}`);
147
- //await utils.initialize();
148
- });
149
-
150
- process.on('SIGINT', async () => {
151
- //await utils.close();
152
- process.exit(0);
153
- });
 
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
  }
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));