Nexchan commited on
Commit
fdf2e1b
1 Parent(s): 9e2045b

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +87 -97
index.js CHANGED
@@ -12,45 +12,41 @@ app.use('/temp', express.static(tempDir));
12
  app.use(express.json());
13
 
14
  app.get("/", (req, res) => {
15
- res.type("json");
16
- const keluaran = {
17
- success: true,
18
- author: "Nex",
19
- data: {
20
- igdl: "/igdl"
21
- },
22
- };
23
- res.send(keluaran);
24
  });
25
 
26
  async function downloadImage(url) {
27
- try {
28
- const response = await axios.get(url, {
29
- responseType: 'arraybuffer'
30
- });
31
- const randomCode = Math.random().toString(36).substring(7);
32
- const imagePath = `downloaded_image_${randomCode}.png`;
33
- fs.writeFileSync(path.join(tempDir, "/" + imagePath), Buffer.from(response.data, 'binary'));
34
- return imagePath;
35
- } catch (error) {
36
- console.error('Error downloading image:', error.message);
37
- throw error;
38
- }
39
  }
40
 
41
  async function downloadVideo(url) {
42
- try {
43
- const response = await axios.get(url, {
44
- responseType: 'arraybuffer'
45
- });
46
- const randomCode = Math.random().toString(36).substring(7);
47
- const videoPath = `downloaded_video_${randomCode}.mp4`;
48
- fs.writeFileSync(path.join(tempDir, "/" + videoPath), Buffer.from(response.data, 'binary'));
49
- return videoPath;
50
- } catch (error) {
51
- console.error('Error downloading video:', error.message);
52
- throw error;
53
- }
54
  }
55
 
