Skip to content

Commit 5df7f65

Browse files
author
Daniel Neto
committed
Update API
1 parent 321adcf commit 5df7f65

13 files changed

+293
-95
lines changed

API/API.php

+64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22

3+
4+
require_once __DIR__ . '/../objects/Login.php';
5+
require_once __DIR__ . '/../objects/Encoder.php';
6+
require_once __DIR__ . '/../objects/Streamer.php';
7+
require_once __DIR__ . '/../objects/functions.php';
8+
39
class API
410
{
511
static function checkCredentials()
@@ -28,4 +34,62 @@ static function checkCredentials()
2834
}
2935
return $object;
3036
}
37+
38+
static function canChangeQueue($queue_id)
39+
{
40+
if (empty($_SESSION['login'])) {
41+
return false;
42+
}
43+
$streamer = new Streamer($_SESSION['login']->streamers_id);
44+
if (self::isAdmin()) {
45+
return true;
46+
}
47+
$encoder = new Encoder($queue_id);
48+
return $encoder->getStreamers_id() == $_SESSION['login']->streamers_id;
49+
}
50+
51+
static function isAdmin()
52+
{
53+
if (empty($_SESSION['login'])) {
54+
return false;
55+
}
56+
$streamer = new Streamer($_SESSION['login']->streamers_id);
57+
return !empty($streamer->getIsAdmin());
58+
}
59+
60+
static function cleanQueueArray($queue)
61+
{
62+
// Convert object to array if necessary
63+
if (is_object($queue)) {
64+
$queue = (array)$queue;
65+
}
66+
67+
// Set videos_id
68+
if (isset($queue['return_vars']->videos_id)) {
69+
$queue['videos_id'] = $queue['return_vars']->videos_id;
70+
}
71+
72+
$keep = array(
73+
'id',
74+
'created',
75+
'modified',
76+
'videos_id',
77+
'priority',
78+
'videoDownloadedLink',
79+
'downloadedFileName',
80+
'streamers_id',
81+
'conversion',
82+
'download',
83+
'title',
84+
);
85+
86+
// Clean the queue
87+
foreach ($queue as $key => $value) {
88+
if (!in_array($key, $keep)) {
89+
unset($queue[$key]);
90+
}
91+
}
92+
93+
return $queue;
94+
}
3195
}

API/delete_queue.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
//OK
3+
require_once __DIR__ . '/../videos/configuration.php';
4+
header('Content-Type: application/json');
5+
6+
require_once __DIR__ . '/../objects/Login.php';
7+
require_once __DIR__ . '/../objects/Encoder.php';
8+
require_once __DIR__ . '/API.php';
9+
10+
$object = API::checkCredentials();
11+
12+
13+
if(empty($_REQUEST['queue_id'])){
14+
$object->msg = 'queue_id is empty';
15+
die(json_encode($object));
16+
}
17+
18+
if(!API::canChangeQueue($_REQUEST['queue_id'])){
19+
$object->msg = 'You cannot change the queue';
20+
die(json_encode($object));
21+
}
22+
23+
$object->queue_id = intval($_REQUEST['queue_id']);
24+
$encoder = new Encoder($object->queue_id);
25+
26+
$object->error = !$encoder->delete();
27+
28+
die(json_encode($object));

API/edit_queue.php

+18-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,28 @@
22
require_once __DIR__ . '/../videos/configuration.php';
33
header('Content-Type: application/json');
44

5-
require_once __DIR__ . '/../objects/Login.php';
6-
require_once __DIR__ . '/../objects/Encoder.php';
75
require_once __DIR__ . '/API.php';
86

97
$object = API::checkCredentials();
108

9+
if(empty($_REQUEST['queue_id'])){
10+
$object->msg = 'queue_id is empty';
11+
die(json_encode($object));
12+
}
1113

12-
$object->queue = Encoder::getQueue(array(), $object->login->streamers_id);
14+
if(!isset($_REQUEST['priority'])){
15+
$object->msg = 'priority is not set';
16+
die(json_encode($object));
17+
}
18+
19+
if(!API::isAdmin()){
20+
$object->msg = 'Only encoder admin can change priority';
21+
die(json_encode($object));
22+
}
23+
24+
$object->queue_id = intval($_REQUEST['queue_id']);
25+
$encoder = new Encoder($object->queue_id);
26+
$encoder->setPriority($_REQUEST['priority']);
27+
$object->error = !$encoder->save();
1328

