diff options
| author | ilotterytea <iltsu@alright.party> | 2025-06-11 00:58:43 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-06-11 00:58:43 +0500 |
| commit | 3415d5f5abb90cba225af9fbb9eed4349abe3cd1 (patch) | |
| tree | b33ac6def1d2f843bd4092d593ffd0ce24a99212 | |
| parent | 2536d0db7edf5069bc2aa22493a7594134eac72a (diff) | |
feat: screenshot twitch messages
| -rw-r--r-- | public/generate.php | 106 |
1 files changed, 100 insertions, 6 deletions
diff --git a/public/generate.php b/public/generate.php index ba7883a..6226c8f 100644 --- a/public/generate.php +++ b/public/generate.php @@ -1,7 +1,101 @@ <?php include_once '../src/message_picture.php'; -$old_pfp = imagecreatefrompng('./pfp.png'); +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); @@ -9,16 +103,16 @@ imagecopyresampled($pfp, $old_pfp, 0, 0, 0, 0, 72, 72, $oldw, $oldh); $screenshot = generate_message_screenshot( $pfp, - "ilotterytea", - "forsen", - "Lorem ipsum dolor sit amet consectetur adipisicing elit. Quos sed quae minus libero odit illum atque itaque nulla! Ut quia fuga excepturi saepe nemo magni exercitationem aperiam commodi maxime ab.", - time() + $username, + $channel_login, + $message, + $timestamp / 1000 ); imagedestroy($pfp); imagedestroy($old_pfp); header('Content-Type: image/jpeg'); -header("Content-Disposition: inline; filename*=UTF-8''someshit.jpg"); +header("Content-Disposition: inline; filename*=UTF-8''message-{$username}-{$message_id}-{$channel_login}.jpg"); imagejpeg($screenshot); imagedestroy($screenshot);
\ No newline at end of file |
