From 43cbd4f6fe89d9f6bc603942ce390f02b5c99ebf Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Tue, 25 Apr 2017 01:53:38 +0200 Subject: [PATCH] Cleanup playlists --- classes/Config.php | 2 +- classes/VideoDownload.php | 9 +- controllers/FrontController.php | 21 ++-- css/style.css | 24 +++++ index.php | 1 + js/cast.js | 34 +++---- templates/playlist.tpl | 19 ++++ templates/video.tpl | 169 ++++++++++++++++---------------- tests/FrontControllerTest.php | 16 +++ tests/VideoDownloadTest.php | 1 - 10 files changed, 175 insertions(+), 121 deletions(-) create mode 100644 templates/playlist.tpl diff --git a/classes/Config.php b/classes/Config.php index 4ee07c1..b90a6ca 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -38,7 +38,7 @@ class Config * * @var array */ - public $params = ['--no-playlist', '--no-warnings', '--playlist-end', 1]; + public $params = ['--no-warnings']; /** * Enable audio conversion. diff --git a/classes/VideoDownload.php b/classes/VideoDownload.php index f773d2c..a838303 100644 --- a/classes/VideoDownload.php +++ b/classes/VideoDownload.php @@ -113,14 +113,7 @@ class VideoDownload * */ public function getJSON($url, $format = null, $password = null) { - $jsonArray = preg_split( "/\r|\n/", $this->getProp($url, $format, 'dump-json', $password), -1, - PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); - $decodedJson = array(); - foreach ($jsonArray as $oneJson) - { - array_push($decodedJson, json_decode($oneJson)); - } - return $decodedJson; + return json_decode($this->getProp($url, $format, 'dump-single-json', $password)); } /** diff --git a/controllers/FrontController.php b/controllers/FrontController.php index 4fb6548..83b2db8 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -213,7 +213,7 @@ class FrontController private function getVideoResponse(Request $request, Response $response, array $params, $password = null) { try { - $vidarr = $this->download->getJSON($params['url'], $this->defaultFormat, $password); + $video = $this->download->getJSON($params['url'], $this->defaultFormat, $password); } catch (PasswordException $e) { return $this->password($request, $response); } @@ -222,14 +222,19 @@ class FrontController } else { $protocol = '[protocol^=http]'; } + if (isset($video->entries)) { + $template = 'playlist.tpl'; + } else { + $template = 'video.tpl'; + } $this->view->render( $response, - 'video.tpl', + $template, [ - 'vidarr' => $vidarr, + 'video' => $video, 'class' => 'video', - 'title' => $vidarr[0]->title, - 'description' => 'Download "'.$vidarr[0]->title.'" from '.$vidarr[0]->extractor_key, + 'title' => $video->title, + 'description' => 'Download "'.$video->title.'" from '.$video->extractor_key, 'protocol' => $protocol, 'config' => $this->config, 'canonical' => $this->getCanonicalUrl($request), @@ -327,7 +332,11 @@ class FrontController $response = $response->withBody($stream->getBody()); } } - $response = $response->withHeader('Content-Disposition', 'attachment; filename="'.$video->_filename.'"'); + $response = $response->withHeader( + 'Content-Disposition', + 'attachment; filename="'. + $this->download->getFilename($url, $format, $password).'"' + ); return $response; } diff --git a/css/style.css b/css/style.css index 31ae741..4ec7276 100644 --- a/css/style.css +++ b/css/style.css @@ -397,10 +397,34 @@ padding:3px; +/* Playlists */ +.playlist-entry .thumb { + float: left; + margin-right: 1em; +} +.playlist-entry { + clear: both; + padding-top: 2em; + text-align: left; +} +.playlist-entry h3 { + margin-top: 0; +} +.playlist-entry h3 a { + text-decoration: none; +} +.playlist-entry h3 a:hover { + text-decoration: underline; +} + +.playlist-entry .downloadBtn { + font-size: 16px; + border-width: 2px; +} diff --git a/index.php b/index.php index ce07549..b3b77de 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,5 @@ 0) { - var i; - for (i = 0; i < launchBtn.length; i++) { - disabledBtn[i].classList.add('cast_hidden'); - launchBtn[i].classList.remove('cast_hidden'); - launchBtn[i].addEventListener('click', launchCast, false); - stopBtn[i].addEventListener('click', stopCast, false); - } + launchBtn = document.getElementById('cast_btn_launch'); + disabledBtn = document.getElementById('cast_disabled'); + stopBtn = document.getElementById('cast_btn_stop'); + if (launchBtn) { + disabledBtn.classList.add('cast_hidden'); + launchBtn.classList.remove('cast_hidden'); + launchBtn.addEventListener('click', launchCast, false); + stopBtn.addEventListener('click', stopCast, false); } } @@ -98,11 +94,11 @@ var castModule = (function () { return { init: function () { - var intro = document.getElementsByClassName('download_intro'), i; - for (i = 0; i < intro.length; i++) { - intro[i].insertAdjacentHTML('beforeend', ' Google Cast™ Casting to ChromeCast…'); + var intro = document.getElementById('download_intro'); + if (intro) { + intro.insertAdjacentHTML('beforeend', ' Google Cast™ Casting to ChromeCast…'); + window.__onGCastApiAvailable = loadCastApi; } - window.__onGCastApiAvailable = loadCastApi; } }; }()); diff --git a/templates/playlist.tpl b/templates/playlist.tpl new file mode 100644 index 0000000..c62ec59 --- /dev/null +++ b/templates/playlist.tpl @@ -0,0 +1,19 @@ +{include file="inc/head.tpl"} +
+
+{include file="inc/logo.tpl"} +

Videos extracted from the + +{$video->title} playlist: +

+{foreach $video->entries as $video} +
+ +

{$video->title}

+ webpage_url}">Download +
+{/foreach} + +
+
+{include file="inc/footer.tpl"} diff --git a/templates/video.tpl b/templates/video.tpl index 369370f..64ca555 100644 --- a/templates/video.tpl +++ b/templates/video.tpl @@ -3,95 +3,92 @@
{include file="inc/logo.tpl"} -{foreach $vidarr as $video} -