56
  const generateRandomIP = () => {
@@ -271,74 +267,68 @@ async function igdl4(url) {
271
  }
272
 
273
  const getInstagramDownloadLinks = async (url) => {
274
- let result = await igdl1(url);
275
- if (!result) {
276
- result = await igdl2(url);
277
- }
278
- if (!result) {
279
- result = await igdl3(url);
280
- }
281
- if (!result) {
282
- result = await igdl4(url);
283
- }
284
- if (!result) {
285
- result = {
286
- message: "all server error"
287
- };
288
- }
289
- return result;
290
  };
291
 
292
  app.get('/igdl', async (req, res) => {
293
- try {
294
- const {
295
- url
296
- } = req.query;
297
- if (!url) {
298
- return res.status(400).json({
299
- error: 'Parameter url is required'
300
- });
301
- }
302
- if (!/https?:\/\/(www\.)?instagram\.com\/(p|reel|tv)/.test(url)) {
303
- return res.status(400).json({
304
- error: "Example: https://www.instagram.com/p/Cz1fTwMJFpx/?igsh=MXRrY2g4eWNucGoyZg=="
305
- });
306
- }
307
- let result = await getInstagramDownloadLinks(url);
308
- let result_upload = {
309
- title: result?.title || 'untitled',
310
- media: []
311
- }
312
 
313
-
314
- for (let item of result.urls) {
315
- if (item.type === "image") {
316
- let unduh = await downloadImage(item.url)
317
- result_upload.media.push({
318
- type: item.type,
319
- path: unduh,
320
- url_path: `http://${process.env.SPACE_HOST}/temp/${path.basename(unduh)}`
321
- })
322
- } else if (item.type === "video") {
323
- let unduh = await downloadVideo(item.url)
324
- result_upload.media.push({
325
- type: item.type,
326
- path: unduh,
327
- url_path: `http://${process.env.SPACE_HOST}/temp/${path.basename(unduh)}`
328
- })
329
- }
330
- }
331
-
332
- res.json(result_upload);
333
- } catch (error) {
334
- console.error('Error processing request:', error);
335
- res.status(500).json({
336
- error: 'Failed to process request\n' + error
337
- });
338
- }
339
  });
340
 
341
- const hostname = `http://${process.env.SPACE_HOST}`; // Ganti dengan host yang sesuai
 
342
  app.listen(PORT, () => {
343
- console.log(`Server is running on http://${hostname}:${PORT}`);
344
- });
 
12
  app.use(express.json());
13
 
14
  app.get("/", (req, res) => {
15
+ res.type("json");
16
+ const keluaran = {
17
+ success: true,
18
+ author: "Nex",
19
+ data: {
20
+ igdl: "/igdl"
21
+ },
22
+ };
23
+ res.send(keluaran);
24
  });
25
 
26
  async function downloadImage(url) {
27
+ try {
28
+ const response = await axios.get(url, { responseType: 'arraybuffer' });
29
+ const randomCode = Math.random().toString(36).substring(7);
30
+ const imagePath = `downloaded_image_${randomCode}.png`;
31
+ fs.writeFileSync(path.join(tempDir, "/" + imagePath), Buffer.from(response.data, 'binary'));
32
+ return imagePath;
33
+ } catch (error) {
34
+ console.error('Error downloading image:', error.message);
35
+ throw error;
36
+ }
 
 
37
  }
38
 
39
  async function downloadVideo(url) {
40
+ try {
41
+ const response = await axios.get(url, { responseType: 'arraybuffer' });
42
+ const randomCode = Math.random().toString(36).substring(7);
43
+ const videoPath = `downloaded_video_${randomCode}.mp4`;
44
+ fs.writeFileSync(path.join(tempDir, "/" + videoPath), Buffer.from(response.data, 'binary'));
45
+ return videoPath;
46
+ } catch (error) {
47
+ console.error('Error downloading video:', error.message);
48
+ throw error;
49
+ }
 
 
50
  }
51
 
52
  const generateRandomIP = () => {
 
267
  }
268
 
269
  const getInstagramDownloadLinks = async (url) => {
270
+ let result = await igdl1(url);
271
+ if (!result) {
272
+ result = await igdl2(url);
273
+ }
274
+ if (!result) {
275
+ result = await igdl3(url);
276
+ }
277
+ if (!result) {
278
+ result = await igdl4(url);
279
+ }
280
+ if (!result) {
281
+ result = {
282
+ message: "all server error"
283
+ };
284
+ }
285
+ return result;
286
  };
287
 
288
  app.get('/igdl', async (req, res) => {
289
+ try {
290
+ const { url } = req.query;
291
+ if (!url) {
292
+ return res.status(400).json({ error: 'Parameter url is required' });
293
+ }
294
+ if (!/https?:\/\/(www\.)?instagram\.com\/(p|reel|tv)/.test(url)) {
295
+ return res.status(400).json({ error: "Example: https://www.instagram.com/p/Cz1fTwMJFpx/?igsh=MXRrY2g4eWNucGoyZg==" });
296
+ }
297
+ let result = await getInstagramDownloadLinks(url);
298
+ let result_upload = {
299
+ title: result?.title || 'untitled',
300
+ media: []
301
+ }
 
 
 
 
 
 
302
 
303
+ for (let item of result.urls) {
304
+ if (item.type === "image") {
305
+ let unduh = await downloadImage(item.url);
306
+ result_upload.media.push({ type: item.type, path: unduh, url_path: `http://${process.env.SPACE_HOST}/temp/${path.basename(unduh)}` });
307
+ } else if (item.type === "video") {
308
+ let unduh = await downloadVideo(item.url);
309
+ result_upload.media.push({ type: item.type, path: unduh, url_path: `http://${process.env.SPACE_HOST}/temp/${path.basename(unduh)}` });
310
+ }
311
+ }
312
+
313
+ res.json(result_upload);
314
+
315
+ for (let item of result_upload.media) {
316
+ try {
317
+ await new Promise(resolve => setTimeout(resolve, 10 * 60 * 1000)); // 10 minutes
318
+ await fs.unlink(item.path);
319
+ console.log(`File ${item.path} deleted.`);
320
+ } catch (error) {
321
+ console.error(`Error deleting file ${item.path}:`, error);
322
+ }
323
+ }
324
+ } catch (error) {
325
+ console.error('Error processing request:', error);
326
+ res.status(500).json({ error: 'Failed to process request\n' + error});
327
+ }
 
328
  });
329
 
330
+
331
+ const hostname = `http://${process.env.SPACE_HOST}`; // Ganti dengan host yang sesuai
332
  app.listen(PORT, () => {
333
+ console.log(`Server is running on http://${hostname}:${PORT}`);
334
+ });