summaryrefslogtreecommitdiff
path: root/includes/refresh.php
blob: 8f9da79d37fb958418f6e3e3ebb882200dd71cbc (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php

$app = json_decode(file_get_contents("./app.json"), true);
$start = microtime(true);
@mkdir("./data");
@mkdir("./data/backup");

$files = [];
$restored = [];
$times = [];

$currentOpStart = microtime(true);
file_put_contents("./data/backup/actions_" . date('Y-m-d_H-i-s') . ".json", @file_get_contents("./data/actions.json"));
file_put_contents("./data/backup/toys_" . date('Y-m-d_H-i-s') . ".json", @file_get_contents("./data/toys.json"));

exec('bash -c "cd /nas/web/ponies.equestria.horse/includes/data/backup; ls -tp toys_* | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"');
exec('bash -c "cd /nas/web/ponies.equestria.horse/includes/data/backup; ls -tp actions_* | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"');

$times["important_backup"] = microtime(true) - $currentOpStart;

$currentOpStart = microtime(true);
$documents = array_map(function ($i) {
    return [
        "id" => substr($i, 0, -5),
        ...(json_decode(file_get_contents("./data/docs/" . $i), true) ?? [])
    ];
}, array_filter(scandir("./data/docs"), function ($i) {
    return !str_starts_with($i, ".") && str_ends_with($i, ".json");
}));

$deletable = array_values(array_filter($documents, function ($i) {
    return strip_tags($i["contents"]) === "/delete";
}));

foreach ($deletable as $item) {
    if (time() - $item["last"]["date"] > 86400) {
        unlink("./data/docs/" . $item["id"] . ".json");
    }
}
$times["docs_cleanup"] = microtime(true) - $currentOpStart;

$currentOpStart = microtime(true);
foreach (array_filter(scandir("./data"), function ($i) {
    return !str_starts_with($i, ".") && $i !== "backup" && is_file("./data/" . $i);
}) as $file) {
    $files[] = $file;
}

@mkdir("./data/backup");
foreach ($files as $file) {
    copy("./data/" . $file, "./data/backup/" . $file);
}

function getSystem(string $id) {
    global $times;
    echo("System: $id\n");

    echo("    Base system info\n");
    $currentOpStart = microtime(true);
    file_put_contents("./data/$id-general.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id"));
    $times["system-general-$id"] = microtime(true) - $currentOpStart;

    echo("    System members\n");
    $currentOpStart = microtime(true);
    file_put_contents("./data/$id-members.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/members"));
    $times["system-members-$id"] = microtime(true) - $currentOpStart;

    echo("    Fronters\n");
    $currentOpStart = microtime(true);
    file_put_contents("./data/$id-fronters.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/fronters"));
    $times["system-fronters-$id"] = microtime(true) - $currentOpStart;

    echo("    Switches\n");
    $currentOpStart = microtime(true);

    echo("        Part 1/3\n");
    $switches1 = json_decode(file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/switches"), true);
    $oldest = $switches1[count($switches1) - 1]["timestamp"];

    echo("        Part 2/3\n");
    $switches2 = json_decode(file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/switches?before=$oldest"), true);
    $oldest = $switches2[count($switches2) - 1]["timestamp"];

    echo("        Part 3/3\n");
    $switches3 = json_decode(file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/switches?before=$oldest"), true);

    file_put_contents("./data/$id-switches.json", json_encode([...$switches1, ...$switches2, ...$switches3], JSON_PRETTY_PRINT));
    $times["system-switches-$id"] = microtime(true) - $currentOpStart;
}

getSystem("gdapd"); // Raindrops
getSystem("ynmuc"); // Cloudburst

echo("Calendar\n");
$currentOpStart = microtime(true);
file_put_contents("./data/calendar.ics", file_get_contents($app["calendar"]));
$times["calendar"] = microtime(true) - $currentOpStart;

echo("Downloading images.\n");
if (!file_exists("./data/images")) mkdir("./data/images");

foreach (json_decode(file_get_contents("./data/gdapd-members.json"), true) as $member) {
    $currentOpStart2 = microtime(true);
    echo("    " . $member['id'] . "\n");

    if (isset($member['avatar_url'])) {
        echo("        Profile picture\n");
        exec("convert -resize 128x128 -quality 50 \"" . $member['avatar_url'] . "\" \"./data/images/pf-gdapd-" . $member['id'] . ".webp\"");
    }

    echo("        Pony Town character\n");
    if (file_exists("../assets/uploads/pt-" . $member['name'] . ".png")) {
        exec("convert -resize 64x64 -quality 50 \"../assets/uploads/pt-" . $member['name'] . ".png\" \"./data/images/pt-gdapd-" . $member['id'] . ".png\"");
    } else {
        exec("convert -resize 64x64 -quality 50 \"../assets/uploads/pt.png\" \"./data/images/pt-gdapd-" . $member['id'] . ".png\"");
    }
    $times["images-gdapd-" . $member['id']] = microtime(true) - $currentOpStart2;
}

foreach (json_decode(file_get_contents("./data/ynmuc-members.json"), true) as $member) {
    $currentOpStart2 = microtime(true);
    echo("    " . $member['id'] . "\n");

    if (isset($member['avatar_url'])) {
        echo("        Profile picture\n");
        exec("convert -resize 128x128 -quality 50 \"" . $member['avatar_url'] . "\" \"./data/images/pf-ynmuc-" . $member['id'] . ".webp\"");
    }

    echo("        Pony Town character\n");
    if (file_exists("../assets/uploads/pt-" . $member['name'] . ".png")) {
        exec("convert -resize 64x64 -quality 50 \"../assets/uploads/pt-" . $member['name'] . ".png\" \"./data/images/pt-ynmuc-" . $member['id'] . ".png\"");
    } else {
        exec("convert -resize 64x64 -quality 50 \"../assets/uploads/pt.png\" \"./data/images/pt-ynmuc-" . $member['id'] . ".png\"");
    }
    $times["images-ynmuc-" . $member['id']] = microtime(true) - $currentOpStart2;
}

$currentOpStart = microtime(true);
function isJson($string): bool {
    json_decode($string);
    return (json_last_error() === JSON_ERROR_NONE);
}

foreach ($files as $file) {
    if (trim(file_get_contents("./data/" . $file)) === "" || (isJson(trim(file_get_contents("./data/" . $file))) && isset(json_decode(trim(file_get_contents("./data/" . $file)), true)["message"])) && !str_ends_with($file, ".html")) {
        $restored[] = $file;
        copy("./data/backup/" . $file, "./data/" . $file);
    }
}
$times["restore"] = microtime(true) - $currentOpStart;

echo("Backing up...\n");

if (date('i') === "00") {
    require_once "./backup.php";
    echo("Backup completed\n");
} else {
    echo("Backup skipped\n");
}

$time = array_sum($times);
echo("Completed in " . $time . " seconds.\n");

file_put_contents("./data/refresh.json", json_encode([
    "timestamp" => microtime(true),
    "duration" => $time,
    "restored" => $restored,
    "times" => $times
]));