🚧 Submit export shows to trakt
Still very messy
This commit is contained in:
parent
f33031ed8b
commit
50c49cd0ef
8 changed files with 1615 additions and 68 deletions
31
app/Commands/TraktWatchImport.php
Normal file
31
app/Commands/TraktWatchImport.php
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Commands;
|
||||||
|
|
||||||
|
use App\Data\WatchData;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
|
||||||
|
class TraktWatchImport extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'show:watch:import {files* : JSON files to import}';
|
||||||
|
protected $description = 'Once online, sync watches from show:watch:export';
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
$watched = WatchData::from($this->arguments());
|
||||||
|
$data = $watched->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
26
app/Data/DataPipes/ReadExportFile.php
Normal file
26
app/Data/DataPipes/ReadExportFile.php
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data\DataPipes;
|
||||||
|
|
||||||
|
use App\Data\FileData;
|
||||||
|
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;
|
||||||
|
|
||||||
|
class ReadExportFile implements DataPipe
|
||||||
|
{
|
||||||
|
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection
|
||||||
|
{
|
||||||
|
if (!$properties->has('path')) {
|
||||||
|
return $properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
$properties['rawData'] = json_decode(file_get_contents($properties->get('path')), true) ?? [];
|
||||||
|
|
||||||
|
return $properties;
|
||||||
|
}
|
||||||
|
}
|
||||||
31
app/Data/DataPipes/ReadExportFiles.php
Normal file
31
app/Data/DataPipes/ReadExportFiles.php
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data\DataPipes;
|
||||||
|
|
||||||
|
use App\Data\FileData;
|
||||||
|
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;
|
||||||
|
|
||||||
|
class ReadExportFiles implements DataPipe
|
||||||
|
{
|
||||||
|
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection
|
||||||
|
{
|
||||||
|
if (!$properties->has('files') || !$properties->get('files')->count()) {
|
||||||
|
return $properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
foreach ($properties->get('files') as $file)
|
||||||
|
{
|
||||||
|
$data[] = $file->rawData;
|
||||||
|
}
|
||||||
|
$properties['rawData'] = $data;
|
||||||
|
|
||||||
|
return $properties;
|
||||||
|
}
|
||||||
|
}
|
||||||
55
app/Data/DataPipes/StructureData.php
Normal file
55
app/Data/DataPipes/StructureData.php
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data\DataPipes;
|
||||||
|
|
||||||
|
use App\Data\FileData;
|
||||||
|
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;
|
||||||
|
|
||||||
|
class StructureData implements DataPipe
|
||||||
|
{
|
||||||
|
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection
|
||||||
|
{
|
||||||
|
if (!$properties->has('rawData') || !count($properties->get('rawData'))) {
|
||||||
|
return $properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
foreach ($properties->get('rawData') as $watch) {
|
||||||
|
$showTmdb = $watch['showTmdb'];
|
||||||
|
$season = $watch['season'];
|
||||||
|
$episode = $watch['episode'];
|
||||||
|
$watched = $watch['watched'];
|
||||||
|
if (!array_key_exists($showTmdb, $data)) {
|
||||||
|
$data[$showTmdb] = [];
|
||||||
|
$data[$showTmdb]['ids'] = ['tmdb' => $showTmdb];
|
||||||
|
$data[$showTmdb]['seasons'] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!array_key_exists($season, $data[$showTmdb]['seasons'])) {
|
||||||
|
$data[$showTmdb]['seasons'][$season] = [];
|
||||||
|
$data[$showTmdb]['seasons'][$season]['number'] = $season;
|
||||||
|
$data[$showTmdb]['seasons'][$season]['episodes'] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$data[$showTmdb]['seasons'][$season]['episodes'][] = [
|
||||||
|
'number' => $episode,
|
||||||
|
'watched_at' => $watched,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$flat = [];
|
||||||
|
foreach ($data as $show) {
|
||||||
|
$show['seasons'] = array_values($show['seasons']);
|
||||||
|
$flat[] = $show;
|
||||||
|
}
|
||||||
|
$properties['structuredData'] = ['shows' => $flat];
|
||||||
|
|
||||||
|
return $properties;
|
||||||
|
}
|
||||||
|
}
|
||||||
24
app/Data/WatchData.php
Normal file
24
app/Data/WatchData.php
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data;
|
||||||
|
|
||||||
|
use App\Exceptions\Quit;
|
||||||
|
use Spatie\LaravelData\Attributes\DataCollectionOf;
|
||||||
|
use Spatie\LaravelData\DataCollection;
|
||||||
|
use Spatie\LaravelData\DataPipeline;
|
||||||
|
|
||||||
|
class WatchData extends Data
|
||||||
|
{
|
||||||
|
#[DataCollectionOf(WatchExport::class)]
|
||||||
|
public DataCollection $files;
|
||||||
|
public array $rawData = [];
|
||||||
|
public array $structuredData = [];
|
||||||
|
|
||||||
|
public static function pipeline(): DataPipeline
|
||||||
|
{
|
||||||
|
return parent::pipeline()
|
||||||
|
->through(DataPipes\ReadExportFiles::class)
|
||||||
|
->through(DataPipes\StructureData::class)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
17
app/Data/WatchExport.php
Normal file
17
app/Data/WatchExport.php
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data;
|
||||||
|
|
||||||
|
use Spatie\LaravelData\DataPipeline;
|
||||||
|
|
||||||
|
class WatchExport extends FileData
|
||||||
|
{
|
||||||
|
public array $rawData = [];
|
||||||
|
|
||||||
|
public static function pipeline(): DataPipeline
|
||||||
|
{
|
||||||
|
return parent::pipeline()
|
||||||
|
->through(DataPipes\ReadExportFile::class)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -29,7 +29,9 @@
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.0",
|
"php": "^8.0",
|
||||||
"danjones000/ffmpeg-mappable-media": "~0.2",
|
"danjones000/ffmpeg-mappable-media": "~0.2",
|
||||||
|
"guzzlehttp/guzzle": "^7.4",
|
||||||
"illuminate/database": "^9.0",
|
"illuminate/database": "^9.0",
|
||||||
|
"illuminate/http": "^9.0",
|
||||||
"illuminate/log": "^9.0",
|
"illuminate/log": "^9.0",
|
||||||
"illuminate/queue": "^9.2",
|
"illuminate/queue": "^9.2",
|
||||||
"laravel-zero/framework": "^9.0",
|
"laravel-zero/framework": "^9.0",
|
||||||
|
|
|
||||||
1497
composer.lock
generated
1497
composer.lock
generated
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue