Merge branch 'develop' of github.com:Rudloff/alltube into develop

This commit is contained in:
Pierre Rudloff 2018-03-03 23:20:03 +01:00
commit e7be0b36f4
14 changed files with 127 additions and 108 deletions

View file

@ -5,6 +5,7 @@
namespace Alltube;
use Exception;
use Symfony\Component\Yaml\Yaml;
/**
@ -172,7 +173,7 @@ class Config
*/
$options = [];
} else {
throw new \Exception("Can't find config file at ".$yamlPath);
throw new Exception("Can't find config file at ".$yamlPath);
}
self::$instance = new self($options);
self::$instance->file = $yamlfile;

View file

@ -5,6 +5,8 @@
namespace Alltube;
use Locale as PHPLocale;
use Rinvex\Country\Country;
use Teto\HTTP\AcceptLanguage;
/**
@ -55,7 +57,7 @@ class Locale
*/
public function getFullName()
{
return \Locale::getDisplayName($this->getIso15897(), $this->getIso15897());
return PHPLocale::getDisplayName($this->getIso15897(), $this->getIso15897());
}
/**
@ -91,7 +93,7 @@ class Locale
/**
* Get country information from locale.
*
* @return \Rinvex\Country\Country|array
* @return Country|array
*/
public function getCountry()
{

View file

@ -5,6 +5,8 @@
namespace Alltube;
use Aura\Session\Segment;
use Aura\Session\SessionFactory;
use Symfony\Component\Process\Process;
/**
@ -29,7 +31,7 @@ class LocaleManager
/**
* Session segment used to store session variables.
*
* @var \Aura\Session\Segment
* @var Segment
*/
private $sessionSegment;
@ -40,9 +42,9 @@ class LocaleManager
*/
public function __construct(array $cookies = [])
{
$session_factory = new \Aura\Session\SessionFactory();
$session_factory = new SessionFactory();
$session = $session_factory->newInstance($cookies);
$this->sessionSegment = $session->getSegment('Alltube\LocaleManager');
$this->sessionSegment = $session->getSegment(self::class);
$cookieLocale = $this->sessionSegment->get('locale');
if (isset($cookieLocale)) {
$this->setLocale(new Locale($cookieLocale));

View file

@ -5,9 +5,11 @@
namespace Alltube;
use Exception;
/**
* Exception thrown when a video requires a password.
*/
class PasswordException extends \Exception
class PasswordException extends Exception
{
}

View file

@ -8,6 +8,7 @@
namespace Alltube;
use Barracuda\ArchiveStream\TarArchive;
use GuzzleHttp\Client;
/**
* Class used to create a Tar archive from playlists and stream it to the browser.
@ -33,7 +34,7 @@ class PlaylistArchiveStream extends TarArchive
/**
* Guzzle client.
*
* @var \GuzzleHttp\Client
* @var Client
*/
private $client;
@ -65,7 +66,7 @@ class PlaylistArchiveStream extends TarArchive
*/
public function __construct(Config $config = null)
{
$this->client = new \GuzzleHttp\Client();
$this->client = new Client();
$this->download = new VideoDownload($config);
}

View file

@ -5,7 +5,9 @@
namespace Alltube;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Slim\Router;
/**
@ -43,8 +45,8 @@ class UglyRouter extends Router
* @param array $data Named argument replacement data
* @param array $queryParams Optional query string parameters
*
* @throws \RuntimeException If named route does not exist
* @throws \InvalidArgumentException If required data not provided
* @throws RuntimeException If named route does not exist
* @throws InvalidArgumentException If required data not provided
*
* @return string
*/

View file

@ -5,6 +5,8 @@
namespace Alltube;
use Exception;
use stdClass;
use Symfony\Component\Process\Process;
/**
@ -24,8 +26,8 @@ class VideoDownload
*
* @param Config $config Config instance.
*
* @throws \Exception If youtube-dl is missing
* @throws \Exception If Python is missing
* @throws Exception If youtube-dl is missing
* @throws Exception If Python is missing
*/
public function __construct(Config $config = null)
{
@ -39,9 +41,9 @@ class VideoDownload
so they will always go to the logs.
*/
if (!is_file($this->config->youtubedl)) {
throw new \Exception("Can't find youtube-dl at ".$this->config->youtubedl);
throw new Exception("Can't find youtube-dl at ".$this->config->youtubedl);
} elseif (!$this->checkCommand([$this->config->python, '--version'])) {
throw new \Exception("Can't find Python at ".$this->config->python);
throw new Exception("Can't find Python at ".$this->config->python);
}
}
@ -82,8 +84,8 @@ class VideoDownload
* @param string $password Video password
*
* @throws PasswordException If the video is protected by a password and no password was specified
* @throws \Exception If the password is wrong
* @throws \Exception If youtube-dl returns an error
* @throws Exception If the password is wrong
* @throws Exception If youtube-dl returns an error
*
* @return string
*/
@ -108,12 +110,13 @@ class VideoDownload
$process->run();
if (!$process->isSuccessful()) {
$errorOutput = trim($process->getErrorOutput());
$exitCode = $process->getExitCode();
if ($errorOutput == 'ERROR: This video is protected by a password, use the --video-password option') {
throw new PasswordException($errorOutput);
throw new PasswordException($errorOutput, $exitCode);
} elseif (substr($errorOutput, 0, 21) == 'ERROR: Wrong password') {
throw new \Exception(_('Wrong password'));
throw new Exception(_('Wrong password'), $exitCode);
} else {
throw new \Exception($errorOutput);
throw new Exception($errorOutput, $exitCode);
}
} else {
return trim($process->getOutput());
@ -209,7 +212,7 @@ class VideoDownload
*
* @return array Arguments
*/
private function getRtmpArguments(\stdClass $video)
private function getRtmpArguments(stdClass $video)
{
$arguments = [];
@ -260,14 +263,14 @@ class VideoDownload
* @param string $filetype Filetype of the converted file
* @param bool $audioOnly True to return an audio-only file
*
* @throws \Exception If avconv/ffmpeg is missing
* @throws Exception If avconv/ffmpeg is missing
*
* @return Process Process
*/
private function getAvconvProcess(\stdClass $video, $audioBitrate, $filetype = 'mp3', $audioOnly = true)
private function getAvconvProcess(stdClass $video, $audioBitrate, $filetype = 'mp3', $audioOnly = true)
{
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw(new \Exception(_('Can\'t find avconv or ffmpeg.')));
throw new Exception(_('Can\'t find avconv or ffmpeg at ').$this->config->avconv.'.');
}
if ($video->protocol == 'rtmp') {
@ -314,8 +317,8 @@ class VideoDownload
* @param string $format Format to use for the video
* @param string $password Video password
*
* @throws \Exception If your try to convert and M3U8 video
* @throws \Exception If the popen stream was not created correctly
* @throws Exception If your try to convert and M3U8 video
* @throws Exception If the popen stream was not created correctly
*
* @return resource popen stream
*/
@ -323,7 +326,7 @@ class VideoDownload
{
$video = $this->getJSON($url, $format, $password);
if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) {
throw(new \Exception(_('Conversion of M3U8 files is not supported.')));
throw new Exception(_('Conversion of M3U8 files is not supported.'));
}
$avconvProc = $this->getAvconvProcess($video, $this->config->audioBitrate);
@ -331,7 +334,7 @@ class VideoDownload
$stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new \Exception(_('Could not open popen stream.'));
throw new Exception(_('Could not open popen stream.'));
}
return $stream;
@ -340,17 +343,17 @@ class VideoDownload
/**
* Get video stream from an M3U playlist.
*
* @param \stdClass $video Video object returned by getJSON
* @param stdClass $video Video object returned by getJSON
*
* @throws \Exception If avconv/ffmpeg is missing
* @throws \Exception If the popen stream was not created correctly
* @throws Exception If avconv/ffmpeg is missing
* @throws Exception If the popen stream was not created correctly
*
* @return resource popen stream
*/
public function getM3uStream(\stdClass $video)
public function getM3uStream(stdClass $video)
{
if (!$this->checkCommand([$this->config->avconv, '-version'])) {
throw(new \Exception(_('Can\'t find avconv or ffmpeg.')));
throw new Exception(_('Can\'t find avconv or ffmpeg at ').$this->config->avconv.'.');
}
$process = new Process(
@ -368,7 +371,7 @@ class VideoDownload
$stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new \Exception(_('Could not open popen stream.'));
throw new Exception(_('Could not open popen stream.'));
}
return $stream;
@ -379,7 +382,7 @@ class VideoDownload
*
* @param array $urls URLs of the video ($urls[0]) and audio ($urls[1]) files
*
* @throws \Exception If the popen stream was not created correctly
* @throws Exception If the popen stream was not created correctly
*
* @return resource popen stream
*/
@ -401,7 +404,7 @@ class VideoDownload
$stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new \Exception(_('Could not open popen stream.'));
throw new Exception(_('Could not open popen stream.'));
}
return $stream;
@ -410,13 +413,13 @@ class VideoDownload
/**
* Get video stream from an RTMP video.
*
* @param \stdClass $video Video object returned by getJSON
* @param stdClass $video Video object returned by getJSON
*
* @throws \Exception If the popen stream was not created correctly
* @throws Exception If the popen stream was not created correctly
*
* @return resource popen stream
*/
public function getRtmpStream(\stdClass $video)
public function getRtmpStream(stdClass $video)
{
$process = new Process(
array_merge(
@ -434,7 +437,7 @@ class VideoDownload
);
$stream = popen($process->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new \Exception(_('Could not open popen stream.'));
throw new Exception(_('Could not open popen stream.'));
}
return $stream;
@ -446,11 +449,11 @@ class VideoDownload
* @param object $video Video object returned by youtube-dl
* @param string $format Requested format
*
* @throws \Exception If the popen stream was not created correctly
* @throws Exception If the popen stream was not created correctly
*
* @return resource
*/
public function getPlaylistArchiveStream(\stdClass $video, $format)
public function getPlaylistArchiveStream(stdClass $video, $format)
{
$playlistItems = [];
foreach ($video->entries as $entry) {
@ -458,7 +461,7 @@ class VideoDownload
}
$stream = fopen('playlist://'.implode(';', $playlistItems).'/'.$format, 'r');
if (!is_resource($stream)) {
throw new \Exception(_('Could not open fopen stream.'));
throw new Exception(_('Could not open fopen stream.'));
}
return $stream;
@ -473,8 +476,8 @@ class VideoDownload
* @param string $filetype Filetype of the converted file
* @param string $password Video password
*
* @throws \Exception If your try to convert and M3U8 video
* @throws \Exception If the popen stream was not created correctly
* @throws Exception If your try to convert and M3U8 video
* @throws Exception If the popen stream was not created correctly
*
* @return resource popen stream
*/
@ -482,7 +485,7 @@ class VideoDownload
{
$video = $this->getJSON($url, $format, $password);
if (in_array($video->protocol, ['m3u8', 'm3u8_native'])) {
throw(new \Exception(_('Conversion of M3U8 files is not supported.')));
throw new Exception(_('Conversion of M3U8 files is not supported.'));
}
$avconvProc = $this->getAvconvProcess($video, $audioBitrate, $filetype, false);
@ -490,7 +493,7 @@ class VideoDownload
$stream = popen($avconvProc->getCommandLine(), 'r');
if (!is_resource($stream)) {
throw new \Exception(_('Could not open popen stream.'));
throw new Exception(_('Could not open popen stream.'));
}
return $stream;

View file

@ -10,11 +10,16 @@ use Alltube\Locale;
use Alltube\LocaleManager;
use Alltube\PasswordException;
use Alltube\VideoDownload;
use Aura\Session\Segment;
use Aura\Session\SessionFactory;
use Exception;
use GuzzleHttp\Client;
use Psr\Container\ContainerInterface;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Stream;
use Slim\Views\Smarty;
/**
* Main controller.
@ -45,14 +50,14 @@ class FrontController
/**
* Session segment used to store session variables.
*
* @var \Aura\Session\Segment
* @var Segment
*/
private $sessionSegment;
/**
* Smarty view.
*
* @var \Slim\Views\Smarty
* @var Smarty
*/
private $view;
@ -88,9 +93,9 @@ class FrontController
$this->container = $container;
$this->view = $this->container->get('view');
$this->localeManager = $this->container->get('locale');
$session_factory = new \Aura\Session\SessionFactory();
$session_factory = new SessionFactory();
$session = $session_factory->newInstance($cookies);
$this->sessionSegment = $session->getSegment('Alltube\Controller\FrontController');
$this->sessionSegment = $session->getSegment(self::class);
if ($this->config->stream) {
$this->defaultFormat = 'best';
}
@ -214,7 +219,7 @@ class FrontController
}
} catch (PasswordException $e) {
return $this->password($request, $response);
} catch (\Exception $e) {
} catch (Exception $e) {
$response = $response->withHeader(
'Content-Disposition',
'attachment; filename="'.
@ -311,13 +316,13 @@ class FrontController
/**
* Display an error page.
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param \Exception $exception Error to display
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param Exception $exception Error to display
*
* @return Response HTTP response
*/
public function error(Request $request, Response $response, \Exception $exception)
public function error(Request $request, Response $response, Exception $exception)
{
$this->view->render(
$response,
@ -366,7 +371,7 @@ class FrontController
$response = $response->withHeader('Content-Type', 'video/'.$video->ext);
$body = new Stream($stream);
} else {
$client = new \GuzzleHttp\Client();
$client = new Client();
$stream = $client->request('GET', $video->url, ['stream' => true]);
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
@ -397,7 +402,7 @@ class FrontController
private function getRemuxStream(array $urls, $format, Response $response, Request $request)
{
if (!$this->config->remux) {
throw new \Exception(_('You need to enable remux mode to merge two formats.'));
throw new Exception(_('You need to enable remux mode to merge two formats.'));
}
$stream = $this->download->getRemuxStream($urls);
$response = $response->withHeader('Content-Type', 'video/x-matroska');
@ -464,7 +469,7 @@ class FrontController
);
} else {
if (empty($videoUrls[0])) {
throw new \Exception(_("Can't find URL of video."));
throw new Exception(_("Can't find URL of video."));
}
return $response->withRedirect($videoUrls[0]);
@ -533,7 +538,7 @@ class FrontController
return $response->withRedirect(
$this->container->get('router')->pathFor('video').'?url='.urlencode($params['url'])
);
} catch (\Exception $e) {
} catch (Exception $e) {
$response->getBody()->write($e->getMessage());
return $response->withHeader('Content-Type', 'text/plain')->withStatus(500);

View file

@ -21,7 +21,7 @@ msgstr " :"
#: templates/playlist.tpl:26 templates/password.tpl:10 templates/video.tpl:97
#: templates/video.tpl:100 templates/index.tpl:19
#: controllers/FrontController.php:265
#: controllers/FrontController.php:270
msgid "Download"
msgstr "Télécharger"
@ -41,7 +41,7 @@ msgstr "L'accès à cette vidéo nécessite un mot de passe."
msgid "Video password"
msgstr "Mot de passe de la vidéo"
#: templates/extractors.tpl:4 controllers/FrontController.php:159
#: templates/extractors.tpl:4 controllers/FrontController.php:164
msgid "Supported websites"
msgstr "Sites web supportés"
@ -145,65 +145,65 @@ msgstr "kbit/s"
msgid "with"
msgstr "avec de l'audio à"
#: classes/VideoDownload.php:114
#: classes/VideoDownload.php:117
msgid "Wrong password"
msgstr "Mauvais mot de passe"
#: classes/VideoDownload.php:270 classes/VideoDownload.php:353
msgid "Can't find avconv or ffmpeg."
msgstr "Impossible de trouver avconv ou ffmpeg."
#: classes/VideoDownload.php:326 classes/VideoDownload.php:485
#: classes/VideoDownload.php:329 classes/VideoDownload.php:488
msgid "Conversion of M3U8 files is not supported."
msgstr "La conversion des fichiers M3U8 n'est pas possible."
#: classes/VideoDownload.php:334 classes/VideoDownload.php:371
#: classes/VideoDownload.php:404 classes/VideoDownload.php:437
#: classes/VideoDownload.php:493
#: classes/VideoDownload.php:337 classes/VideoDownload.php:374
#: classes/VideoDownload.php:407 classes/VideoDownload.php:440
#: classes/VideoDownload.php:496
msgid "Could not open popen stream."
msgstr "Impossible d'ouvrir le flux popen."
#: classes/VideoDownload.php:461
#: classes/VideoDownload.php:464
msgid "Could not open fopen stream."
msgstr "Impossible d'ouvrir le flux fopen."
#: controllers/FrontController.php:116
#: controllers/FrontController.php:121
msgid "Easily download videos from Youtube, Dailymotion, Vimeo and other websites."
msgstr "Téléchargez facilement des vidéos depuis Youtube, Dailymotion, Vimeo et d'autres sites web."
#: controllers/FrontController.php:160
#: controllers/FrontController.php:165
msgid "List of all supported websites from which Alltube Download can extract video or audio files"
msgstr "Liste de tous les sites web depuis lesquels Alltube Download peut extraire des fichiers vidéo ou audio"
#: controllers/FrontController.php:185
#: controllers/FrontController.php:190
msgid "Password prompt"
msgstr "Demande de mot de passe"
#: controllers/FrontController.php:186
#: controllers/FrontController.php:191
msgid "You need a password in order to download this video with Alltube Download"
msgstr "Vous avez besoin d'un mot de passe pour télécharger cette vidéo avec Alltube Download"
#: controllers/FrontController.php:261
#: controllers/FrontController.php:266
msgid "Video download"
msgstr "Téléchargement d'une vidéo"
#: controllers/FrontController.php:262
#: controllers/FrontController.php:267
msgid "Download video from "
msgstr "Téléchargement d'une vidéo depuis "
#: controllers/FrontController.php:265
#: controllers/FrontController.php:270
msgid "from"
msgstr "depuis"
#: controllers/FrontController.php:328
#: controllers/FrontController.php:333
msgid "Error"
msgstr "Erreur"
#: controllers/FrontController.php:400
#: controllers/FrontController.php:405
msgid "You need to enable remux mode to merge two formats."
msgstr "Vous devez activer le mode remux pour fusionner deux formats."
#: controllers/FrontController.php:467
#: controllers/FrontController.php:472
msgid "Can't find URL of video."
msgstr "Impossible de trouver l'URL de la vidéo."
#: classes/VideoDownload.php:273 classes/VideoDownload.php:356
msgid "Can't find avconv or ffmpeg at "
msgstr "Impossible de trouver avconv ou ffmpeg à "

View file

@ -19,7 +19,7 @@ msgstr ""
#: templates/playlist.tpl:26 templates/password.tpl:10 templates/video.tpl:97
#: templates/video.tpl:100 templates/index.tpl:19
#: controllers/FrontController.php:265
#: controllers/FrontController.php:270
msgid "Download"
msgstr ""
@ -39,7 +39,7 @@ msgstr ""
msgid "Video password"
msgstr ""
#: templates/extractors.tpl:4 controllers/FrontController.php:159
#: templates/extractors.tpl:4 controllers/FrontController.php:164
msgid "Supported websites"
msgstr ""
@ -139,68 +139,68 @@ msgstr ""
msgid "Bookmarklet"
msgstr ""
#: classes/VideoDownload.php:114
#: classes/VideoDownload.php:117
msgid "Wrong password"
msgstr ""
#: classes/VideoDownload.php:270 classes/VideoDownload.php:353
msgid "Can't find avconv or ffmpeg."
#: classes/VideoDownload.php:273 classes/VideoDownload.php:356
msgid "Can't find avconv or ffmpeg at "
msgstr ""
#: classes/VideoDownload.php:326 classes/VideoDownload.php:485
#: classes/VideoDownload.php:329 classes/VideoDownload.php:488
msgid "Conversion of M3U8 files is not supported."
msgstr ""
#: classes/VideoDownload.php:334 classes/VideoDownload.php:371
#: classes/VideoDownload.php:404 classes/VideoDownload.php:437
#: classes/VideoDownload.php:493
#: classes/VideoDownload.php:337 classes/VideoDownload.php:374
#: classes/VideoDownload.php:407 classes/VideoDownload.php:440
#: classes/VideoDownload.php:496
msgid "Could not open popen stream."
msgstr ""
#: classes/VideoDownload.php:461
#: classes/VideoDownload.php:464
msgid "Could not open fopen stream."
msgstr ""
#: controllers/FrontController.php:116
#: controllers/FrontController.php:121
msgid ""
"Easily download videos from Youtube, Dailymotion, Vimeo and other websites."
msgstr ""
#: controllers/FrontController.php:160
#: controllers/FrontController.php:165
msgid ""
"List of all supported websites from which Alltube Download can extract video "
"or audio files"
msgstr ""
#: controllers/FrontController.php:185
#: controllers/FrontController.php:190
msgid "Password prompt"
msgstr ""
#: controllers/FrontController.php:186
#: controllers/FrontController.php:191
msgid ""
"You need a password in order to download this video with Alltube Download"
msgstr ""
#: controllers/FrontController.php:261
#: controllers/FrontController.php:266
msgid "Video download"
msgstr ""
#: controllers/FrontController.php:262
#: controllers/FrontController.php:267
msgid "Download video from "
msgstr ""
#: controllers/FrontController.php:265
#: controllers/FrontController.php:270
msgid "from"
msgstr ""
#: controllers/FrontController.php:328
#: controllers/FrontController.php:333
msgid "Error"
msgstr ""
#: controllers/FrontController.php:400
#: controllers/FrontController.php:405
msgid "You need to enable remux mode to merge two formats."
msgstr ""
#: controllers/FrontController.php:467
#: controllers/FrontController.php:472
msgid "Can't find URL of video."
msgstr ""

View file

@ -58,6 +58,6 @@ $app->get(
try {
$app->run();
} catch (\SmartyException $e) {
} catch (SmartyException $e) {
die('Smarty could not compile the template file: '.$e->getMessage());
}

View file

@ -9,6 +9,7 @@ use Alltube\Config;
use Alltube\Controller\FrontController;
use Alltube\LocaleManager;
use Alltube\ViewFactory;
use Exception;
use PHPUnit\Framework\TestCase;
use Slim\Container;
use Slim\Http\Environment;
@ -348,7 +349,7 @@ class FrontControllerTest extends TestCase
*/
public function testError()
{
$result = $this->controller->error($this->request, $this->response, new \Exception('foo'));
$result = $this->controller->error($this->request, $this->response, new Exception('foo'));
$this->assertTrue($result->isServerError());
}

View file

@ -27,7 +27,7 @@ class LocaleManagerTest extends TestCase
protected function setUp()
{
$this->localeManager = new LocaleManager();
$_SESSION['Alltube\LocaleManager']['locale'] = 'foo_BAR';
$_SESSION[LocaleManager::class]['locale'] = 'foo_BAR';
}
/**

View file

@ -126,7 +126,7 @@ class VideoDownloadTest extends TestCase
* Test getURL function with a protected video and no password.
*
* @return void
* @expectedException \Alltube\PasswordException
* @expectedException Alltube\PasswordException
*/
public function testGetURLWithMissingPassword()
{
@ -198,8 +198,8 @@ class VideoDownloadTest extends TestCase
'edgefcs.net',
],
[
'https://openload.co/embed/qTsjMEUtN4U', 'best[protocol^=http]',
'aup-the-lego-ninjago-movie-2017-1508463762.MP4.mp4-qTsjMEUtN4U',
'https://openload.co/f/kUEfGclsU9o', 'best[protocol^=http]',
'skyrim_no-audio_1080.mp4-kUEfGclsU9o',
'mp4',
'openload.co',
],