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

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +66 -8
app.js CHANGED
@@ -39,10 +39,12 @@ async function mediafire(url) {
39
  await page.goto(url);
40
 
41
  let downloadInfo = await page.evaluate(() => {
42
- const fileName = document.querySelector('.dl-btn-label')?.textContent?.trim() || '';
43
- const downloadLink = document.querySelector('#downloadButton')?.href || '';
44
- const fileSizeText = document.querySelector('#downloadButton')?.textContent || '';
45
- const sizeMatch = fileSizeText.match(/\$([^)]+)\$/);
 
 
46
  const fileSize = sizeMatch ? sizeMatch[1] : '';
47
 
48
  // Ambil informasi meta
@@ -61,16 +63,25 @@ async function mediafire(url) {
61
  };
62
  });
63
 
64
- // Jika tautan unduhan tidak valid, buka tautan baru
65
  if (!downloadInfo.downloadLink.startsWith('https://down')) {
66
- await page.goto(downloadInfo.downloadLink);
67
 
68
- const updatedInfo = await page.evaluate(() => {
 
 
 
 
 
 
 
 
69
  const downloadLink = document.querySelector('#downloadButton')?.href || '';
70
  return { downloadLink };
71
  });
72
 
73
  downloadInfo.downloadLink = updatedInfo.downloadLink;
 
74
  }
75
 
76
  return downloadInfo;
@@ -78,10 +89,57 @@ async function mediafire(url) {
78
  console.error('Error:', error.message);
79
  return { success: false, message: error.message };
80
  } finally {
81
- await browser.close();
 
 
82
  }
83
  }
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  const PORT = process.env.PORT || 7860;
86
 
87
  app.listen(PORT, async () => {
 
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
 
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;
 
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 () => {