summaryrefslogtreecommitdiff
path: root/includes/external/addressbook/reddit.js
blob: 3e5dd159c4f362e95d12dbc04687fadf37962b41 (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
58
59
60
61
62
63
const axios = require('axios');

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

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

    if (user.startsWith("u/")) user = user.substring(2);
    if (user.startsWith("user/")) user = user.substring(5);

    try {
        let data = (await axios.get(`https://www.reddit.com/user/${user}/about.json`)).data.data;

        if (data['icon_img']) data['icon_img'] = data['icon_img'].replaceAll("&", "&");

        let obj = {
            error: null,
            avatar: "https://img.icons8.com/fluency-systems-regular/64/ffffff/reddit.png",
            name: data['subreddit']['title'] ?? data['name'],
            description: data['subreddit']['display_name_prefixed'],
            link: `https://www.reddit.com${data['subreddit']['url']}`,
            copy: [
                {
                    title: "Copy link to profile",
                    text: `https://www.reddit.com${data['subreddit']['url']}`
                },
                {
                    title: "Copy username",
                    text: data['username']
                },
                {
                    title: "Copy ID",
                    text: data['id']
                },
                data['icon_img'] ? {
                    title: "Copy avatar URL",
                    text: data['icon_img']
                } : null
            ]
        }

        if (data['icon_img']) {
            obj.avatar = data['icon_img'];
        }

        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/reddit.png",
            name: user,
            description: "Reddit",
            link: null,
            copy: []
        }, null, 2));
    }


})();