Remove unused code

Unit tests for every function
Error handling
This commit is contained in:
Pierre Rudloff 2015-09-04 22:45:55 +02:00
parent de96f30d57
commit 11ff0fa9c4
7 changed files with 159 additions and 138 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ templates_c/
ffmpeg.tar.xz ffmpeg.tar.xz
ffmpeg-*/ ffmpeg-*/
alltube-release.zip alltube-release.zip
coverage/

18
api.php
View file

@ -15,9 +15,9 @@ $smarty->assign('class', 'video');
require_once 'download.php'; require_once 'download.php';
if (isset($_GET["url"])) { if (isset($_GET["url"])) {
if (isset($_GET['audio'])) { if (isset($_GET['audio'])) {
$video = VideoDownload::getJSON($_GET["url"]); try {
$video = VideoDownload::getJSON($_GET["url"]);
if (isset($video->url)) {
//Vimeo needs a correct user-agent //Vimeo needs a correct user-agent
$UA = VideoDownload::getUA(); $UA = VideoDownload::getUA();
ini_set( ini_set(
@ -61,24 +61,24 @@ if (isset($_GET["url"])) {
); );
exit; exit;
} }
} else { } catch (Exception $e) {
$error=true; $error = $e->getMessage();
} }
} else { } else {
$video = VideoDownload::getJSON($_GET["url"]); try {
if (isset($video->webpage_url)) { $video = VideoDownload::getJSON($_GET["url"]);
$smarty->display('head.tpl'); $smarty->display('head.tpl');
$smarty->assign('video', $video); $smarty->assign('video', $video);
$smarty->display('video.tpl'); $smarty->display('video.tpl');
$smarty->display('footer.tpl'); $smarty->display('footer.tpl');
} else { } catch (Exception $e) {
$error=true; $error = $e->getMessage();
} }
} }
} }
if (isset($error)) { if (isset($error)) {
$smarty->display('head.tpl'); $smarty->display('head.tpl');
$smarty->assign('errors', $video['error']); $smarty->assign('errors', $error);
$smarty->display('error.tpl'); $smarty->display('error.tpl');
$smarty->display('footer.tpl'); $smarty->display('footer.tpl');
} }

View file

