-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPremiumize.class.php
More file actions
447 lines (415 loc) · 9.52 KB
/
Copy pathPremiumize.class.php
File metadata and controls
447 lines (415 loc) · 9.52 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/**
* Premiumize.class.php
*
* @link https://www.premiumize.me/api
* @version 1.0
* @author Tim "timmyRS" Speckhals
* @copyright Copyright (c) 2017, timmyRS
*/
define("PREMIUMIZE_CUSTOMER_ID", "XXXXXXXXX");
define("PREMIUMIZE_PIN", "xxxxxxxxxxxxxxxx");
define("PREMIUMIZE_USE_HTTPS", true);
abstract class Premiumize
{
/**
* Sends a request to the Premiumize API.
* Usually you don't need to use this function.
*
* @param string API Endpoint
* @param array Arguments
* @return array JSON
*/
public static function request($endpoint, $args = [])
{
$args["customer_id"] = PREMIUMIZE_CUSTOMER_ID;
$args["pin"] = PREMIUMIZE_PIN;
$json = json_decode(file_get_contents((PREMIUMIZE_USE_HTTPS?"https://www":"http://http").".premiumize.me/api".$endpoint."?".http_build_query($args)), true);
if(!$json || $json["status"] != "success")
{
if($json && isset($json["message"]))
{
throw new Exception("Request was unsuccessful: ".$json["message"]);
}
else
{
throw new Exception("Request was unsuccessful.");
}
}
return $json;
}
/**
* Returns an array of account informations with the following keys:
* "premium_until" The timestamp of when your premium will end.
* "available_points" How many Fair Use Points you have remaining.
* "points_used" Percentage of how much of your fair use points you have used.
*
* @return array<string> Account Information Array
*/
public static function accountInfo()
{
$json = Premiumize::request("/account/info");
return ["premium_until" => $json["premium_until"], "points_used" => $json["limit_used"], "available_points" => (1000 - (1000 * $json["limit_used"]))];
}
/**
* Starts a Transfer.
*
* @param string Magnet Link or Link to a Torrent File.
* @return string The ID of the Transfer.
*/
public static function startTransfer($src)
{
return Premiumize::request("/transfer/create", ["type" => "torrent", "src" => $src])["id"];
}
/**
* Lists All Transfers.
*
* @return array<PremiumizeTransfer> Array of PremiumizeTransfer Objects.
*/
public static function getTransfers()
{
$transfers = [];
foreach(Premiumize::request("/transfer/list")["transfers"] as $rawTransfer)
{
$transfer = new PremiumizeTransfer();
$transfer->id = $rawTransfer["id"];
$transfer->name = $rawTransfer["name"];
$transfer->hash = $rawTransfer["hash"];
$transfer->status = PremiumizeTransferStatus::fromName($rawTransfer["status"]);
$transfer->bytes = $rawTransfer["size"];
$transfer->type = PremiumizeFileType::fromName($rawTransfer["type"]);
$transfer->message = $rawTransfer["message"];
$transfer->up_speed = $rawTransfer["speed_up"];
$transfer->down_speed = $rawTransfer["speed_down"];
$transfer->eta = $rawTransfer["eta"];
$transfer->seeder = $rawTransfer["seeder"];
$transfer->leacher = $rawTransfer["leacher"];
$transfer->ratio = $rawTransfer["ratio"];
$transfer->progress = $rawTransfer["progress"];
array_push($transfers, $transfer);
}
return $transfers;
}
/**
* Clears All Finished Transfers.
*/
public static function clearFinishedTransfers()
{
Premiumize::request("/transfer/clearfinished");
}
/**
* Checks the Given Hashes.
*
* @param array<string> Array of hashes
* @return array<PremiumizeTransferStatus,boolean> Array for each hash including a "status" and "transcoded" value.
*/
public static function checkHashes($hashes)
{
$hashes = [];
foreach(Premiumize::request("/torrent/checkhashes", ["hashes" => $hashes]) as $hash)
{
array_push($hashes, ["status" => PremiumizeTransferStatus::fromName($hash["status"]), "transcoded" => $hash["transcoded"]]);
}
return $hashes;
}
public static function getRootFolder()
{
$folder = new PremiumizeFolder();
$folder->id = "";
$folder->name = "root";
return $folder;
}
public static function getFolderContent($id = "")
{
$files = [];
foreach(Premiumize::request("/folder/list", ["id" => $id])["content"] as $rawfile)
{
$file;
if($rawfile["type"] == "torrent")
{
$file = new PremiumizeFileTorrent();
}
else if($rawfile["type"] == "folder")
{
$file = new PremiumizeFolder();
}
else
{
throw new Exception("Unsupported File Type: ".$rawfile["type"]);
}
$file->id = $rawfile["id"];
$file->name = $rawfile["name"];
switch($rawfile["type"])
{
case "torrent":
$file->hash = $rawfile["hash"];
$file->size = $rawfile["size"];
$file->created_at = $rawfile["created_at"];
break;
}
array_push($files, $file);
return $files;
}
}
public static function getTorrentContent($hash)
{
$torrent = new PremiumizeTorrent();
if($rawtorrent = Premiumize::request("/torrent/browse", ["hash" => $hash]))
{
$torrent->download = $rawtorrent["zip"];
$torrent->size = $rawtorrent["size"];
$torrent->content = Premiumize::recurseTorrentContent($rawtorrent["content"]);
}
return $torrent;
}
private static function recurseTorrentContent($rawchildren)
{
$children = [];
foreach($rawchildren as $rawchild)
{
$child;
if($rawchild["type"] == "dir")
{
$child = new PremiumizeTorrentDirectory();
$child->download = $rawchild["zip"];
}
else
{
$child = new PremiumizeTorrentFile();
}
$child->name = $rawchild["name"];
$child->size = $rawchild["size"];
if($rawchild["type"] == "dir")
{
$child->children = Premiumize::recurseTorrentContent($rawchild["children"]);
}
else
{
$child->download = $rawchild["url"];
$child->width = $rawchild["width"];
$child->height = $rawchild["height"];
$child->duration = $rawchild["duration"];
$child->transcoded = $rawchild["transcoded"];
}
array_push($children, $child);
}
return $children;
}
}
abstract class PremiumizeTransferStatus
{
const WAITING = 0;
const FINISHED = 1;
const SEEDING = 2;
const TIMEOUT = 3;
const ERROR = 4;
public static function fromName($name)
{
if($name == "waiting")
{
return PremiumizeTransferStatus::WAITING;
}
else if($name == "finished")
{
return PremiumizeTransferStatus::FINISHED;
}
else if($name == "seeding")
{
return PremiumizeTransferStatus::SEEDING;
}
else if($name == "timeout")
{
return PremiumizeTransferStatus::TIMEOUT;
}
return PremiumizeTransferStatus::ERROR;
}
}
abstract class PremiumizeFileType
{
const FOLDER = 0;
const TORRENT = 1;
public static function fromName($name)
{
if($name == "folder")
{
return PremiumizeTransferStatus::FOLDER;
}
else if($name == "torrent")
{
return PremiumizeTransferStatus::TORRENT;
}
}
}
class PremiumizeTransfer
{
public $id;
public $name;
public $hash;
/**
* Size of the Transfer in bytes.
*
* @var integer
*/
public $size;
/**
* Status of the Transfer.
*
* @var PremiumizeTransferStatus
*/
public $status;
/**
* Type of the Transfer.
*
* @var PremiumizeFileType
*/
public $type;
public $message;
public $up_speed;
public $down_speed;
public $eta;
public $seeder;
public $leacher;
public $ratio;
/**
* Progress as a percentage (0.0 - 1.0)
*
* @var float
*/
public $progress;
/**
* Deletes or aborts this Transfer.
*/
public function delete()
{
Premiumize::request("/transfer/delete", ["id" => $this->id, "type" => $this->type]);
}
}
class PremiumizeTorrent
{
public $content;
/**
* Size of the Torrent in Bytes.
*
* @var integer
*/
public $size;
public $download;
}
abstract class PremiumizeAbstractTorrentFile
{
public $name;
/**
* Size of the File in Bytes.
*
* @var integer
*/
public $size;
public $download;
}
class PremiumizeTorrentDirectory extends PremiumizeAbstractTorrentFile
{
public $children;
}
class PremiumizeTorrentFile extends PremiumizeAbstractTorrentFile
{
/**
* The File Extension.
* The file name does include the extension too. However, this variable is probably used for loading icons.
*
* @var string
*/
public $ext;
/**
* The MIME Type of the file.
*
* @var string
*/
public $mime;
public $width;
public $height;
public $duration;
public $transcoded;
}
abstract class PremiumizeFile
{
/**
* ID of the File.
*
* @var integer
*/
public $id;
/**
* Name of the File.
*
* @var string
*/
public $name;
private $content = null;
abstract public function getContent();
/**
* Deletes the File.
*/
abstract public function delete();
}
class PremiumizeFileTorrent extends PremiumizeFile
{
public $hash;
/**
* Size of the File in Bytes.
*
* @var string
*/
public $size;
/**
* Timestamp of when the torrent was created at.
*
* @var string
*/
public $created_at;
/**
* Returns the PremiumizeTorrent object for the File.
*
* @return PremiumizeTorrent PremiumizeTorrent object of the File.
*/
public function getContent()
{
if($this->content === null)
{
$this->content = Premiumize::getTorrentContent($this->hash);
}
return $this->content;
}
public function delete()
{
Premiumize::request("/item/delete", ["type" => "torrent", "id" => $this->id]);
}
}
class PremiumizeFolder extends PremiumizeFile
{
/**
* Returns an array of Files contained in this Folder.
*
* @return array<PremiumizeFile> Array of contained Files.
*/
public function getContent()
{
if($this->content === null)
{
$this->content = Premiumize::getFolderContent($this->id);
}
return $this->content;
}
public function renameTo($newname)
{
if($this->id == "")
{
throw new Exception("Can't rename root folder.");
}
Premiumize::request("/folder/rename", ["id" => $this->id, "newname" => $newname]);
}
public function delete()
{
Premiumize::request("/folder/delete", ["id" => $this->id]);
}
}
?>