File size: 1,459 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
/* eslint-disable jsdoc/require-jsdoc */
const axios = require("axios");
const { JSDOM } = require("jsdom");

async function spotlight(url) {
  try {
    const { data } = await axios.get(url, {
      headers: {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
      }
    });

    const dom = new JSDOM(data);
    const scripts = dom.window.document.querySelectorAll('script[type="application/ld+json"]');

    let jsonData = null;
    scripts.forEach(script => {
      try {
        const parsedData = JSON.parse(script.textContent);
        if (parsedData["@context"] === "https://schema.org") {
          jsonData = {
            status: 200,
            thumbnail: parsedData["@graph"][0].thumbnailUrl,
            title: parsedData["@graph"][0].name,
            author: parsedData["@graph"][0].creator,
            video: [{
              resolusi: "Watermark",
              url: parsedData["@graph"][0].contentUrl
            }]
          }
        }
      } catch {
        /* empty */
      }
    });

    if (jsonData) {
      console.log("JSON ditemukan:");
      console.log(JSON.stringify(jsonData, null, 2));
    } else {
      console.log("Tidak ditemukan data JSON yang sesuai.");
    }
  } catch (error) {
    return { status: 500, message: error.message}
    console.error("Gagal mengambil data:", error.message);
  }
}

module.exports = { spotlight };