diff --git a/app/Commands/TraktWatch.php b/app/Commands/TraktWatch.php new file mode 100644 index 0000000..07333d6 --- /dev/null +++ b/app/Commands/TraktWatch.php @@ -0,0 +1,34 @@ +argument('file')); + + $watchData = WatchData::from(['rawData' => [$file->toArray()]]); + $data = $watchData->structuredData; + + $headers = [ + 'trakt-api-version' => 2, + 'trakt-api-key' => env('TRAKT_APP_ID'), + 'Authorization' => 'Bearer ' . env('TRAKT_OAUTH_TOKEN'), + ]; + + $this->info(sprintf('Submitting %s to trakt with headers %s', json_encode($data), json_encode($headers))); + $response = Http::withHeaders($headers)->post('https://api.trakt.tv/sync/history', $data); + + $this->line('Response: ' . PHP_EOL . json_encode($response->json(), JSON_PRETTY_PRINT)); + + return static::SUCCESS; + } +} diff --git a/app/Data/DataPipes/ReadExportFiles.php b/app/Data/DataPipes/ReadExportFiles.php index 261770e..a76046b 100644 --- a/app/Data/DataPipes/ReadExportFiles.php +++ b/app/Data/DataPipes/ReadExportFiles.php @@ -3,7 +3,9 @@ namespace App\Data\DataPipes; use App\Data\FileData; +use App\Data\WatchExport; use Illuminate\Support\Collection; +use Spatie\LaravelData\DataCollection; use Spatie\LaravelData\DataPipes\DataPipe; use Spatie\LaravelData\Lazy; use Spatie\LaravelData\Optional; @@ -15,7 +17,11 @@ class ReadExportFiles implements DataPipe { public function handle(mixed $payload, DataClass $class, Collection $properties): Collection { - if (!$properties->has('files') || !$properties->get('files')->count()) { + if (!$properties->has('files')) { + $properties['files'] = new DataCollection(WatchExport::class, []); + } + + if (!$properties->get('files')->count()) { return $properties; }