diff --git a/README.md b/README.md index 8a138be..0c8d283 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,11 @@ The library documentation is available on [alltube.surge.sh](https://alltube.sur You can also have a look at this [example project](https://github.com/Rudloff/alltube-example-project). +## JSON API + +We also provide a JSON API that you can use like this: +[/json?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdQw4w9WgXcQ](https://alltubedownload.net/json?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdQw4w9WgXcQ) + ## FAQ Please read the [FAQ](resources/FAQ.md) before reporting any issue. diff --git a/controllers/FrontController.php b/controllers/FrontController.php index 8108bd6..0d8d78e 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -548,6 +548,36 @@ class FrontController } } + /** + * 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) + { + $params = $request->getQueryParams(); + $format = $this->getFormat($request); + if (isset($params['url'])) { + try { + return $response->withJson( + $this->download->getJSON( + $params['url'], + $format + ) + ); + } 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); + } + } + /** * Generate the canonical URL of the current page. * diff --git a/index.php b/index.php index 37eb1d6..e59b357 100644 --- a/index.php +++ b/index.php @@ -51,6 +51,10 @@ $app->get( '/redirect', [$controller, 'redirect'] )->setName('redirect'); +$app->get( + '/json', + [$controller, 'json'] +)->setName('json'); $app->get( '/locale/{locale}', [$controller, 'locale'] diff --git a/tests/FrontControllerTest.php b/tests/FrontControllerTest.php index 4ce4495..21bf70d 100644 --- a/tests/FrontControllerTest.php +++ b/tests/FrontControllerTest.php @@ -147,7 +147,7 @@ class FrontControllerTest extends TestCase } /** - * Assert that calling controller function with these parameters returns an HTTP redirect. + * Assert that calling controller function with these parameters returns an HTTP 500 error. * * @param string $request Controller function to call * @param array $params Query parameters @@ -160,6 +160,20 @@ class FrontControllerTest extends TestCase $this->assertTrue($this->getRequestResult($request, $params, $config)->isServerError()); } + /** + * Assert that calling controller function with these parameters returns an HTTP 400 error. + * + * @param string $request Controller function to call + * @param array $params Query parameters + * @param Config $config Custom config + * + * @return void + */ + private function assertRequestIsClientError($request, array $params = [], Config $config = null) + { + $this->assertTrue($this->getRequestResult($request, $params, $config)->isClientError()); + } + /** * Test the constructor. * @@ -539,6 +553,36 @@ class FrontControllerTest extends TestCase ); } + /** + * Test the json() function without the URL parameter. + * + * @return void + */ + public function testJsonWithoutUrl() + { + $this->assertRequestIsClientError('json'); + } + + /** + * Test the json() function. + * + * @return void + */ + public function testJson() + { + $this->assertRequestIsOk('json', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']); + } + + /** + * Test the json() function with an error. + * + * @return void + */ + public function testJsonWithError() + { + $this->assertRequestIsServerError('json', ['url' => 'http://example.com/foo']); + } + /** * Test the locale() function. *