diff options
| author | moderndevslulw <moderndevslulw@alright.party> | 2025-07-09 19:09:25 +0500 |
|---|---|---|
| committer | moderndevslulw <moderndevslulw@alright.party> | 2025-07-09 19:09:25 +0500 |
| commit | 45c6dc58dfc49f62d58b9c5caad1343287ed0f38 (patch) | |
| tree | 2db88a1212a2dcc36c880d42b1c0244241c22065 | |
| parent | a11beb6de34d8420495cfab4ebbc1e58c39a2ed0 (diff) | |
feat: get kick channels
| -rw-r--r-- | public/channels/index.php | 65 |
1 files changed, 60 insertions, 5 deletions
diff --git a/public/channels/index.php b/public/channels/index.php index e9ef56f..7241a4d 100644 --- a/public/channels/index.php +++ b/public/channels/index.php @@ -24,6 +24,44 @@ function get_twitch_users(string $ids): array return $response['data'] ?? []; } +function get_kick_users(string $ids): array +{ + $client_id = BOT_CONFIG["kick"]["client_id"]; + $client_secret = BOT_CONFIG["kick"]["client_secret"]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "https://id.kick.com/oauth/token?grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret"); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + $response = curl_exec($ch); + $response = json_decode($response, true); + + if (!array_key_exists("access_token", $response)) { + throw new RuntimeException("Failed to get Kick token."); + } + + $token = $response["access_token"]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "https://api.kick.com/public/v1/channels?broadcaster_user_id=$ids"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Authorization: Bearer ' . $token + ]); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + + $response = curl_exec($ch); + $response = json_decode($response, true); + + curl_close($ch); + + return $response['data'] ?? []; +} + $channel = null; $channels = []; @@ -71,11 +109,15 @@ if (isset($_GET['alias_id'])) { $channel['events'] = $stmt->fetchAll(PDO::FETCH_ASSOC); // fetching event names - $twitch_types = [0, 1, 2, 3, 8]; - $channel_ids = []; + $twitch_types = [0, 1, 2, 3, 8, 10, 11, 12, 13, 14, 15]; + $twitch_channel_ids = []; + $kick_types = [4, 5, 6, 7]; + $kick_channel_ids = []; foreach ($channel['events'] as &$e) { if (in_array($e['event_type'], $twitch_types)) { - array_push($channel_ids, $e['name']); + array_push($twitch_channel_ids, $e['name']); + } else if (in_array($e['event_type'], $kick_types)) { + array_push($kick_channel_ids, $e['name']); } $e['event_type_translated'] = match ($e['event_type']) { @@ -99,8 +141,8 @@ if (isset($_GET['alias_id'])) { }; } unset($e); - if (!empty($channel_ids)) { - $event_users = get_twitch_users(implode('&id=', $channel_ids)); + if (!empty($twitch_channel_ids)) { + $event_users = get_twitch_users(implode('&id=', $twitch_channel_ids)); foreach ($event_users as $user) { foreach ($channel['events'] as &$e) { if ($e['name'] == $user['id']) { @@ -110,6 +152,17 @@ if (isset($_GET['alias_id'])) { unset($e); } } + if (!empty($kick_channel_ids)) { + $event_users = get_kick_users(implode('&broadcaster_user_id=', $kick_channel_ids)); + foreach ($event_users as $user) { + foreach ($channel['events'] as &$e) { + if ($e['name'] == $user['broadcaster_user_id']) { + $e['name'] = $user['slug']; + } + } + unset($e); + } + } if ($response = get_twitch_users($channel['alias_id'])) { $user = $response[0]; @@ -234,6 +287,8 @@ if (!empty($channels)) { <td> <?php if (in_array($e['event_type'], $twitch_types)): ?> <a href="https://twitch.tv/<?= $e['name'] ?>"><?= $e['name'] ?></a> + <?php elseif (in_array($e['event_type'], $kick_types)): ?> + <a href="https://kick.com/<?= $e['name'] ?>"><?= $e['name'] ?></a> <?php elseif ($e['event_type'] == 40): ?> <a href="https://github.com/<?= $e['name'] ?>"><?= $e['name'] ?></a> <?php else: ?> |