1429
die(json_encode($object));

API/get_queue_status.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
//OK
3+
require_once __DIR__ . '/../videos/configuration.php';
4+
header('Content-Type: application/json');
5+
6+
require_once __DIR__ . '/../objects/Login.php';
7+
require_once __DIR__ . '/../objects/Encoder.php';
8+
require_once __DIR__ . '/API.php';
9+
10+
$object = API::checkCredentials();
11+
12+
$status = array(
13+
Encoder::$STATUS_ENCODING,
14+
Encoder::$STATUS_DOWNLOADING,
15+
Encoder::$STATUS_DOWNLOADED,
16+
Encoder::$STATUS_QUEUE,
17+
Encoder::$STATUS_ERROR,
18+
Encoder::$STATUS_DONE,
19+
Encoder::$STATUS_TRANSFERRING,
20+
Encoder::$STATUS_PACKING,
21+
Encoder::$STATUS_FIXING,
22+
);
23+
24+
$object->queue = Encoder::getQueue($status, $object->login->streamers_id);
25+
$object->error = false;
26+
27+
if(!empty($_REQUEST['videos_id'])){
28+
$object->videos_id = intval($_REQUEST['videos_id']);
29+
foreach ($object->queue as $key => $value) {
30+
if($value['return_vars']->videos_id !== $object->videos_id){
31+
unset($object->queue[$key]);
32+
}
33+
}
34+
}
35+
36+
foreach ($object->queue as $key => $value) {
37+
$object->queue[$key]['conversion'] = Encoder::getVideoConversionStatus($value['id']);
38+
$object->queue[$key]['download'] = Encoder::getYoutubeDlProgress($value['id']);
39+
$object->queue[$key] = API::cleanQueueArray($object->queue[$key]);
40+
}
41+
42+
die(json_encode($object));
File renamed without changes.

API/remove_video.php

Whitespace-only changes.

API/restart_queue.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
//OK
3+
require_once __DIR__ . '/../videos/configuration.php';
4+
header('Content-Type: application/json');
5+
require_once __DIR__ . '/API.php';
6+
7+
$object = API::checkCredentials();
8+
9+
if(empty($_REQUEST['queue_id'])){
10+
$object->msg = 'queue_id is empty';
11+
die(json_encode($object));
12+
}
13+
14+
if(!API::canChangeQueue($_REQUEST['queue_id'])){
15+
$object->msg = 'You cannot change the queue';
16+
die(json_encode($object));
17+
}
18+
19+
$object->queue_id = intval($_REQUEST['queue_id']);
20+
$encoder = new Encoder($object->queue_id);
21+
22+
$encoder->setStatus(Encoder::$STATUS_QUEUE);
23+
24+
$object->error = !$encoder->save();
25+
26+
die(json_encode($object));

API/resubmit_video.php

Whitespace-only changes.

API/submit_video.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../videos/configuration.php';
4+
header('Content-Type: application/json');
5+
6+
require_once __DIR__ . '/API.php';
7+
8+
if(empty($_REQUEST['videoURL'])){
9+
$object->msg = 'videoURL is required';
10+
die(json_encode($object));
11+
}
12+
13+
$object = API::checkCredentials();
14+
if (!Login::canUpload()) {
15+
$object->msg = "This user can not upload files";
16+
die(json_encode($object));
17+
}
18+
19+
$object->videoURL = $_REQUEST['videoURL'];
20+
$object->videoTitle = @$_REQUEST['videoTitle'];
21+
22+
$object->addVideo = addVideo($object->videoURL, $object->login->streamers_id, $object->videoTitle);
23+
24+
die(json_encode($object));

API/view_encoded_video.php

Whitespace-only changes.

objects/Encoder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ public static function getQueue($status = array(), $streamers_id = 0)
767767
CASE WHEN priority IS NULL THEN 1 ELSE 0 END ASC,
768768
priority ASC,
769769
e.id ASC ";
770-
770+
//var_dump($sql);
771771
/**
772772
* @var array $global
773773
* @var object $global['mysqli']

objects/functions.php

+89
Original file line numberDiff line numberDiff line change
@@ -1419,3 +1419,92 @@ function _error_log($message)
14191419
}
14201420
error_log($str);
14211421
}
1422+
1423+
function addVideo($link, $streamers_id, $title = "") {
1424+
$obj = new stdClass();
1425+
// remove list parameter from
1426+
$link = preg_replace('~(\?|&)list=[^&]*~', '$1', $link);
1427+
$link = str_replace("?&", "?", $link);
1428+
if (substr($link, -1) == '&') {
1429+
$link = substr($link, 0, -1);
1430+
}
1431+
1432+
$msg = '';
1433+
if (empty($title)) {
1434+
$_title = Encoder::getTitleFromLink($link);
1435+
$msg = $_title['output'];
1436+
$title = $_title['output'];
1437+
if ($_title['error']) {
1438+
$title = false;
1439+
}
1440+
}
1441+
if (!$title) {
1442+
$obj->error = "youtube-dl --force-ipv4 get title ERROR** " . print_r($link, true);
1443+
$obj->type = "warning";
1444+
$obj->title = "Sorry!";
1445+
1446+
if (!empty($msg)) {
1447+
$obj->text = $msg;
1448+
} else {
1449+
$obj->text = sprintf("We could not get the title of your video (%s) go to %s to fix it", $link, "<a href='https://github.com/WWBN/AVideo/wiki/youtube-dl-failed-to-extract-signature' class='btn btn-xm btn-default'>Update your Youtube-DL</a>");
1450+
}
1451+
1452+
error_log("youtubeDl::addVideo We could not get the title ($title) of your video ($link)");
1453+
} else {
1454+
$obj->type = "success";
1455+
$obj->title = "Congratulations!";
1456+
$obj->text = sprintf("Your video (%s) is downloading", $title);
1457+
1458+
$filename = preg_replace("/[^A-Za-z0-9]+/", "_", cleanString($title));
1459+
$filename = uniqid("{$filename}_YPTuniqid_", true) . ".mp4";
1460+
1461+
$s = new Streamer($streamers_id);
1462+
1463+
$e = new Encoder("");
1464+
$e->setStreamers_id($streamers_id);
1465+
$e->setTitle($title);
1466+
$e->setFileURI($link);
1467+
$e->setVideoDownloadedLink($link);
1468+
$e->setFilename($filename);
1469+
$e->setStatus(Encoder::$STATUS_QUEUE);
1470+
$e->setPriority($s->getPriority());
1471+
//$e->setNotifyURL($global['AVideoURL'] . "aVideoEncoder.json");
1472+
1473+
$encoders_ids = [];
1474+
1475+
if (!empty($_REQUEST['audioOnly']) && $_REQUEST['audioOnly'] !== 'false') {
1476+
if (!empty($_REQUEST['spectrum']) && $_REQUEST['spectrum'] !== 'false') {
1477+
$e->setFormats_idFromOrder(70); // video to spectrum [(6)MP4 to MP3] -> [(5)MP3 to spectrum] -> [(2)MP4 to webm]
1478+
} else {
1479+
$e->setFormats_idFromOrder(71);
1480+
}
1481+
} else {
1482+
$e->setFormats_idFromOrder(decideFormatOrder());
1483+
}
1484+
$obj = new stdClass();
1485+
$f = new Format($e->getFormats_id());
1486+
$format = $f->getExtension();
1487+
1488+
$obj = new stdClass();
1489+
$obj->videos_id = 0;
1490+
$obj->video_id_hash = '';
1491+
if (!empty($_REQUEST['update_video_id'])) {
1492+
$obj->videos_id = $_REQUEST['update_video_id'];
1493+
}
1494+
1495+
$obj->releaseDate = @$_REQUEST['releaseDate'];
1496+
1497+
$response = Encoder::sendFile('', $obj, $format, $e);
1498+
//var_dump($response);exit;
1499+
if (!empty($response->response->video_id)) {
1500+
$obj->videos_id = $response->response->video_id;
1501+
}
1502+
if (!empty($response->response->video_id_hash)) {
1503+
$obj->video_id_hash = $response->response->video_id_hash;
1504+
}
1505+
$e->setReturn_vars(json_encode($obj));
1506+
$encoders_ids[] = $e->save();
1507+
}
1508+
$obj->queue_id = $encoders_ids[0];
1509+
return $obj;
1510+
}

0 commit comments

Comments
 (0)