-
Notifications
You must be signed in to change notification settings - Fork 0
/
seriesly.class.php
278 lines (257 loc) · 8.82 KB
/
seriesly.class.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* Series.ly API v2.0 SDK class for PHP
*
* @author David Maillo (www.davidmaillo.com)
* @version 1.0
* @link http://api.series.ly/docs (API documentation)
**/
class seriesly_api
{
public $auth_token;
public $user_token;
public $user_expires_date;
/*--------------------------------------
AUTHENTICATION AND USER LOGIN
----------------------------------------*/
//Get the auth_token:
public function get_auth_token($id, $secret)
{
$base_url = 'http://api.series.ly/v2/auth_token';
$params = array(
'id_api' => $id,
'secret' => $secret
);
$query = $base_url.'?'.http_build_query($params);
$response = file_get_contents($query);
$data = json_decode($response, TRUE); // true for recursively to Array
$this->auth_token = $data['auth_token'];
if ($data['error'] == 0) return $data["auth_token"];
else echo __FUNCTION__." error: ".$data['error'];
}
//Make login and redirect to a callback file:
public function user_login($redirect){
$base_url = 'http://api.series.ly/v2/user/user_login';
$params = array(
'auth_token' => $this->auth_token,
'redirect_url' => $redirect
);
$query = $base_url . '?' . http_build_query($params);
header('Location: ' . $query);
}
//Get the user token (permissions from user):
public function get_user_token($method="GET"){
global $_SESSION, $_GET;
switch ($method){
case "SESSION":
$this->user_token = $_SESSION['user_token'];
$this->user_expires_date = $_SESSION['user_expires_date'];
break;
case "GET":
$this->user_token = $_GET['user_token'];
$this->user_expires_date = $_GET['user_expires_date'];
break;
}
}
//Logout:
public function user_logout(){
$base_url = 'http://api.series.ly/v2/user/logout';
$params = array(
'auth_token' => $this->auth_token,
'user_token' => $this->user_token
);
$query = $base_url . '?' . http_build_query($params);
$response = file_get_contents($query);
$data = json_decode($response, TRUE); // true for recursively to Array
if ($data['error'] == 0) {
// Sucess response, no error found.
// Now you can request a new user_token for other user.
}
if ($data['error'] == 0) return $data;
else echo __FUNCTION__." error: ".$data['error'];
}
/*--------------------------------------
USER INFO
----------------------------------------*/
//Retrieve user information: Name, surname, email, user ID, score, gender...
public function user(){
$base_url = 'http://api.series.ly/v2/user';
$params = array(
'auth_token' => $this->auth_token,
'user_token' => $this->user_token
);
$query = $base_url . '?' . http_build_query($params);
$response = file_get_contents($query);
$data = json_decode($response, TRUE); // true for recursively to Array
return $data;
}
/*--------------------------------------
USER / MEDIA
----------------------------------------*/
//Retrieve all the saved media of the user:
public function user_media($type="")
{
$base_url = 'http://api.series.ly/v2/user/media';
switch ($type)
{
case "series": $base_url .= "/series"; break;
case "movies": $base_url .= "/movies"; break;
case "tvshows": $base_url .= "/tvshows"; break;
case "documentaries": $base_url .= "/documentaries";
}
$params = array(
'auth_token' => $this->auth_token,
'user_token' => $this->user_token
);
$query = $base_url . '?' . http_build_query($params);
$response = file_get_contents($query);
$data = json_decode($response, TRUE); // true for recursively to Array
if ($data['error'] == 0) return $data;
else echo __FUNCTION__." error: ".$data['error'];
}
public function user_media_recommended(){
$base_url = 'http://api.series.ly/v2/user/media/recommended';
$params = array(
'auth_token' => $this->auth_token,
'user_token' => $this->user_token
);
$query = $base_url . '?' . http_build_query($params);
$response = file_get_contents($query);
$data = json_decode($response, TRUE); // true for recursively to Array
if ($data['error'] == 0) return $data;
else echo __FUNCTION__." error: ".$data['error'];
}
/*--------------------------------------
MEDIA
----------------------------------------*/
/* Perform a new search */
public function media_search($query="", $onlyTitle="", $page="", $per_page="", $order="", $year="", $decade="", $genere="", $mediaType=""){
$base_url = 'http://api.series.ly/v2/media/search';
if ($mediaType == "") unset($mediaType);
$params = array(
'auth_token' => $this->auth_token,
'q' => $query,
'year' => $year,
'decade' => $decade,
'page' => $page,
'per_page' => $per_page,
'genere' => $genere,
'mediaType' => $mediaType,
'order' => $order,
'onlyTitle' => $onlyTitle
);
$query = $base_url . '?' . http_build_query($params);
$response = file_get_contents($query);
$data = json_decode($response, TRUE); // true for recursively to Array
if ($data['error'] == 0) return $data;
else echo __FUNCTION__." error: ".$data['error'];
}
/* List of most seen media */
public function media_most_seen($type="", $limit=10){
$base_url = 'http://api.series.ly/v2/media/most_seen';
switch ($type)
{
case "series": $base_url .= "/series"; break;
case "movies": $base_url .= "/movies"; break;
case "tvshows": $base_url .= "/tvshows"; break;
case "documentaries": $base_url .= "/documentaries";
}
$params = array(
'auth_token' => $this->auth_token,
'limit' => $limit
);
$query = $base_url . '?' . http_build_query($params);
$response = file_get_contents($query);
$data = json_decode($response, TRUE); // true for recursively to Array
if ($data['error'] == 0) return $data;
else echo __FUNCTION__." error: ".$data['error'];
}
/* Full info of media */
public function media_full_info($mediaType="", $idm="", $multiple=""){
$base_url = 'http://api.series.ly/v2/media/full_info';
if (empty($multiple)) {
/* Single media */
$params = array(
'auth_token' => $this->auth_token,
'mediaType' => $mediaType, // See mediaType constants reference.
'idm' => $idm
);
}
else {
/* Multiple media */
$params = array(
'auth_token' => $this->auth_token,
'group' => $multiple
);
}
$query = $base_url . '?' . http_build_query($params);
$query = str_replace("%5B", "[", $query);
$query = str_replace("%5D", "]", $query);
$response = file_get_contents($query);
$data = json_decode($response, TRUE); // true for recursively to Array
if ($data['error'] == 0) return $data;
else echo __FUNCTION__." error: ".$data['error'];
}
/*--------------------------------------
TOOLS
----------------------------------------*/
//Know if user is logged in correctly
public function logged_in(){
$check = $this->user();
if ($check["error"] == 0)
return true;
else
return false;
}
//Api information
public function show_quota(){
$base_url = 'http://api.series.ly/v2/app/show_quota';
$params = array (
'auth_token' => $this->auth_token,
'user_token' => $this->user_token,
);
$query = $base_url.'?'.http_build_query($params);
$response = file_get_contents($query);
$data = json_decode($response, TRUE); // true for recursively to Array
if ($data['error'] == 0) return $data;
else echo __FUNCTION__." error: ".$data['error'];
}
//Translate genre of media
public function translate_genre($genre, $lang="ES")
{
//SPANISH
if ($lang == "ES")
{
switch($genre)
{
case "Action": $g = "Acción"; break;
case "Comedy": $g = "Comedia"; break;
case "Family": $g = "Familiar"; break;
case "History": $g = "Historia"; break;
case "Mystery": $g = "Misterio"; break;
case "Sci": $g = "Ciencia ficción"; break;
case "War": $g = "Guerra"; break;
case "Adventure": $g = "Aventura"; break;
case "Crime": $g = "Crimen"; break;
case "Fantasy": $g = "Fantasía"; break;
case "Horror": $g = "Terror"; break;
case "News": $g = "Actualidad"; break;
case "Sport": $g = "Deportes"; break;
case "Western": $g = "Western"; break;
case "Animation": $g = "Animación"; break;
case "Documentary": $g = "Documental"; break;
case "Film-noir": $g = "Cine negro"; break;
case "Music": $g = "Música"; break;
case "Drama": $g = "Drama"; break;
case "Musical": $g = "Musical"; break;
case "Romance": $g = "Romance"; break;
case "Thriller": $g = "Thriller"; break;
case "Reallity": $g = "Reallity Show"; break;
case "Biography": $g = "Biografía"; break;
default: $g = "";
}
}
return $g;
}
}
?>