You are going to download - . -

- {if isset($video->thumbnail)} - - {/if} - {if isset($video->description)} - - {/if} - {if isset($video->upload_date)} - - {/if} -
- {if isset($video->formats)} -

-
- - {if uglyUrls} - - {/if} - + {if $uglyUrls} + + {/if} +

-
-
- {else} - - Download
- {/if} -
-{/foreach} + {else} + {for $foo=1 to 10} +   + {/for} + {/if} + {if isset($format->filesize)} + {($format->filesize/1000000)|round:2} MB + {for $foo=1 to (7 - (($format->filesize/1000000)|round:2|strlen))} +   + {/for} + {else} + {for $foo=1 to 10} +   + {/for} + {/if} + {if isset($format->format_note)} + {$format->format_note} + {/if} +  ({$format->format_id}) + + {/strip} + {/if} + {/foreach} + +

+
+ +{else} + + Download
+{/if}
{include file="inc/footer.tpl"} diff --git a/tests/FrontControllerTest.php b/tests/FrontControllerTest.php index d2dce25..7a6d425 100644 --- a/tests/FrontControllerTest.php +++ b/tests/FrontControllerTest.php @@ -264,6 +264,22 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase $this->assertTrue($result->isOk()); } + /** + * Test the video() function with a playlist. + * + * @return void + */ + public function testVideoWithPlaylist() + { + $result = $this->controller->video( + $this->request->withQueryParams( + ['url'=>'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC'] + ), + $this->response + ); + $this->assertTrue($result->isOk()); + } + /** * Test the error() function. * diff --git a/tests/VideoDownloadTest.php b/tests/VideoDownloadTest.php index e169c8a..d9bda2c 100644 --- a/tests/VideoDownloadTest.php +++ b/tests/VideoDownloadTest.php @@ -266,7 +266,6 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase $this->assertObjectHasAttribute('title', $info); $this->assertObjectHasAttribute('extractor_key', $info); $this->assertObjectHasAttribute('formats', $info); - $this->assertObjectHasAttribute('_filename', $info); } /**