summaryrefslogtreecommitdiff
path: root/includes/external/addressbook/twitter.js
blob: d8834ca4e712786d0ecc6d9335ab77d3d8707a64 (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
64
const axios = require('axios');
const apiVersion = "2";
const app = require('../../app.json');

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

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

    try {
        let data = (await axios.get(`https://api.twitter.com/${apiVersion}/users/${user}?user.fields=id,description,profile_image_url`, {
            headers: {
                Authorization: `Bearer ${app.twitter.bearer}`
            }
        })).data.data;

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

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

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


})();