@ -2,9 +2,9 @@
/** /**
* PHP web interface for youtube-dl (http://rg3.github.com/youtube-dl/) * PHP web interface for youtube-dl (http://rg3.github.com/youtube-dl/)
* Main class * Main class
* *
* PHP Version 5.3.10 * PHP Version 5.3.10
* *
* @category Youtube-dl * @category Youtube-dl
* @package Youtubedl * @package Youtubedl
* @author Pierre Rudloff <contact@rudloff.pro> * @author Pierre Rudloff <contact@rudloff.pro>
@ -15,9 +15,9 @@
/** /**
* PHP web interface for youtube-dl (http://rg3.github.com/youtube-dl/) * PHP web interface for youtube-dl (http://rg3.github.com/youtube-dl/)
* Main class * Main class
* *
* PHP Version 5.3.10 * PHP Version 5.3.10
* *
* @category Youtube-dl * @category Youtube-dl
* @package Youtubedl * @package Youtubedl
* @author Pierre Rudloff <contact@rudloff.pro> * @author Pierre Rudloff <contact@rudloff.pro>
@ -26,53 +26,40 @@
* */ * */
Class VideoDownload Class VideoDownload
{ {
/**
* Get version of youtube-dl
*
* @return string Version
* */
static function getVersion()
{
exec(
PYTHON.' '.YOUTUBE_DL.' --version',
$version, $code
);
return $version[0];
}
/** /**
* Get the user agent used youtube-dl * Get the user agent used youtube-dl
* *
* @return string UA * @return string UA
* */ * */
static function getUA() static function getUA()
{ {
exec( exec(
PYTHON.' '.YOUTUBE_DL.' --dump-user-agent', PYTHON.' '.YOUTUBE_DL.' --dump-user-agent',
$version, $code $version
); );
return $version[0]; return $version[0];
} }
/** /**
* List all extractors * List all extractors
* *
* @return array Extractors * @return array Extractors
* */ * */
static function listExtractors() static function listExtractors()
{ {
exec( exec(
PYTHON.' '.YOUTUBE_DL.' --list-extractors', PYTHON.' '.YOUTUBE_DL.' --list-extractors',
$extractors, $code $extractors
); );
return $extractors; return $extractors;
} }
/** /**
* Get filename of video * Get filename of video
* *
* @param string $url URL of page * @param string $url URL of page
* @param string $format Format to use for the video * @param string $format Format to use for the video
* *
* @return string Filename * @return string Filename
* */ * */
static function getFilename($url, $format=null) static function getFilename($url, $format=null)
@ -88,31 +75,13 @@ Class VideoDownload
); );
return end($filename); return end($filename);
} }
/**
* Get title of video
*
* @param string $url URL of page
*
* @return string Title
* */
static function getTitle($url)
{
exec(
PYTHON.' '.YOUTUBE_DL.' --get-title '.
escapeshellarg($url),
$title
);
$title=$title[0];
return $title;
}
/** /**
* Get all information about a video * Get all information about a video
* *
* @param string $url URL of page * @param string $url URL of page
* @param string $format Format to use for the video * @param string $format Format to use for the video
* *
* @return string JSON * @return string JSON
* */ * */
static function getJSON($url, $format=null) static function getJSON($url, $format=null)
@ -123,88 +92,38 @@ Class VideoDownload
} }
$cmd .=' --dump-json '.escapeshellarg($url)." 2>&1"; $cmd .=' --dump-json '.escapeshellarg($url)." 2>&1";
exec( exec(
$cmd, $cmd, $result, $code
$json, $code
); );
if ($code>0) { if ($code>0) {
return array('success'=>false, 'error'=>$json); throw new Exception(implode(PHP_EOL, $result));
} else { } else {
return json_decode($json[0]); return json_decode($result[0]);
} }
} }
/**
* Get thumbnail of video
*
* @param string $url URL of page
*
* @return string URL of image
* */
static function getThumbnail($url)
{
exec(
PYTHON.' '.YOUTUBE_DL.' --get-thumbnail '.
escapeshellarg($url),
$thumb
);
if (isset($thumb[0])) {
return $thumb[0];
}
}
/**
* Get a list available formats for this video
*
* @param string $url URL of page
*
* @return string Title
* */
static function getAvailableFormats($url)
{
exec(
PYTHON.' '.YOUTUBE_DL.' -F '.
escapeshellarg($url),
$formats
);
$return=array();
foreach ($formats as $i=>$format) {
if ($i > 4) {
$return[]=(preg_split('/(\s\s+)|(\s+:?\s+)|(\s+\[)|\]/', $format));
}
}
if (empty($return)) {
foreach ($formats as $i=>$format) {
if ($i > 3) {
$return[]=preg_split('/(\s\s+)|(\s+:?\s+)|(\s+\[)|\]/', $format);
}
}
}
return $return;
}
/** /**
* Get URL of video from URL of page * Get URL of video from URL of page
* *
* @param string $url URL of page * @param string $url URL of page
* @param string $format Format to use for the video * @param string $format Format to use for the video
* *
* @return string URL of video * @return string URL of video
* */ * */
static function getURL($url, $format=null) static function getURL($url, $format=null)
{ {
$cmd=PYTHON.' '.YOUTUBE_DL; $cmd=PYTHON.' '.YOUTUBE_DL.' '.PARAMS;
if (isset($format)) { if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format); $cmd .= ' -f '.escapeshellarg($format);
} }
$cmd .=' -g '.escapeshellarg($url)." 2>&1"; $cmd .=' -g '.escapeshellarg($url)." 2>&1";
exec( exec(
$cmd, $url, $code $cmd, $result, $code
); );
if ($code>0) { if ($code>0) {
return array('success'=>false, 'error'=>$url); throw new Exception(implode(PHP_EOL, $result));
} else { } else {
return array('success'=>true, 'url'=>end($url)); return array('success'=>true, 'url'=>end($result));
} }
} }
} }

View file

@ -15,6 +15,10 @@ require_once 'common.php';
require_once 'download.php'; require_once 'download.php';
if (isset($_GET["url"])) { if (isset($_GET["url"])) {
header('Content-Type: application/json'); header('Content-Type: application/json');
$video = VideoDownload::getJSON($_GET["url"]); try {
echo json_encode($video); $video = VideoDownload::getJSON($_GET["url"]);
echo json_encode($video);
} catch (Exception $e) {
echo json_encode(array('success'=>false, 'error'=>$e->getMessage()));
}
} }

15
phpunit.xml Normal file
View file

