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-*/
alltube-release.zip
coverage/

18
api.php
View file

@ -15,9 +15,9 @@ $smarty->assign('class', 'video');
require_once 'download.php';
if (isset($_GET["url"])) {
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
$UA = VideoDownload::getUA();
ini_set(
@ -61,24 +61,24 @@ if (isset($_GET["url"])) {
);
exit;
}
} else {
$error=true;
} catch (Exception $e) {
$error = $e->getMessage();
}
} else {
$video = VideoDownload::getJSON($_GET["url"]);
if (isset($video->webpage_url)) {
try {
$video = VideoDownload::getJSON($_GET["url"]);
$smarty->display('head.tpl');
$smarty->assign('video', $video);
$smarty->display('video.tpl');
$smarty->display('footer.tpl');
} else {
$error=true;
} catch (Exception $e) {
$error = $e->getMessage();
}
}
}
if (isset($error)) {
$smarty->display('head.tpl');
$smarty->assign('errors', $video['error']);
$smarty->assign('errors', $error);
$smarty->display('error.tpl');
$smarty->display('footer.tpl');
}

View file

@ -26,19 +26,6 @@
* */
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
*
@ -48,7 +35,7 @@ Class VideoDownload
{
exec(
PYTHON.' '.YOUTUBE_DL.' --dump-user-agent',
$version, $code
$version
);
return $version[0];
}
@ -62,7 +49,7 @@ Class VideoDownload
{
exec(
PYTHON.' '.YOUTUBE_DL.' --list-extractors',
$extractors, $code
$extractors
);
return $extractors;
}
@ -89,24 +76,6 @@ Class VideoDownload
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
*
@ -123,65 +92,15 @@ Class VideoDownload
}
$cmd .=' --dump-json '.escapeshellarg($url)." 2>&1";
exec(
$cmd,
$json, $code
$cmd, $result, $code
);
if ($code>0) {
return array('success'=>false, 'error'=>$json);
throw new Exception(implode(PHP_EOL, $result));
} 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
*
@ -192,18 +111,18 @@ Class VideoDownload
* */
static function getURL($url, $format=null)
{
$cmd=PYTHON.' '.YOUTUBE_DL;
$cmd=PYTHON.' '.YOUTUBE_DL.' '.PARAMS;
if (isset($format)) {
$cmd .= ' -f '.escapeshellarg($format);
}
$cmd .=' -g '.escapeshellarg($url)." 2>&1";
exec(
$cmd, $url, $code
$cmd, $result, $code
);
if ($code>0) {
return array('success'=>false, 'error'=>$url);
throw new Exception(implode(PHP_EOL, $result));
} 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';
if (isset($_GET["url"])) {
header('Content-Type: application/json');
$video = VideoDownload::getJSON($_GET["url"]);
echo json_encode($video);
try {
$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 'download.php';
if (isset($_GET["url"])) {
$video = VideoDownload::getURL($_GET["url"]);
if (isset($video['url'])) {
try {
$video = VideoDownload::getURL($_GET["url"]);
header('Location: '.$video['url']);
} else {
echo "Can't find video";
} catch (Exception $e) {
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
{
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
* @return void
@ -54,18 +43,110 @@ class VideoDownloadTest extends PHPUnit_Framework_TestCase
public function testListExtractors()
{
$extractors = VideoDownload::listExtractors();
$this->assertNotEmpty($extractors);
$this->assertInternalType('array', $extractors);
$this->assertContains('youtube', $extractors);
}
/**
* 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
*/
public function testGetURL()
public function URLProvider()
{
$url = VideoDownload::getURL(self::$_testVideoURL);
$this->assertArrayHasKey('success', $url);
$this->assertArrayHasKey('url', $url);
return array(
array(
'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);
}
}