summaryrefslogtreecommitdiff
path: root/public/generate.php
blob: 6226c8f43994601fe3be0cb2041cddf447a457ab (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
<?php
include_once '../src/message_picture.php';

if (!isset($_GET['message_id'], $_GET['channel_login'])) {
    http_response_code(400);
    exit('message_id and channel_login must be set in query string!');
}

$message_id = $_GET['message_id'];
$channel_login = $_GET['channel_login'];

// obtaining user data and message
$request = curl_init();
curl_setopt($request, CURLOPT_URL, "https://recent-messages.robotty.de/api/v2/recent-messages/{$channel_login}?hide_moderation_messages=true&clearchat_to_notice=true");
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($request);
if (curl_errno($request)) {
    echo 'cURL Error: ' . curl_error($request);
}
curl_close($request);

$recent_messages = json_decode($response, true);

if (isset($recent_messages['error'])) {
    http_response_code(400);
    exit("Failed to retrieve messages from recent-messages API: {$recent_messages["error"]}");
}

// searching the message
$username = "";
$message = "";
$timestamp = 0;

foreach ($recent_messages['messages'] as $m) {
    $parts = explode(' ', $m);

    // searching message by id
    $flags = $parts[0];
    if (str_starts_with($flags, '@')) {
        $flags = substr($flags, 1);
    }
    $flags = explode(';', $flags);

    $found = false;

    foreach ($flags as $f) {
        $ff = explode('=', $f);
        if ($ff[0] == 'id' && $ff[1] == $message_id) {
            $found = true;
        } else if ($ff[0] == 'tmi-sent-ts') {
            $timestamp = intval($ff[1]);
        }
    }

    if (!$found) {
        continue;
    }

    $username = explode('!', $parts[1]);
    $username = substr($username[0], 1);

    array_splice($parts, 0, 4);
    $message = implode(' ', $parts);

    if (str_starts_with($message, ':')) {
        $message = substr($message, 1);
    }

    break;
}

if (empty($username) || empty($message)) {
    http_response_code(404);
    exit("Message ID {$message_id} not found");
}

// getting user pfp
$request = curl_init();
curl_setopt($request, CURLOPT_URL, "https://api.ivr.fi/v2/twitch/user?login={$username}");
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($request);
curl_close($request);

$user = json_decode($response, true);

if (isset($user['error'])) {
    http_response_code(400);
    $m = $user['error']['message'] ?? "unknown";
    exit("Failed to retrieve a user from IVR API: {$m}");
}

if (empty($user)) {
    http_response_code(404);
    exit("User {$username} doesn't exist in IVR API");
}

$old_pfp = imagecreatefromstring(file_get_contents($user[0]['logo']));

$oldw = imagesx($old_pfp);
$oldh = imagesy($old_pfp);
$pfp = imagecreatetruecolor(72, 72);
imagecopyresampled($pfp, $old_pfp, 0, 0, 0, 0, 72, 72, $oldw, $oldh);

$screenshot = generate_message_screenshot(
    $pfp,
    $username,
    $channel_login,
    $message,
    $timestamp / 1000
);

imagedestroy($pfp);
imagedestroy($old_pfp);

header('Content-Type: image/jpeg');
header("Content-Disposition: inline; filename*=UTF-8''message-{$username}-{$message_id}-{$channel_login}.jpg");
imagejpeg($screenshot);
imagedestroy($screenshot);