From b7534c01f554c43479eb9e7a42d6e56040ed08c9 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Tue, 13 Sep 2022 16:50:10 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Export=20watched=20show=20based=20o?= =?UTF-8?q?n=20nfo=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Still a work in progress. But enough to get me going now. --- .env.example | 2 ++ .gitignore | 1 + app/Commands/GetShow.php | 2 +- app/Commands/TraktWatchExport.php | 25 +++++++++++++++ app/Data/DataPipes/GetSeasonEp.php | 44 +++++++++++++++++++++++++++ app/Data/DataPipes/GetShowTmdb.php | 44 +++++++++++++++++++++++++++ app/Data/DataPipes/ParseWatchFile.php | 30 ++++++++++++++++++ app/Data/WatchFile.php | 36 ++++++++++++++++++++++ 8 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 .env.example create mode 100644 app/Commands/TraktWatchExport.php create mode 100644 app/Data/DataPipes/GetSeasonEp.php create mode 100644 app/Data/DataPipes/GetShowTmdb.php create mode 100644 app/Data/DataPipes/ParseWatchFile.php create mode 100644 app/Data/WatchFile.php diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..8040edc --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +TRAKT_APP_ID= +TRAKT_APP_SECRET= diff --git a/.gitignore b/.gitignore index 38e1ccf..dd8fc43 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /.vagrant .phpunit.result.cache /database/database.sqlite +.env diff --git a/app/Commands/GetShow.php b/app/Commands/GetShow.php index 864f2b4..e673e12 100644 --- a/app/Commands/GetShow.php +++ b/app/Commands/GetShow.php @@ -9,7 +9,7 @@ use Symfony\Component\Console\Output\OutputInterface; class GetShow extends Command { - protected $signature = 'get:show ' . + protected $signature = 'show:get ' . '{--i|input=* : Input files} ' . '{--u|url= : URL to download} ' . '{--d|destination= : Directory of show} ' . diff --git a/app/Commands/TraktWatchExport.php b/app/Commands/TraktWatchExport.php new file mode 100644 index 0000000..8714ba2 --- /dev/null +++ b/app/Commands/TraktWatchExport.php @@ -0,0 +1,25 @@ +argument('file')); + $ret = file_put_contents($file->output, $file->toJson()); + + if ($ret) { + $this->line("Saved to {$file->output}"); + } else { + $this->error('Failed to export'); + } + + return $ret ? static::SUCCESS : static::FAILURE; + } +} diff --git a/app/Data/DataPipes/GetSeasonEp.php b/app/Data/DataPipes/GetSeasonEp.php new file mode 100644 index 0000000..30bd508 --- /dev/null +++ b/app/Data/DataPipes/GetSeasonEp.php @@ -0,0 +1,44 @@ +get('path'); + $pi = pathinfo($path); + + $nfo = "{$pi['dirname']}/{$pi['filename']}.nfo"; + if (file_exists($nfo)) { + $properties['epNfo'] = $nfo; + + return $this->getEpFromNfo($properties, $nfo); + } + + return $properties; + } + + protected function getEpFromNfo(Collection $properties, string $nfo): Collection + { + $xml = simplexml_load_file($nfo); + if ($xml->season) { + $properties['season'] = (int) $xml->season; + } + if ($xml->episode) { + $properties['episode'] = (int) $xml->episode; + } + + return $properties; + } +} diff --git a/app/Data/DataPipes/GetShowTmdb.php b/app/Data/DataPipes/GetShowTmdb.php new file mode 100644 index 0000000..192d729 --- /dev/null +++ b/app/Data/DataPipes/GetShowTmdb.php @@ -0,0 +1,44 @@ +get('path'); + + $dir = dirname($path); + do { + $nfo = "$dir/tvshow.nfo"; + $dir = dirname($dir); + } while (!file_exists($nfo) && $dir !== '/'); + + if (!file_exists($nfo)) { + return $properties; + } + + $properties['showNfo'] = $nfo; + + $xml = simplexml_load_file($nfo); + $properties['showTmdb'] = $this->getProp($xml, 'tmdbid'); + $properties['showImdb'] = $this->getProp($xml, 'imdb_id'); + $properties['showTitle'] = $this->getProp($xml, 'title'); + + return $properties; + } + + protected function getProp(SimpleXMLElement $xml, string $prop): ?string + { + return $xml->$prop ? (string) $xml->$prop : null; + } +} diff --git a/app/Data/DataPipes/ParseWatchFile.php b/app/Data/DataPipes/ParseWatchFile.php new file mode 100644 index 0000000..775ae40 --- /dev/null +++ b/app/Data/DataPipes/ParseWatchFile.php @@ -0,0 +1,30 @@ + $path]); + } + + public static function pipeline(): DataPipeline + { + return parent::pipeline() + ->through(DataPipes\GetShowTmdb::class) + ->through(DataPipes\GetSeasonEp::class) + ->through(DataPipes\ParseWatchFile::class) + ; + } +}