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;
|
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
use Alltube\Library\Exception\AlltubeLibraryException;
|
2022-02-27 09:54:56 +00:00
|
|
|
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidURLException;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
2020-07-05 09:22:55 +00:00
|
|
|
use Slim\Http\StatusCode;
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller that returns JSON.
|
|
|
|
*/
|
|
|
|
class JsonController extends BaseController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Return the JSON object generated by youtube-dl.
|
|
|
|
*
|
2020-06-20 23:44:20 +00:00
|
|
|
* @param Request $request PSR-7 request
|
2019-04-22 14:05:58 +00:00
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
2022-02-27 22:36:51 +00:00
|
|
|
* @throws AlltubeLibraryException
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function json(Request $request, Response $response): Response
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
2022-02-27 09:54:56 +00:00
|
|
|
try {
|
|
|
|
$url = $this->getVideoPageUrl($request);
|
2019-04-22 14:05:58 +00:00
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
$this->video = $this->downloader->getVideo(
|
|
|
|
$url,
|
|
|
|
$this->getFormat($request),
|
|
|
|
$this->getPassword($request)
|
|
|
|
);
|
2019-04-22 14:05:58 +00:00
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
return $response->withJson($this->video->getJson());
|
2022-02-27 09:54:56 +00:00
|
|
|
} catch (InvalidURLException $e) {
|
|
|
|
return $response->withJson(['error' => $e->getMessage()])
|
2020-07-05 09:22:55 +00:00
|
|
|
->withStatus(StatusCode::HTTP_BAD_REQUEST);
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|