-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.php
140 lines (121 loc) · 3.27 KB
/
logic.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
<?php
// Base Variables
$base_url = "https://api.twitch.tv/helix";
$stream_url = "/streams";
$followed_url = "/streams/followed";
$game_url = "/games/top";
$client_id = "tfdaga4350ved4acxim5958z1qcr8y";
$top_games_limit = "100";
$total = "900";
// Config Options
if (file_exists("config.php")) {
include 'config.php';
}
// Functions
function getRequest(
$base_url,
$url_extend,
$client_id,
$params = [],
$oauth = ""
) {
$request_url = createUrl($base_url, $url_extend, $params);
$json = curlGet($request_url, $client_id, $oauth);
return toArray($json);
}
function createUrl($base_url, $url_extend, $params = [])
{
$param_string = paramToString($params);
return $base_url . $url_extend . "?" . $param_string;
}
function paramToString($params)
{
$filtered_params = filterParams($params);
$string = "";
foreach ($filtered_params as $param => $value) {
$string .= "&" . $param . "=" . $value;
}
return $string;
}
function filterParams($params)
{
$new_params = [];
foreach ($params as $param => $value) {
switch ($param) {
case "limit":
if (is_numeric($value) && ($value > 0) && ($value < 101)) {
$new_params["first"] = $value;
}
continue 2;
case "game":
if (is_numeric($value)) {
$new_params["game_id"] = $value;
}
continue 2;
case "user_id":
$new_params["user_id"] = $value;
continue 2;
case "after":
$new_params["after"] = $value;
continue 2;
default:
continue 2;
}
}
return $new_params;
}
function curlGet($request_url, $client_id, $oauth = "")
{
$header_array[] = 'Client-ID: ' . $client_id;
if ($oauth) {
$header_array[] = 'Authorization: Bearer ' . $oauth;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
function toArray($json)
{
return json_decode($json, true);
}
// Logic
if (!isset($_GET["only_followed"])
&& !isset($_GET["ofh"])
&& isset($only_followed_default)
&& $only_followed_default === true
) {
$_GET["only_followed"] = "1";
} elseif (!isset($_GET["only_followed"])) {
$_GET["only_followed"] = "0";
}
if (!isset($_GET['limit']) && isset($default_limit)) {
$_GET['limit'] = $default_limit;
}
if ($_GET["only_followed"] === "1") {
$_GET["user_id"] = $user_id;
$content = getRequest(
$base_url,
$followed_url,
$client_id,
$_GET,
$oauth_token
);
} else {
$content = getRequest($base_url, $stream_url, $client_id, $_GET, $oauth_token);
}
if (isset($get_top_games) && $get_top_games === true) {
$games = getRequest($base_url, $game_url, $client_id, ["limit" => $top_games_limit], $oauth_token);
}
if (!isset($_GET['limit']) or $_GET['limit'] === "") {
$_GET['limit'] = "25";
}
if (!isset($_GET['offset']) or $_GET['offset'] === "") {
$_GET['offset'] = "0";
}
if (!isset($_GET['game'])) {
$_GET['game'] = "";
}