-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetuserschedule.php
194 lines (149 loc) · 7.66 KB
/
getuserschedule.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
require_once(__DIR__ . '/../config/config.php');
$ical = isset($_GET['ical']) ? $_GET['ical'] : '';
$html = isset($_GET['html']) ? $_GET['html'] : '';
$limit = isset($_GET['limit']) ? $_GET['limit'] : 100;
$userEventsArray = array();
$userHtmlResponse = array();
if ($limit > 100) {
$limit = 100;
}
$headers = [
'Authorization: Bearer ' . AUTH_TOKEN,
'Client-Id: ' . CLIENT_ID
];
if (isset($_GET['channel']) || isset($_GET['id'])) {
$ch = curl_init();
//Get user id and info
if (isset($_GET['channel'])) {
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/users?login=" . trim(strtolower(str_replace('@', '', $_GET['channel']))));
} elseif (isset($_GET['id'])) {
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/users?id=" . trim($_GET['id']));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$userInfo = curl_exec($ch);
$userStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$userResult = json_decode($userInfo, true);
if (isset($ical) && $ical == "true" && $userStatus == 200 && count($userResult['data']) > 0) {
// Get user ical data
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/schedule/icalendar?broadcaster_id=" . $userResult['data'][0]['id']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$userIcalResponse = curl_exec($ch);
header('Content-type: text/calendar');
echo $userIcalResponse;
} elseif (isset($html) && $html == "true" && $userStatus == 200 && count($userResult['data']) > 0) {
// Get user schedule as html
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/schedule?broadcaster_id=" . $userResult['data'][0]['id']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$userHtmlResponse1 = curl_exec($ch);
$userHtmlResponseArray1 = json_decode($userHtmlResponse1, true);
if (is_array($userHtmlResponseArray1['pagination']) && count($userHtmlResponseArray1['data']['segments']) > 0 && $userHtmlResponseArray1['pagination']['cursor'] > "") {
// Gets the next 20 items from schedule
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/schedule?broadcaster_id=" . $userResult['data'][0]['id'] . "&after=" . $userHtmlResponseArray1['pagination']['cursor']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$userHtmlResponse2 = curl_exec($ch);
$userHtmlResponseArray2 = json_decode($userHtmlResponse2, true);
}
if (is_array($userHtmlResponseArray2['pagination']) && count($userHtmlResponseArray2['data']['segments']) > 0 && $userHtmlResponseArray2['pagination']['cursor'] > "") {
// Gets the next 20 items from schedule
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/schedule?broadcaster_id=" . $userResult['data'][0]['id'] . "&after=" . $userHtmlResponseArray2['pagination']['cursor']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$userHtmlResponse3 = curl_exec($ch);
$userHtmlResponseArray3 = json_decode($userHtmlResponse3, true);
}
if (is_array($userHtmlResponseArray3['pagination']) && count($userHtmlResponseArray3['data']['segments']) > 0 && $userHtmlResponseArray3['pagination']['cursor'] > "") {
// Gets the next 20 items from schedule
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/schedule?broadcaster_id=" . $userResult['data'][0]['id'] . "&after=" . $userHtmlResponseArray3['pagination']['cursor']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$userHtmlResponse4 = curl_exec($ch);
$userHtmlResponseArray4 = json_decode($userHtmlResponse4, true);
}
if (is_array($userHtmlResponseArray4['pagination']) && count($userHtmlResponseArray4['data']['segments']) > 0 && $userHtmlResponseArray4['pagination']['cursor'] > "") {
// Gets the next 20 items from schedule
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/schedule?broadcaster_id=" . $userResult['data'][0]['id'] . "&after=" . $userHtmlResponseArray4['pagination']['cursor']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$userHtmlResponse5 = curl_exec($ch);
$userHtmlResponseArray5 = json_decode($userHtmlResponse5, true);
}
// merge all array data into one array
$userHtmlResponse = array_merge(
(array)$userHtmlResponseArray1['data']['segments'],
(array)$userHtmlResponseArray2['data']['segments'],
(array)$userHtmlResponseArray3['data']['segments'],
(array)$userHtmlResponseArray4['data']['segments'],
(array)$userHtmlResponseArray5['data']['segments']
);
$userEventsArray = $userHtmlResponse;
$htmlEventContents = "";
$cnt = 0;
if (is_array($userEventsArray) && count($userEventsArray) > 0) {
foreach ($userEventsArray as $event) {
$cnt++;
$title = trim($event['title']);
$start = trim($event['start_time']);
if (!empty($title)) {
$htmlEventContents .= "<div class='event item_" . $cnt . "'>";
$htmlEventContents .= "<span class='start_date'>" . $start . "</span>";
$htmlEventContents .= "<span class='title'>" . $title . "</span>";
$htmlEventContents .= "</div>";
}
if ($cnt == $limit) {
break;
}
}
} else {
// exit - do nothing else
die();
}
// print html page
header('Content-type: text/html');
echo "<!DOCTYPE html>";
echo "<html>";
echo "<head>";
echo "<meta charset='UTF-8'>";
echo "<link rel='stylesheet' href='/assets/styles.css?cachebust=oL8nT1pQ5qI7gC8r'>";
echo "</head>";
echo "<body>";
echo "<div id='container'>";
echo $htmlEventContents;
echo "</div>";
echo "<script src='/assets/functions.js?cachebust=oL8nT1pQ5qI7gC8r'></script>";
echo "</body>";
echo "</html>";
} else {
if ($userStatus == 200 && count($userResult['data']) > 0) {
// Get user schedule
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/schedule?broadcaster_id=" . $userResult['data'][0]['id']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$userResponse = curl_exec($ch);
header('Content-type: application/json');
echo $userResponse;
} else {
// return and empty data array/object
$userResponse = array(
"data" => []
);
$userResponse = json_encode($userResponse, true);
header('Content-type: application/json');
echo $userResponse;
}
}
curl_close($ch);
} else {
// return and empty data array/object
$userResponse = array(
"data" => []
);
$userResponse = json_encode($userResponse, true);
header('Content-type: application/json');
echo $userResponse;
}
?>