forked from YannPl/MetaTwitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoduleStreaming.php
More file actions
107 lines (94 loc) · 3.57 KB
/
moduleStreaming.php
File metadata and controls
107 lines (94 loc) · 3.57 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* GENERAL INFORMATIONS :
* I know that use of globals should be avoided when possible, but here, the aim
* is to have an efficient latency and to reduce the number of requests to the API because
* each request is about 200-500ms. So once data is fetch, no need to do it again and
* it should be accessible from everywhere in the script.
*/
/*******************************************************
******************* SETTINGS **************************
*******************************************************/
$LIST_STREAMS = array("the_kabal", "sucettalanis");
$MAIN_STREAM = "metatrone74";
$URL_IMG_OFFLINE = "/images/Metatrone/Guilde/offline60s.png";
$WIDTH = "100%"; $HEIGHT = "100%";
$AUTO_PLAY = "true";
/*******************************************************
******************* GLOBALS ***************************
*******************************************************/
$LIST_RESULT = array(); // Pas touche à ça Zae! ;)
/*******************************************************
******************* MAIN ALGORITHM ********************
*******************************************************/
$streamOnline = false;
// Here the mainStream is displayed if needed. Else, let's load the others!
if(displayMainStreamIfEnabled()){
$streamOnline = true;
}
// Loop over all streams to display the first one which is online
foreach($LIST_STREAMS as $streamName){
// If no stream displayed yet AND this one is online, display
if(!$streamOnline && isOnline($streamName)){
displayStream($streamName);
$streamOnline = true;
}
// If one stream is already displayed and this one is online too
// Display a link
else if(isOnline($streamName)){
echo '<br/><a href="http://www.twitch.tv/'.$streamName.'" title="'.$streamName.'" class="link_stream">'.$streamName.' est en ligne aussi!</a>';
}
}
if(!$streamOnline){
echo '<img src="'.$URL_IMG_OFFLINE.'" alt="Stream offline" title="Stream Offline"/>';
}
/*******************************************************
******************* UTILITIES FUNCTIONS ***************
*******************************************************/
/**
* If the main stream is online, the player is displayed and true is returned.
* If the main stream is offline, returns false.
*
* @return: <b>true</b> if the main stream is online, <b>false</b> if not.
*/
function displayMainStreamIfEnabled(){
global $MAIN_STREAM;
if(isOnline($MAIN_STREAM)){
displayStream($MAIN_STREAM);
return true;
}
else{
return false;
}
}
/**
* Display the stream specified in parameter.
* @param String $streamName : Name of the stream to display
*/
function displayStream($streamName){
global $HEIGHT, $WIDTH, $AUTO_PLAY;
$search = array('height="xxx"', 'width="xxx"', "auto_play=false");
$replace = array('height="'.$HEIGHT.'"', 'width="'.$WIDTH.'"', "auto_play=$AUTO_PLAY");
echo str_replace($search, $replace, file_get_contents("http://api.justin.tv/api/channel/embed/$streamName?width=xxx&height=xxx"));
}
/**
* Check stream's status and return true or false
* @param String $streamName : Name of the stream
* @return : true or false according to the stream status.
*/
function isOnline($streamName){
global $LIST_RESULT, $MAIN_STREAM;
if(!isset($LIST_RESULT[$streamName])){
getStreamDetail($streamName);
}
return !empty($LIST_RESULT[$streamName]);
}
/**
* Fetch channel informations and store it in the LIST_RESULT global array.
* @param String $streamName
*/
function getStreamDetail($streamName){
global $LIST_RESULT;
$LIST_RESULT[$streamName] = json_decode(file_get_contents("http://api.justin.tv/api/stream/list.json?channel=$streamName", 0, null, null), true);
}
?>