♻️ DRY sync//history

This commit is contained in:
Dan Jones 2023-03-22 20:39:09 -05:00
commit fda7c13926
4 changed files with 48 additions and 30 deletions

View file

@ -4,7 +4,7 @@ namespace App\Commands;
use App\Data\WatchFile; use App\Data\WatchFile;
use App\Data\WatchData; use App\Data\WatchData;
use Illuminate\Support\Facades\Http; use App\Services\Trakt;
class TraktWatch extends Command class TraktWatch extends Command
{ {
@ -14,21 +14,9 @@ class TraktWatch extends Command
public function handle(): int public function handle(): int
{ {
$file = WatchFile::from($this->argument('file')); $file = WatchFile::from($this->argument('file'));
$data = WatchData::from(['rawData' => [$file->toArray()]]);
$resp = $this->app->make(Trakt::class)->syncHistory($data, $this->output);
$watchData = WatchData::from(['rawData' => [$file->toArray()]]); return $resp->ok() ? static::SUCCESS : static::FAILURE;
$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;
} }
} }

View file

@ -3,7 +3,7 @@
namespace App\Commands; namespace App\Commands;
use App\Data\WatchData; use App\Data\WatchData;
use Illuminate\Support\Facades\Http; use App\Services\Trakt;
class TraktWatchImport extends Command class TraktWatchImport extends Command
{ {
@ -13,19 +13,8 @@ class TraktWatchImport extends Command
public function handle(): int public function handle(): int
{ {
$watched = WatchData::from($this->arguments()); $watched = WatchData::from($this->arguments());
$data = $watched->structuredData; $resp = $this->app->make(Trakt::class)->syncHistory($watched, $this->output);
$headers = [ return $resp->ok() ? static::SUCCESS : static::FAILURE;
'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;
} }
} }

View file

@ -6,6 +6,7 @@ use App\Services\ProcessInput;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Queue\Factory as QueueFactoryContract; use Illuminate\Contracts\Queue\Factory as QueueFactoryContract;
use App\Queue\DatabaseConnector; use App\Queue\DatabaseConnector;
use App\Services\Trakt;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
@ -28,5 +29,6 @@ class AppServiceProvider extends ServiceProvider
public function register() public function register()
{ {
$this->app->instance(ProcessInput::class, new ProcessInput()); $this->app->instance(ProcessInput::class, new ProcessInput());
$this->app->instance(Trakt::class, new Trakt());
} }
} }

39
app/Services/Trakt.php Normal file
View file

@ -0,0 +1,39 @@
<?php
namespace App\Services;
use App\Data\WatchData;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Symfony\Component\Console\Output\OutputInterface;
class Trakt
{
protected $headers = [];
public function __construct()
{
$this->headers = [
'trakt-api-version' => 2,
'trakt-api-key' => env('TRAKT_APP_ID'),
'Authorization' => 'Bearer ' . env('TRAKT_OAUTH_TOKEN'),
];
}
public function request(array $headers = []): PendingRequest
{
return Http::withHeaders($this->headers + $headers)->baseUrl('https://api.trakt.tv');
}
public function syncHistory(WatchData $data, OutputInterface $output = null): Response
{
$output?->writeln(sprintf('Submitting %s to trakt with headers %s', json_encode($data->structuredData), json_encode($this->headers)));
$resp = $this->request()->post('/sync/history', $data->structuredData);
$output?->writeln('Response (' . $resp->status() . '):' . PHP_EOL . json_encode($resp->json(), JSON_PRETTY_PRINT));
return $resp;
}
}