justintv / Twitch-API

A home for details about our API
www.twitch.tv
1.72k stars 379 forks source link

Dashboard stats via API #455

Open jeremyhaile opened 9 years ago

jeremyhaile commented 9 years ago

Is there any way currently or any plans to allow retrieving Twitch dashboard stats via the API?

For example, our customers would like to see their concurrent viewers, time broadcast, time watched, etc. However, we can't find a way to access this via the API.

scagood commented 9 years ago

I've just spent about an hour digging around and this is what I've found:

http://www.twitch.tv/broadcast/stats/get?id=$user&type=$type&month=$timestamp&grouping=$grouping&resolution=$resolution

Parameters

user:
    the username of the user

month:
    unix timestamp of your start date

Types:
    commercials: # of Commercial Breaks
        grouping: none
        resolution: hour || day
    commercial_length: Commercial Break (secs)
        grouping: none
        resolution: hour || day

    chat: Chat Activity
        grouping: none
        resolution: hour || day
    max_concurrents: Max Concurrent Viewers
        grouping: none
        resolution: hour || day
    concurrents: Concurrent Viewers
        grouping: none || host_channel

    follow: Follows
        grouping: none
        resolution: hour || day
    unfollow: Unfollows
        grouping: none
        resolution: hour || day

    time_broadcast: Time Broadcast
        grouping: none
        resolution: hour || day
    time_watched: Time Watched
        grouping: none
        resolution: hour || day

    unique_video_plays: Unique Visitors
        grouping: none || url || player || geo || live
    total_video_plays: Unique Visitors
        grouping: none || url || player || geo || live || host_channel

Example url:

http://www.twitch.tv/broadcast/stats/get?id=scagood&type=concurrents&month=1441090800&grouping=none&resolution=hour

Applied settings: db122bd9b6c8af1f6378b10b736928b3

Note it does require you to send a valid 'persistent' cookie with the request to show that you are logged in.

The 'persistent' cookie is set during the log in process (not the authentication process) and is next to impossible to get easily.

Login process:

https://passport.twitch.tv/authentications/new
https://secure.twitch.tv/passport/callback <-- cookie is set here.
-- Redirected to page where the login request was made from --

How I used php to test this in a cli environment:

<?php
function sendGet($url, $data = array(), $cookies, $decode = true) {
    // Function for checking if a  string is json
    function isJson($string) {
        json_decode($string);
        return (json_last_error() == JSON_ERROR_NONE);
    }
    // Function for building the cookies into the header
    function buildCookies($data) {
        $toReturn = "";
        foreach ($data as $id => $val) {
            $toReturn = $toReturn . $id . "=" . $val . "; ";
        }
        return $toReturn;
    }

    // Append the GET data to the end of the url
    $url = ($data == array()) ? $url : $url . "?" . http_build_query($data);

    // Perform the request
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: " . buildCookies($cookies)));
    curl_setopt($ch, CURLOPT_TCP_NODELAY, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $return = curl_exec($ch);
    curl_close($ch);

    if ($decode && isJson($return))
        $return = json_decode($return, true);

    return $return;
}

$data = array(
    "id" => "scagood",
    "type" => "concurrents",
    "grouping" => "none",
    "resolution" => "day",
    "month" => "1446047979"
);
$cookies = array(
     // Note this is not a working value :)
    "persistent" => "29490556%3A490178b4d38c7a02c0693f61a9%3A3641f6c1f1679ddf075324c697"
);

$data = sendGet("http://www.twitch.tv/broadcast/stats/get", $data, $cookies, false);
?>

Hope this helps sca

jeremyhaile commented 9 years ago

@scagood - thanks for the info. That's very useful.

However, we currently have our customers go through an oauth flow, and we don't ask them for usernames/passwords. Which means we don't have access to a persistent cookie.

I assume there's no way to get this information without a username/password and performing a login on behalf of the user? (i.e. no way to get it with an oauth access token)

cpoetter commented 7 years ago

I am also very interested! I am trying now for a day to somehow access the Stats via an OAuth2 API call.

scagood commented 7 years ago

Unfortunately there are no methods that I can think of to allow you to simply request that data using the twitch api. I assume you've checked out the v5 documentation (https://dev.twitch.tv/docs/).

The only way I can think of you getting those stats are by either gathering them yourself or (dont do this next one) getting the user's username and password then page scraping the raw stats, this is a very bad idea and SHOULD NOT be implemented.

Good luck, sca