@ -0,0 +1,15 @@
<phpunit>
<filter>
<whitelist>
<file>download.php</file>
</whitelist>
</filter>
<testsuites>
<testsuite>
<directory>tests/</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-html" target="coverage/" />
</logging>
</phpunit>

View file

@ -14,10 +14,11 @@
require_once 'common.php'; require_once 'common.php';
require_once 'download.php'; require_once 'download.php';
if (isset($_GET["url"])) { if (isset($_GET["url"])) {
$video = VideoDownload::getURL($_GET["url"]); try {
if (isset($video['url'])) { $video = VideoDownload::getURL($_GET["url"]);
header('Location: '.$video['url']); header('Location: '.$video['url']);
} else { } catch (Exception $e) {
echo "Can't find video"; header('Content-Type: text/plain');
echo $e->getMessage();
} }
} }

View file

@ -27,17 +27,6 @@ require_once __DIR__.'/../download.php';
* */ * */
class VideoDownloadTest extends PHPUnit_Framework_TestCase class VideoDownloadTest extends PHPUnit_Framework_TestCase
{ {
static private $_testVideoURL = 'https://www.youtube.com/watch?v=RJJ6FCAXvKg';
/**
* Test getVersion function
* @return void
*/
public function testGetVersion()
{
$this->assertStringMatchesFormat('%i.%i.%i', VideoDownload::getVersion());
}
/** /**
* Test getUA function * Test getUA function
* @return void * @return void
@ -54,18 +43,110 @@ class VideoDownloadTest extends PHPUnit_Framework_TestCase
public function testListExtractors() public function testListExtractors()
{ {
$extractors = VideoDownload::listExtractors(); $extractors = VideoDownload::listExtractors();
$this->assertNotEmpty($extractors); $this->assertContains('youtube', $extractors);
$this->assertInternalType('array', $extractors);
} }
/** /**
* Test getURL function * Test getURL function
* @param string $url URL
* @param string $format Format
* @return void
* @dataProvider URLProvider
*/
public function testGetURL($url, $format)
{
$videoURL = VideoDownload::getURL($url, $format);
$this->assertArrayHasKey('success', $videoURL);
$this->assertArrayHasKey('url', $videoURL);
}
/**
* Test getURL function errors
* @param string $url URL
* @return void
* @expectedException Exception
* @dataProvider ErrorURLProvider
*/
public function testGetURLError($url)
{
$videoURL = VideoDownload::getURL($url);
}
/**
* Provides URLs for tests
* @return void * @return void
*/ */
public function testGetURL() public function URLProvider()
{ {
$url = VideoDownload::getURL(self::$_testVideoURL); return array(
$this->assertArrayHasKey('success', $url); array(
$this->assertArrayHasKey('url', $url); 'https://www.youtube.com/watch?v=M7IpKCZ47pU', null,
"It's Not Me, It's You - Hearts Under Fire-M7IpKCZ47pU.mp4"
),
array(
'https://www.youtube.com/watch?v=RJJ6FCAXvKg', 22,
"'Heart Attack' - Demi Lovato ".
"(Sam Tsui & Against The Current)-RJJ6FCAXvKg.mp4"
),
array(
'https://vimeo.com/24195442', null,
"Carving the Mountains-24195442.mp4"
),
);
}
/**
* Provides incorrect URLs for tests
* @return void
*/
public function errorURLProvider()
{
return array(
array('http://example.com/video')
);
}
/**
* Test getFilename function
* @param string $url URL
* @param string $format Format
* @param string $result Expected filename
* @return void
* @dataProvider URLProvider
*/
public function testGetFilename($url, $format, $result)
{
$filename = VideoDownload::getFilename($url, $format);
$this->assertEquals($filename, $result);
}
/**
* Test getJSON function
* @param string $url URL
* @param string $format Format
* @return void
* @dataProvider URLProvider
*/
public function testGetJSON($url, $format)
{
$info = VideoDownload::getJSON($url, $format);
$this->assertObjectHasAttribute('webpage_url', $info);
$this->assertObjectHasAttribute('url', $info);
$this->assertObjectHasAttribute('ext', $info);
$this->assertObjectHasAttribute('title', $info);
$this->assertObjectHasAttribute('formats', $info);
$this->assertObjectHasAttribute('_filename', $info);
}
/**
* Test getJSON function errors
* @param string $url URL
* @return void
* @expectedException Exception
* @dataProvider ErrorURLProvider
*/
public function testGetJSONError($url)
{
$videoURL = VideoDownload::getJSON($url);
} }
} }