Get recently streamed games

I was looking for a way to get a list of the 5 most recently played games for a stream but could not find anything.

Here is what I did. :blush:

I made a script that pulls data from channels/videos of the 30 most recent streams.

Now that we have a list of 30 videos we need to do something with it.

First: We get the data from the 30 most recent streams.
Second: We make an array of the games.
Third: We sort the games in the array from most streamed to least streamed.

Here is a DEMO of the script in action: http://basement-developers.com/projects/xiceman/games.php
The game data is brought in using a different api.

<?php
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://api.twitch.tv/kraken/channels/{CHANNEL_ID]/videos?limit=30');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Client-ID: [CLIENT_ID]",
    "Accept: application/vnd.twitchtv.v5+json",
    "Authorization: OAuth [ACESS_TOKEN]"
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // ONLY USE FOR DEVELOPMENT WITHOUT SSL

$result = curl_exec($ch);

curl_close($ch);

$data = json_decode($result, true);

$games = array(); // Creates empty array for games
foreach($data['videos'] as $item) {
    if ($item['game'] != ''){ // Checks if video has game name. If not it is ignored.
        $games[] =  $item['game'];
    }
}

$gc = array_count_values($games); // This will is counts the number of times game name is displayed.

arsort($gc); // This will sort the array of from most to least.

var_dump($gc); // Here you can see the array. Should look something like this: array(5) { ["Atlas"]=> int(10) ["Tom Clancy's Rainbow Six: Siege"]=> int(8) ["Arma 3"]=> int(7) ["Minecraft"]=> int(3) ["Grand Theft Auto V"]=> int(2) }

$i = 1;
foreach($gc as $key => $value) {
    echo 'Game: '.$key.' - '.$key; // This will output a list of games with the [GameName] - [Number]

    if ($i++ == 10) break; // Limits the number of games that will be displayed.
}

You can edit the limit in the URL to how many videos you pull data from.
You can edit number of games displayed by changing $i++ == 10 to what ever you want.

NOTE: The list will only give you the games that are listed in the video data. If you only played 1 game for the past 30 streams, You will only get that game in return.

Have any questions or need any help just ask. All input is welcome.

How helpful was my post?

  • 1
  • 2
  • 3
  • 4
  • 5

0 voters

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.