File size: 1,710 Bytes
a4cf109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 cheerio = require("cheerio");
const { domain, fetchOptions } = require("./common");
const fetch = require("node-fetch");

module.exports = function (args, socket) {
    const profile = fetch(domain + "/papi/user/dstats", fetchOptions).then(res => res.json()).then(json => {
        if (!json) return;

        return ({
            today_download: json.dailyDownloads + "/" + json.dailyDownloadsLimit
        });
    })
        .catch(err => {
            socket.write("ERR: 1 " + err + "\n");
            socket.end();
        });

    const history = fetch(domain + "/users/dstats.php?today", fetchOptions).then(res => res.text()).then(html => {
        const $ = cheerio.load(html);
        const rows = $(".dstats-row");
        if (!rows.length) return;

        return rows.get().map(row => {
            const a = $(row).find("a");
            if (!a) return undefined;

            let url = a.attr("href");
            let name = a.text().trim();
            if (!url || !name) return undefined;

            if (url[0] !== "/") url = "/" + url;

            return ({ url, name });
        })
            .filter(a => a);
    })
        .catch(err => {
            socket.write("ERR: 1 " + err + "\n");
            socket.end();
        });

    Promise.all([profile, history]).then(results => {
        if (!results || results.length < 2) return;

        const final = results[0];
        final.today_list = results[1];
        socket.write(JSON.stringify(final));
        socket.write("\n");
        socket.end();
    })
        .catch(err => {
            socket.write("ERR: 1 " + err + "\n");
            socket.end();
        })

}