From 9cd0234665ea66dff172d94b9c1b4cb61b1d25b1 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sun, 21 Jun 2020 17:58:23 +0100 Subject: Improve robustness of all scripts and add more to sync command --- app/Youtube/Service.php | 83 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 19 deletions(-) (limited to 'app/Youtube') diff --git a/app/Youtube/Service.php b/app/Youtube/Service.php index 862a27a..e55fae5 100644 --- a/app/Youtube/Service.php +++ b/app/Youtube/Service.php @@ -2,6 +2,8 @@ namespace App\Youtube; +use App\Rugby\Model\Video; +use Goutte\Client; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Symfony\Component\Console\Helper\ProgressBar; @@ -13,6 +15,7 @@ use YoutubeDl\YoutubeDl; class Service { protected $url; + protected $path; protected $progressBar; protected $running = false; protected $video; @@ -20,6 +23,7 @@ class Service public function __construct(string $url, $output = null) { $this->url = $url; + if ($output) { $this->progressBar = new ProgressBar($output, 100); $this->output = $output; @@ -37,14 +41,68 @@ class Service public function downloadVideo(string $path) { - $this->path = $path; - $this->download($this->getVideoOptions()); + if (!$this->fileExists($this->url, 'video')) { + $this->path = $path; + $this->download($this->getVideoOptions()); + return true; + } + + return false; + } + + // public function downloadAudio(string $path) + // { + // if (!$this->fileExists($this->url, 'audio')) { + // $this->path = $path; + // + // $this->download($this->getAudioOptions()); + // return true; + // } + // + // return false; + // } + + public function modelExists(string $url): bool + { + $existing_match = Video::where('url', '=', $url)->get(); + + if ($existing_match->count()) { + return true; + } + + return false; + } + + public function createModel($url) + { + if ($this->downloadVideo('video')) { + $video_model = Video::create( + [ + 'path' => $this->getFullPath(), + 'url' => $url + ] + ); + }; + } + + public function getTitle(): string + { + return $this->video->getTitle(); } - public function downloadAudio(string $path) + protected function fileExists(string $url, string $directory): bool { - $this->path = $path; - $this->download($thjis->getAudioOptions()); + $url_parts = explode('=', $url); + + $all_files = Storage::disk('local')->files('youtube/'. $directory); + + $matching_files = preg_grep("/$url_parts[1]/i", $all_files); + + foreach ($matching_files as $path) { + return true; + } + + return false; } protected function download(array $options) @@ -74,15 +132,12 @@ class Service $this->progressBar->finish(); $this->running(false); - // $this->getWorkingPath() .'/' . $video->getFilename(), Storage::disk('local')->move( 'tmp/youtube/video/' . $video->getFilename(), 'youtube/video/' . $video->getFilename() ); $this->video = $video; - - // $video->getFile(); // \SplFileInfo instance of downloaded file } catch (NotFoundException $e) { dd($e); // Video not found @@ -98,16 +153,6 @@ class Service } } - public function getTitle(): string - { - return $this->video->getTitle(); - } - - public function getFullPath(): string - { - return $this->getStoragePath() . '/' . $this->video->getFilename(); - } - protected function formatBytes(string $bytes) { $units = [ @@ -143,7 +188,7 @@ class Service return [ 'prefer-free-formats' => true, 'no-overwrites' => true, - 'skip-download' => true + // 'skip-download' => true ]; } -- cgit v1.2.3