✨ Export watched show based on nfo files
Still a work in progress. But enough to get me going now.
This commit is contained in:
parent
a4aab55616
commit
b7534c01f5
8 changed files with 183 additions and 1 deletions
2
.env.example
Normal file
2
.env.example
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
TRAKT_APP_ID=
|
||||||
|
TRAKT_APP_SECRET=
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,3 +4,4 @@
|
||||||
/.vagrant
|
/.vagrant
|
||||||
.phpunit.result.cache
|
.phpunit.result.cache
|
||||||
/database/database.sqlite
|
/database/database.sqlite
|
||||||
|
.env
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
class GetShow extends Command
|
class GetShow extends Command
|
||||||
{
|
{
|
||||||
protected $signature = 'get:show ' .
|
protected $signature = 'show:get ' .
|
||||||
'{--i|input=* : Input files} ' .
|
'{--i|input=* : Input files} ' .
|
||||||
'{--u|url= : URL to download} ' .
|
'{--u|url= : URL to download} ' .
|
||||||
'{--d|destination= : Directory of show} ' .
|
'{--d|destination= : Directory of show} ' .
|
||||||
|
|
|
||||||
25
app/Commands/TraktWatchExport.php
Normal file
25
app/Commands/TraktWatchExport.php
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Commands;
|
||||||
|
|
||||||
|
use App\Data\WatchFile;
|
||||||
|
|
||||||
|
class TraktWatchExport extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'show:watch:export {file : File to watch}';
|
||||||
|
protected $description = 'Create a json file to mark watched and import with show:watch:import when online';
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
$file = WatchFile::from($this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
44
app/Data/DataPipes/GetSeasonEp.php
Normal file
44
app/Data/DataPipes/GetSeasonEp.php
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data\DataPipes;
|
||||||
|
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use SimpleXMLElement;
|
||||||
|
use Spatie\LaravelData\DataPipes\DataPipe;
|
||||||
|
use Spatie\LaravelData\Support\DataClass;
|
||||||
|
|
||||||
|
use function file_exists;
|
||||||
|
use function pathinfo;
|
||||||
|
use function simplexml_load_file;
|
||||||
|
|
||||||
|
|
||||||
|
class GetSeasonEp implements DataPipe
|
||||||
|
{
|
||||||
|
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection
|
||||||
|
{
|
||||||
|
$path = $properties->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
44
app/Data/DataPipes/GetShowTmdb.php
Normal file
44
app/Data/DataPipes/GetShowTmdb.php
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data\DataPipes;
|
||||||
|
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use SimpleXMLElement;
|
||||||
|
use Spatie\LaravelData\DataPipes\DataPipe;
|
||||||
|
use Spatie\LaravelData\Support\DataClass;
|
||||||
|
|
||||||
|
use function dirname;
|
||||||
|
use function file_exists;
|
||||||
|
use function simplexml_load_file;
|
||||||
|
|
||||||
|
class GetShowTmdb implements DataPipe
|
||||||
|
{
|
||||||
|
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection
|
||||||
|
{
|
||||||
|
$path = $properties->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
30
app/Data/DataPipes/ParseWatchFile.php
Normal file
30
app/Data/DataPipes/ParseWatchFile.php
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data\DataPipes;
|
||||||
|
|
||||||
|
use App\Data\FileData;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Spatie\LaravelData\DataPipes\DataPipe;
|
||||||
|
use Spatie\LaravelData\Lazy;
|
||||||
|
use Spatie\LaravelData\Optional;
|
||||||
|
use Spatie\LaravelData\Support\DataClass;
|
||||||
|
use Spatie\LaravelData\Support\DataConfig;
|
||||||
|
use Spatie\LaravelData\Support\DataProperty;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
class ParseWatchFile implements DataPipe
|
||||||
|
{
|
||||||
|
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection
|
||||||
|
{
|
||||||
|
$properties['watched'] = Carbon::now();
|
||||||
|
$properties['output'] = sprintf(
|
||||||
|
'%s-%dx%02d.json',
|
||||||
|
Str::slug($properties['showTitle']),
|
||||||
|
$properties['season'],
|
||||||
|
$properties['episode']
|
||||||
|
);
|
||||||
|
|
||||||
|
return $properties;
|
||||||
|
}
|
||||||
|
}
|
||||||
36
app/Data/WatchFile.php
Normal file
36
app/Data/WatchFile.php
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data;
|
||||||
|
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Spatie\LaravelData\Attributes\WithCast;
|
||||||
|
use Spatie\LaravelData\DataPipeline;
|
||||||
|
|
||||||
|
class WatchFile extends Data
|
||||||
|
{
|
||||||
|
#[WithCast(Casts\File::class)]
|
||||||
|
public string $path;
|
||||||
|
public ?string $output = null;
|
||||||
|
public ?string $showNfo = null;
|
||||||
|
public ?string $showTmdb = null;
|
||||||
|
public ?string $showImdb = null;
|
||||||
|
public ?string $showTitle = null;
|
||||||
|
public ?string $epNfo = null;
|
||||||
|
public int $season = 0;
|
||||||
|
public int $episode = 0;
|
||||||
|
public Carbon $watched;
|
||||||
|
|
||||||
|
public static function fromPath(string $path): static
|
||||||
|
{
|
||||||
|
return static::from(['path' => $path]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function pipeline(): DataPipeline
|
||||||
|
{
|
||||||
|
return parent::pipeline()
|
||||||
|
->through(DataPipes\GetShowTmdb::class)
|
||||||
|
->through(DataPipes\GetSeasonEp::class)
|
||||||
|
->through(DataPipes\ParseWatchFile::class)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue