2019-04-22 14:05:58 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2019-04-22 14:05:58 +00:00
|
|
|
/**
|
|
|
|
* JsonController class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube\Controller;
|
|
|
|
|
|
|
|
use Alltube\Video;
|
|
|
|
use Exception;
|
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller that returns JSON.
|
|
|
|
*/
|
|
|
|
class JsonController extends BaseController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Return the JSON object generated by youtube-dl.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
|
|
|
public function json(Request $request, Response $response)
|
|
|
|
{
|
|
|
|
$url = $request->getQueryParam('url');
|
|
|
|
|
|
|
|
if (isset($url)) {
|
|
|
|
try {
|
|
|
|
$this->video = new Video($url, $this->getFormat($request), $this->getPassword($request));
|
|
|
|
|
|
|
|
return $response->withJson($this->video->getJson());
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $response->withJson(['error' => $e->getMessage()])
|
|
|
|
->withStatus(500);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $response->withJson(['error' => 'You need to provide the url parameter'])
|
|
|
|
->withStatus(400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|