Compare commits

...

2 commits

6 changed files with 197 additions and 14 deletions

View file

@ -6,13 +6,14 @@ namespace App\Commands;
use App\Services\Trakt;
use Illuminate\Support\Arr;
use Illuminate\Contracts\Config\Repository;
class TraktDebug extends Command
{
protected $signature = 'trakt:debug {endpoint : Trakt API endpoint to call} {method=GET : HTTP method to use} {--d|data=* : data}';
protected $description = 'Make arbitrary requests to Trakt API.' . PHP_EOL . '-d should be formatted with key=value or key:=value for non-strings';
public function handle(Trakt $trakt): int
public function handle(Trakt $trakt, Repository $config): int
{
$body = $this->getBody();
$method = $this->argument('method');
@ -28,6 +29,8 @@ class TraktDebug extends Command
$resp = $resp->send($method, $url);
$this->line('Response (' . $resp->status() . '):' . PHP_EOL . json_encode($resp->json(), JSON_PRETTY_PRINT));
$this->line('Config: ' . json_encode($config->get('trakt')));
return static::SUCCESS;
}

View file

@ -6,12 +6,14 @@ namespace App\Commands;
use App\Services\Trakt;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Filesystem\Filesystem;
class TraktLogin extends Command
{
protected $signature = 'trakt:login';
protected $description = 'Login and get new token.';
public function handle(Trakt $trakt, Repository $config): int
public function handle(Trakt $trakt, Repository $config, Filesystem $files): int
{
$data = [
'client_id' => $config->get('trakt.app_id'),
@ -31,6 +33,7 @@ class TraktLogin extends Command
}
$this->line("TRAKT_DEVICE_RESP='" . json_encode($body) . "'");
$config->set('trakt.login.device', $body);
$data = [
'client_id' => $config->get('trakt.app_id'),
@ -48,6 +51,8 @@ class TraktLogin extends Command
$body = $resp->json();
$this->line("TRAKT_OAUTH_RESP='" . json_encode($body) . "'");
$this->line('TRAKT_OAUTH_TOKEN=' . $body['access_token']);
$config->set('trakt.login.oauth', $body);
$files->put($config->get('trakt.path'), json_encode($config->get('trakt'), JSON_PRETTY_PRINT));
return static::SUCCESS;

View file

@ -6,12 +6,14 @@ namespace App\Commands;
use App\Services\Trakt;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Filesystem\Filesystem;
class TraktRefreshLogin extends Command
{
protected $signature = 'trakt:refresh';
protected $description = 'Refresh access token.';
public function handle(Trakt $trakt, Repository $config): int
public function handle(Trakt $trakt, Repository $config, Filesystem $files): int
{
$refresh_token = $config->get('trakt.login.oauth.refresh_token');
if (empty($refresh_token)) {
@ -40,6 +42,9 @@ class TraktRefreshLogin extends Command
$this->line("TRAKT_OAUTH_RESP='" . json_encode($body) . "'");
$this->line('TRAKT_OAUTH_TOKEN=' . $body['access_token']);
$config->set('trakt.login.oauth', $body);
$files->put($config->get('trakt.path'), json_encode($config->get('trakt'), JSON_PRETTY_PRINT));
return static::SUCCESS;
}
}

View file

@ -37,7 +37,8 @@
"laravel-zero/framework": "^10.0",
"nunomaduro/termwind": "^1.3",
"php-ffmpeg/php-ffmpeg": "^1.0",
"spatie/laravel-data": "^2.0"
"spatie/laravel-data": "^2.0",
"xdg/base-directory": "^1.2"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",

160
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "9ebd8521e376754effa9bc94f7afbc9e",
"content-hash": "dbc82eba2362bad2cc7266710afb0827",
"packages": [
{
"name": "brick/math",
@ -5166,6 +5166,76 @@
],
"time": "2024-09-25T14:21:43+00:00"
},
{
"name": "symfony/filesystem",
"version": "v6.4.24",
"source": {
"type": "git",
"url": "https://github.com/symfony/filesystem.git",
"reference": "75ae2edb7cdcc0c53766c30b0a2512b8df574bd8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/filesystem/zipball/75ae2edb7cdcc0c53766c30b0a2512b8df574bd8",
"reference": "75ae2edb7cdcc0c53766c30b0a2512b8df574bd8",
"shasum": ""
},
"require": {
"php": ">=8.1",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-mbstring": "~1.8"
},
"require-dev": {
"symfony/process": "^5.4|^6.4|^7.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Symfony\\Component\\Filesystem\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Provides basic utilities for the filesystem",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/filesystem/tree/v6.4.24"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://github.com/nicolas-grekas",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2025-07-10T08:14:14+00:00"
},
{
"name": "symfony/finder",
"version": "v6.4.24",
@ -6907,6 +6977,94 @@
"source": "https://github.com/webmozarts/assert/tree/1.12.0"
},
"time": "2025-10-20T12:43:39+00:00"
},
{
"name": "xdg/base-directory",
"version": "1.2",
"source": {
"type": "git",
"url": "https://github.com/php-xdg/base-directory.git",
"reference": "7a91de4c387e9e0d31b8ab6aa564dc85f546b381"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-xdg/base-directory/zipball/7a91de4c387e9e0d31b8ab6aa564dc85f546b381",
"reference": "7a91de4c387e9e0d31b8ab6aa564dc85f546b381",
"shasum": ""
},
"require": {
"php": ">=8.1",
"symfony/filesystem": "^5.4||^6.0",
"xdg/environment": "^2.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Xdg\\BaseDirectory\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "ju1ius",
"email": "jules.bernable@gmail.com"
}
],
"description": "Implementation of the XDG Base Directory Specification",
"support": {
"issues": "https://github.com/php-xdg/base-directory/issues",
"source": "https://github.com/php-xdg/base-directory/tree/1.2"
},
"time": "2023-03-02T08:43:12+00:00"
},
{
"name": "xdg/environment",
"version": "2.1",
"source": {
"type": "git",
"url": "https://github.com/php-xdg/environment.git",
"reference": "d626d63e902150bbf326756e4b78eea461fd7c12"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-xdg/environment/zipball/d626d63e902150bbf326756e4b78eea461fd7c12",
"reference": "d626d63e902150bbf326756e4b78eea461fd7c12",
"shasum": ""
},
"require": {
"php": ">=8.1"
},
"type": "library",
"autoload": {
"psr-4": {
"Xdg\\Environment\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "ju1ius",
"email": "jules.bernable@gmail.com"
}
],
"description": "An abstraction layer over environment variables.",
"keywords": [
"environment",
"environment-variables",
"freedesktop",
"xdg"
],
"support": {
"issues": "https://github.com/php-xdg/environment/issues",
"source": "https://github.com/php-xdg/environment/tree/2.1"
},
"time": "2023-03-13T19:24:46+00:00"
}
],
"packages-dev": [

View file

@ -1,11 +1,22 @@
<?php
return [
'app_id' => env('TRAKT_APP_ID'),
'app_secret' => env('TRAKT_APP_secret'),
'redirect_uri' => env('TRAKT_REDIRECT_URI', 'urn:ietf:wg:oauth:2.0:oob'),
'login' => [
'device' => json_decode(env('TRAKT_DEVICE_RESP', '{}}'), true),
'oauth' => json_decode(env('TRAKT_OAUTH_RESP', '{}}'), true),
],
];
use Xdg\BaseDirectory\XdgBaseDirectory as Xdg;
use Illuminate\Support\Arr;
$data = [];
$xdg = Xdg::fromEnvironment();
$path = $xdg->findDataPath("trakt/data.json", file_exists(...));
if (file_exists($path)) {
$data = json_decode(file_get_contents($path), true);
}
$data['path'] = $path;
$data = Arr::add($data, 'app_id', env('TRAKT_APP_ID'));
$data = Arr::add($data, 'app_secret', env('TRAKT_APP_SECRET'));
$data = Arr::add($data, 'redirect_uri', env('TRAKT_REDIRECT_URI', 'urn:ietf:wg:oauth:2.0:oob'));
$data['login'] = $data['login'] ?? [];
$data['login'] = Arr::add($data['login'], 'device', json_decode(env('TRAKT_DEVICE_RESP', '{}'), true));
$data['login'] = Arr::add($data['login'], 'oauth', json_decode(env('TRAKT_OAUTH_RESP', '{}'), true));
return $data;