wudysoft commited on
Commit
7e6bac7
·
verified ·
1 Parent(s): 5743bb5

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +31 -79
app.js CHANGED
@@ -28,86 +28,38 @@ app.get('/mediafire', async (req, res) => {
28
  }
29
  });
30
 
31
- // Function to handle MediaFire download info extraction
32
  async function mediafire(url) {
33
- const hasParams = url.includes('dkey') && url.includes('r=');
34
-
35
- if (hasParams) {
36
- const strng = await data(url);
37
- return parseResultToJson(strng);
38
- } else {
39
- const firstResult = await data(url);
40
- const urlLink = extractDownloadLink(firstResult);
41
- const fetching = await data(urlLink);
42
- return parseResultToJson(fetching);
43
- }
44
- }
45
-
46
- // Extract the download link from the result string
47
- function extractDownloadLink(result) {
48
- const regex = /downloadLink:\s*'([^']+)'/;
49
- const match = result.match(regex);
50
- return match ? match[1] : null;
51
- }
52
-
53
- // Parse result string into JSON format
54
- function parseResultToJson(resultString) {
55
- const jsonResult = {};
56
- const fileNameRegex = /fileName:\s*'([^']+)'/;
57
- const downloadLinkRegex = /downloadLink:\s*'([^']+)'/;
58
- const fileSizeRegex = /fileSize:\s*'([^']+)'/;
59
-
60
- const fileNameMatch = resultString.match(fileNameRegex);
61
- const downloadLinkMatch = resultString.match(downloadLinkRegex);
62
- const fileSizeMatch = resultString.match(fileSizeRegex);
63
-
64
- if (fileNameMatch) {
65
- jsonResult.fileName = fileNameMatch[1];
66
- }
67
- if (downloadLinkMatch) {
68
- jsonResult.downloadLink = downloadLinkMatch[1];
69
- }
70
- if (fileSizeMatch) {
71
- jsonResult.fileSize = fileSizeMatch[1];
72
- }
73
-
74
- return jsonResult;
75
- }
76
-
77
- // Function to fetch the MediaFire page and extract data
78
- async function data(url) {
79
- const browser = await chromium.launch({ headless: true });
80
- const context = await browser.newContext({
81
- userAgent: 'Mozilla/5.0 (Linux; Android 6.0; iris50) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36'
82
- });
83
- const page = await context.newPage();
84
-
85
- try {
86
- await page.goto(url);
87
- const downloadInfo = await page.evaluate(() => {
88
- const fileNameElement = document.querySelector('.dl-btn-label');
89
- const fileName = fileNameElement ? fileNameElement.textContent.trim() : '';
90
- const downloadLinkElement = document.querySelector('#downloadButton');
91
- const downloadLink = downloadLinkElement ? downloadLinkElement.href : '';
92
- const fileSizeText = downloadLinkElement ? downloadLinkElement.textContent : '';
93
- const sizeMatch = fileSizeText.match(/\$([^)]+)\$/);
94
- const fileSize = sizeMatch ? sizeMatch[1] : '';
95
-
96
- return {
97
- fileName: fileName,
98
- downloadLink: downloadLink,
99
- fileSize: fileSize
100
- };
101
- });
102
-
103
- return downloadInfo;
104
- } catch (error) {
105
- console.error('Error:', error);
106
- throw new Error('Failed to fetch or parse the MediaFire page');
107
- } finally {
108
- await browser.close();
109
- }
110
- }
111
 
112
  const PORT = process.env.PORT || 7860;
113
 
 
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
+ try {
38
+ await page.goto(url);
39
+ const downloadInfo = await page.evaluate(() => {
40
+ const fileNameElement = document.querySelector('.dl-btn-label');
41
+ const fileName = fileNameElement ? fileNameElement.textContent.trim() : '';
42
+ const downloadLinkElement = document.querySelector('#downloadButton');
43
+ const downloadLink = downloadLinkElement ? downloadLinkElement.href : '';
44
+ const fileSizeText = downloadLinkElement ? downloadLinkElement.textContent : '';
45
+ const sizeMatch = fileSizeText.match(/\\$([^)]+)\\$/);
46
+ const fileSize = sizeMatch ? sizeMatch[1] : '';
47
+
48
+ return {
49
+ fileName: fileName,
50
+ downloadLink: downloadLink,
51
+ fileSize: fileSize
52
+ };
53
+ });
54
+
55
+ return downloadInfo;
56
+ } catch (error) {
57
+ console.error("Error:", error.response ? error.response.data : error.message);
58
+ return { success: false, message: error.message };
59
+ } finally {
60
+ await browser.close();
61
+ }
62
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  const PORT = process.env.PORT || 7860;
65