File size: 2,157 Bytes
d145b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ed6900
d145b59
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* eslint-disable jsdoc/require-jsdoc */
const axios = require('axios');
const FormData = require('form-data');
const cheerio = require('cheerio');
const fromBuffer = x => import("file-type").then(v => v.fileTypeFromBuffer(x))

async function igdl(url) {
  try {
    const formData = new FormData();
    formData.append('url', url);

    const { data } = await axios.post('https://snapsave.app/id/action.php?lang=id', formData, {
      headers: {
        ...formData.getHeaders(),
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
      }
    });

    // eslint-disable-next-line no-unused-vars 
    const js = await new Promise(r => eval(data.replace('eval', 'r')));
    const raw = js.match(/document\.getElementById\("download-section"\)\.innerHTML\s*=\s*"(.*?)";/s);
    const $ = cheerio.load(raw[1].replace(/\\"/g, '"').replace(/\\n/g, '').replace(/\\t/g, '').replace(/\\\\/g, '\\'));
    
    let thumb = "";
    $("img").each((i, img) => {
      const src = $(img).attr("src")
      thumb = src
    })
    
    let res = {
      status: 200,
      type: "ig",
      title: "",
      thumbnail: thumb,
      video: [],
      image: []
    };
     
     
     let sesi = []
    // Cek setiap link yang ada
    $('.download-items__btn a').each(async (i, element) => {
      const xk = $(element).attr('href');
      sesi.push(xk)
    });
    for (const i of sesi) {
         const response = await axios.get(i, { responseType: 'arraybuffer' });
          const buffer = Buffer.from(response.data);
          const type = await fromBuffer(buffer);
          
          if (type) {
            if (type.mime.startsWith('image')) {
              res.image.push(i);
              if (!res.thumbnail) {
                res.thumbnail = i;
              }
            }
            // Jika file adalah video
            else if (type.mime.startsWith('video')) {
              res.video.push({ resolusi: "HD", url: i, mimetype: type.mime });
            }
          }
    }
    return res;
  } catch (err) {
    console.error("[ERROR IG]\n\n", e);
    return { status: 500, message: err.message};
  }
}
module.exports = igdl;