summaryrefslogtreecommitdiff
path: root/includes/external/addressbook/website.js
blob: 48aa072a000d6fd841fe5a8fd66dc741420c2db3 (plain)
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
const axios = require('axios');
const ogs = require('open-graph-scraper');

function sleep(ms) {
    return new Promise((res) => {
        setTimeout(res, ms);
    });
}

(async () => {
    let user = process.argv[2];

    try {
        let url = new URL(user);
        let data = (await ogs({ url: user }));

        if (data.error) throw data.error;
        if (!data.result.success) throw new Error("Failed");

        let obj = {
            error: null,
            avatar: data.result.favicon.startsWith("/") ? url.origin + data.result.favicon : ((data.result.favicon.startsWith("http:") || data.result.favicon.startsWith("https:")) ? data.result.favicon : data.result.requestUrl + "/" + data.result.favicon),
            name: data.result.ogTitle,
            description: url.host + (url.pathname === "/" ? "" : url.pathname),
            link: data.result.requestUrl,
            copy: [
                {
                    title: "Copy URL",
                    text: data.result.requestUrl
                },
                {
                    title: "Copy URL to favicon",
                    text: data.result.favicon.startsWith("/") ? url.origin + data.result.favicon : ((data.result.favicon.startsWith("http:") || data.result.favicon.startsWith("https:")) ? data.result.favicon : data.result.requestUrl + "/" + data.result.favicon)
                },
                {
                    title: "Copy home URL",
                    text: url.origin
                },
                {
                    title: "Copy page title",
                    text: data.result.ogTitle
                }
            ]
        }

        console.log(JSON.stringify(obj, null, 2));
    } catch (e) {
        console.log(JSON.stringify({
            error: e,
            avatar: "https://img.icons8.com/fluency-systems-regular/64/ffffff/globe.png",
            name: user,
            description: "Website",
            link: null,
            copy: []
        }, null, 2));
    }
})();