Don't use static functions

This commit is contained in:
Pierre Rudloff 2016-04-08 19:06:41 +02:00
parent 7eef219128
commit 11e8243443
4 changed files with 64 additions and 52 deletions

View file

@ -27,17 +27,21 @@ use Symfony\Component\Process\Process;
* */ * */
class VideoDownload class VideoDownload
{ {
public function __construct()
{
$this->config = Config::getInstance();
}
/** /**
* Get the user agent used youtube-dl * Get the user agent used youtube-dl
* *
* @return string UA * @return string UA
* */ * */
public static function getUA() public function getUA()
{ {
$config = Config::getInstance();
$cmd = escapeshellcmd( $cmd = escapeshellcmd(
$config->python.' '.escapeshellarg($config->youtubedl). $this->config->python.' '.escapeshellarg($this->config->youtubedl).
' '.$config->params ' '.$this->config->params
); );
$process = new Process($cmd.' --dump-user-agent'); $process = new Process($cmd.' --dump-user-agent');
$process->run(); $process->run();
@ -49,12 +53,11 @@ class VideoDownload
* *
* @return array Extractors * @return array Extractors
* */ * */
public static function listExtractors() public function listExtractors()
{ {
$config = Config::getInstance();
$cmd = escapeshellcmd( $cmd = escapeshellcmd(
$config->python.' '.escapeshellarg($config->youtubedl). $this->config->python.' '.escapeshellarg($this->config->youtubedl).
' '.$config->params ' '.$this->config->params
); );
$process = new Process($cmd.' --list-extractors'); $process = new Process($cmd.' --list-extractors');
$process->run(); $process->run();
@ -69,12 +72,11 @@ class VideoDownload
* *
* @return string Filename * @return string Filename
* */ * */
public static function getFilename($url, $format = null) public function getFilename($url, $format = null)
{ {
$config = Config::getInstance();
$cmd = escapeshellcmd( $cmd = escapeshellcmd(
$config->python.' '.escapeshellarg($config->youtubedl). $this->config->python.' '.escapeshellarg($this->config->youtubedl).
' '.$config->params ' '.$this->config->params
); );
if (isset($format)) { if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format); $cmd .= ' -f '.escapeshellarg($format);
@ -93,12 +95,11 @@ class VideoDownload
* *
* @return string JSON * @return string JSON
* */ * */
public static function getJSON($url, $format = null) public function getJSON($url, $format = null)
{ {
$config = Config::getInstance();
$cmd = escapeshellcmd( $cmd = escapeshellcmd(
$config->python.' '.escapeshellarg($config->youtubedl). $this->config->python.' '.escapeshellarg($this->config->youtubedl).
' '.$config->params ' '.$this->config->params
); );
if (isset($format)) { if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format); $cmd .= ' -f '.escapeshellarg($format);
@ -121,12 +122,11 @@ class VideoDownload
* *
* @return string URL of video * @return string URL of video
* */ * */
public static function getURL($url, $format = null) public function getURL($url, $format = null)
{ {
$config = Config::getInstance();
$cmd = escapeshellcmd( $cmd = escapeshellcmd(
$config->python.' '.escapeshellarg($config->youtubedl). $this->config->python.' '.escapeshellarg($this->config->youtubedl).
' '.$config->params ' '.$this->config->params
); );
if (isset($format)) { if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format); $cmd .= ' -f '.escapeshellarg($format);

View file

@ -28,6 +28,11 @@ use Alltube\Config;
* */ * */
class FrontController class FrontController
{ {
public function __construct()
{
$this->config = Config::getInstance();
$this->download = new VideoDownload();
}
/** /**
* Display index page * Display index page
@ -37,10 +42,9 @@ class FrontController
* *
* @return void * @return void
*/ */
public static function index($request, $response) public function index($request, $response)
{ {
global $container; global $container;
$config = Config::getInstance();
$container->view->render( $container->view->render(
$response, $response,
'head.tpl', 'head.tpl',
@ -56,7 +60,7 @@ class FrontController
$response, $response,
'index.tpl', 'index.tpl',
array( array(
'convert'=>$config->convert 'convert'=>$this->config->convert
) )
); );
$container->view->render($response, 'footer.tpl'); $container->view->render($response, 'footer.tpl');
@ -70,7 +74,7 @@ class FrontController
* *
* @return void * @return void
*/ */
public static function extractors($request, $response) public function extractors($request, $response)
{ {
global $container; global $container;
$container->view->render( $container->view->render(
@ -86,7 +90,7 @@ class FrontController
$response, $response,
'extractors.tpl', 'extractors.tpl',
array( array(
'extractors'=>VideoDownload::listExtractors() 'extractors'=>$this->download->listExtractors()
) )
); );
$container->view->render($response, 'footer.tpl'); $container->view->render($response, 'footer.tpl');
@ -100,17 +104,17 @@ class FrontController
* *
* @return void * @return void
*/ */
public static function video($request, $response) public function video($request, $response)
{ {
global $container; global $container;
$config = Config::getInstance(); $this->config = Config::getInstance();
if (isset($_GET["url"])) { if (isset($_GET["url"])) {
if (isset($_GET['audio'])) { if (isset($_GET['audio'])) {
try { try {
$video = VideoDownload::getJSON($_GET["url"]); $video = $this->download->getJSON($_GET["url"]);
//Vimeo needs a correct user-agent //Vimeo needs a correct user-agent
$UA = VideoDownload::getUA(); $UA = $this->download->getUA();
ini_set( ini_set(
'user_agent', 'user_agent',
$UA $UA
@ -122,7 +126,7 @@ class FrontController
'Content-Disposition: attachment; filename="'. 'Content-Disposition: attachment; filename="'.
html_entity_decode( html_entity_decode(
pathinfo( pathinfo(
VideoDownload::getFilename( $this->download->getFilename(
$video->webpage_url $video->webpage_url
), ),
PATHINFO_FILENAME PATHINFO_FILENAME
@ -134,7 +138,7 @@ class FrontController
header("Content-Type: audio/mpeg"); header("Content-Type: audio/mpeg");
passthru( passthru(
'/usr/bin/rtmpdump -q -r '.escapeshellarg($video->url). '/usr/bin/rtmpdump -q -r '.escapeshellarg($video->url).
' | '.$config->avconv. ' | '.$this->config->avconv.
' -v quiet -i - -f mp3 -vn pipe:1' ' -v quiet -i - -f mp3 -vn pipe:1'
); );
exit; exit;
@ -144,7 +148,7 @@ class FrontController
'Content-Disposition: attachment; filename="'. 'Content-Disposition: attachment; filename="'.
html_entity_decode( html_entity_decode(
pathinfo( pathinfo(
VideoDownload::getFilename( $this->download->getFilename(
$video->webpage_url $video->webpage_url
), ),
PATHINFO_FILENAME PATHINFO_FILENAME
@ -155,10 +159,10 @@ class FrontController
); );
header("Content-Type: audio/mpeg"); header("Content-Type: audio/mpeg");
passthru( passthru(
'curl '.$config->curl_params. 'curl '.$this->config->curl_params.
' --user-agent '.escapeshellarg($UA). ' --user-agent '.escapeshellarg($UA).
' '.escapeshellarg($video->url). ' '.escapeshellarg($video->url).
' | '.$config->avconv. ' | '.$this->config->avconv.
' -v quiet -i - -f mp3 -vn pipe:1' ' -v quiet -i - -f mp3 -vn pipe:1'
); );
exit; exit;
@ -168,7 +172,7 @@ class FrontController
} }
} else { } else {
try { try {
$video = VideoDownload::getJSON($_GET["url"]); $video = $this->download->getJSON($_GET["url"]);
$container->view->render( $container->view->render(
$response, $response,
'head.tpl', 'head.tpl',
@ -216,12 +220,12 @@ class FrontController
* *
* @return void * @return void
*/ */
public static function redirect($request, $response) public function redirect($request, $response)
{ {
global $app; global $app;
if (isset($_GET["url"])) { if (isset($_GET["url"])) {
try { try {
$video = VideoDownload::getURL($_GET["url"]); $video = $this->download->getURL($_GET["url"]);
return $response->withRedirect($video['url']); return $response->withRedirect($video['url']);
} catch (\Exception $e) { } catch (\Exception $e) {
echo $e->getMessage().PHP_EOL; echo $e->getMessage().PHP_EOL;
@ -238,12 +242,12 @@ class FrontController
* *
* @return void * @return void
*/ */
public static function json($request, $response) public function json($request, $response)
{ {
global $app; global $app;
if (isset($_GET["url"])) { if (isset($_GET["url"])) {
try { try {
$video = VideoDownload::getJSON($_GET["url"]); $video = $this->download->getJSON($_GET["url"]);
return $response->withJson($video); return $response->withJson($video);
} catch (\Exception $e) { } catch (\Exception $e) {
return $response->withJson( return $response->withJson(

View file

@ -15,6 +15,7 @@
require_once __DIR__.'/vendor/autoload.php'; require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/vendor/rudloff/smarty-plugin-noscheme/modifier.noscheme.php'; require_once __DIR__.'/vendor/rudloff/smarty-plugin-noscheme/modifier.noscheme.php';
use Alltube\VideoDownload; use Alltube\VideoDownload;
use Alltube\Controller\FrontController;
$app = new \Slim\App(); $app = new \Slim\App();
$container = $app->getContainer(); $container = $app->getContainer();
@ -28,24 +29,26 @@ $container['view'] = function ($c) {
return $view; return $view;
}; };
$controller = new FrontController();
$app->get( $app->get(
'/', '/',
array('Alltube\Controller\FrontController', 'index') array($controller, 'index')
); );
$app->get( $app->get(
'/extractors', '/extractors',
array('Alltube\Controller\FrontController', 'extractors') array($controller, 'extractors')
)->setName('extractors'); )->setName('extractors');
$app->get( $app->get(
'/video', '/video',
array('Alltube\Controller\FrontController', 'video') array($controller, 'video')
)->setName('video'); )->setName('video');
$app->get( $app->get(
'/redirect', '/redirect',
array('Alltube\Controller\FrontController', 'redirect') array($controller, 'redirect')
); );
$app->get( $app->get(
'/json', '/json',
array('Alltube\Controller\FrontController', 'json') array($controller, 'json')
); );
$app->run(); $app->run();

View file

@ -27,6 +27,11 @@ use Alltube\VideoDownload;
* */ * */
class VideoDownloadTest extends \PHPUnit_Framework_TestCase class VideoDownloadTest extends \PHPUnit_Framework_TestCase
{ {
protected function setUp()
{
$this->download = new VideoDownload();
}
/** /**
* Test getUA function * Test getUA function
* *
@ -34,7 +39,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetUA() public function testGetUA()
{ {
$this->assertStringStartsWith('Mozilla/', VideoDownload::getUA()); $this->assertStringStartsWith('Mozilla/', $this->download->getUA());
} }
/** /**
@ -44,7 +49,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase
*/ */
public function testListExtractors() public function testListExtractors()
{ {
$extractors = VideoDownload::listExtractors(); $extractors = $this->download->listExtractors();
$this->assertContains('youtube', $extractors); $this->assertContains('youtube', $extractors);
} }
@ -59,7 +64,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetURL($url, $format) public function testGetURL($url, $format)
{ {
$videoURL = VideoDownload::getURL($url, $format); $videoURL = $this->download->getURL($url, $format);
$this->assertArrayHasKey('success', $videoURL); $this->assertArrayHasKey('success', $videoURL);
$this->assertArrayHasKey('url', $videoURL); $this->assertArrayHasKey('url', $videoURL);
} }
@ -75,7 +80,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetURLError($url) public function testGetURLError($url)
{ {
$videoURL = VideoDownload::getURL($url); $videoURL = $this->download->getURL($url);
} }
/** /**
@ -126,7 +131,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetFilename($url, $format, $result) public function testGetFilename($url, $format, $result)
{ {
$filename = VideoDownload::getFilename($url, $format); $filename = $this->download->getFilename($url, $format);
$this->assertEquals($filename, $result); $this->assertEquals($filename, $result);
} }
@ -141,7 +146,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetJSON($url, $format) public function testGetJSON($url, $format)
{ {
$info = VideoDownload::getJSON($url, $format); $info = $this->download->getJSON($url, $format);
$this->assertObjectHasAttribute('webpage_url', $info); $this->assertObjectHasAttribute('webpage_url', $info);
$this->assertObjectHasAttribute('url', $info); $this->assertObjectHasAttribute('url', $info);
$this->assertObjectHasAttribute('ext', $info); $this->assertObjectHasAttribute('ext', $info);
@ -161,6 +166,6 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetJSONError($url) public function testGetJSONError($url)
{ {
$videoURL = VideoDownload::getJSON($url); $videoURL = $this->download->getJSON($url);
} }
} }