/
twitch.php
52 lines (46 loc) · 1.66 KB
/
twitch.php
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
<?
// CONFIG
$channel_names = array("ceh9", "cheatbanned", "starladder5", "sharishaxd", "dreamhackcs", "eleaguetv");
define("CLIENT_ID", "o3tvos5r70b1u3njfhm2uz8rsrb6n9x");
// CONFIG
header("Content-Type:text/plain;charset=utf-8");
$res = "#EXTM3U\n";
foreach($channel_names as $item) {
$status = getStreams($item);
if(!$status)
$res .= "#EXTINF:-1 mpeg4," . $item."(offline)\nhttp://offline\n";
else
$res .= "#EXTINF:-1 mpeg4," . $item. " [". $status[0]["res"] . " " . $status[0]["bw"]."Mbit/s] " . "\n" . $status[0]["stream"] . "\n";
}
echo $res;
function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
function getStreams($channel_name)
{
$token_content = json_decode(file_get_contents("http://api.twitch.tv/api/channels/".$channel_name."/access_token?client_id=".CLIENT_ID));
$token = $token_content->token;
$sig = $token_content->sig;
$random = rand(0, 10000000);
$url_streams = "http://usher.twitch.tv/api/channel/hls/$channel_name.m3u8?player=twitchweb&token=$token&sig=$sig&\$allow_audio_only=true&allow_source=true&type=any&p=$random";
if(get_http_response_code($url_streams) != 200)
return false;
else
{
$streams = explode("\n", file_get_contents($url_streams));
$streams_res = array();
foreach($streams as $key => $value)
{
if(stripos($value, "#EXT-X-STREAM-INF") !== false)
{
$info = explode(",", $value);
$pl = explode("\"", $info[3]);
$bw = explode("=", $info[1]);
$res = explode("x", $info[2]);
array_push($streams_res, array("name"=> $pl[1], "res"=> $res[1] . "p", "bw" => round($bw[1] / 1048576, 1), "stream" => $streams[$key + 1]));
}
}
return $streams_res;
}
}