-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added anilist logic for external entry action (#721)
- Loading branch information
Showing
18 changed files
with
699 additions
and
178 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
app/Actions/Models/List/ExternalProfile/ExternalEntry/AnilistExternalEntryAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Actions\Models\List\ExternalProfile\ExternalEntry; | ||
|
||
use App\Actions\Models\List\ExternalProfile\ExternalEntryAction; | ||
use App\Enums\Models\List\ExternalEntryWatchStatus; | ||
use App\Models\List\External\ExternalEntry; | ||
use App\Models\Wiki\ExternalResource; | ||
use Illuminate\Http\Client\RequestException; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
/** | ||
* Class AnilistExternalEntryAction. | ||
*/ | ||
class AnilistExternalEntryAction extends ExternalEntryAction | ||
{ | ||
/** | ||
* Get the entries of the response. | ||
* | ||
* @return array | ||
*/ | ||
public function getEntries(): array | ||
{ | ||
$entries = []; | ||
$response = $this->makeRequest(); | ||
|
||
if ($response !== null) { | ||
$lists = Arr::get($response, 'data.MediaListCollection.lists'); | ||
$lists = Arr::where($lists, fn ($value, $key) => $value['isCustomList'] === false); | ||
|
||
foreach ($lists as $list) { | ||
foreach (Arr::get($list, 'entries') as $entry) { | ||
$entries[] = [ | ||
ExternalResource::ATTRIBUTE_EXTERNAL_ID => intval(Arr::get($entry, 'media.id')), | ||
ExternalEntry::ATTRIBUTE_SCORE => Arr::get($entry, 'score'), | ||
ExternalEntry::ATTRIBUTE_WATCH_STATUS => ExternalEntryWatchStatus::getAnilistMapping(Arr::get($entry, 'status'))->value, | ||
ExternalEntry::ATTRIBUTE_IS_FAVORITE => false, // TODO | ||
]; | ||
} | ||
} | ||
} | ||
|
||
return $entries; | ||
} | ||
|
||
/** | ||
* Make the request to the external api. | ||
* | ||
* @return array|null | ||
*/ | ||
public function makeRequest(): ?array | ||
{ | ||
try { | ||
$query = ' | ||
query($userName: String) { | ||
MediaListCollection(userName: $userName, type: ANIME) { | ||
lists { | ||
name | ||
status | ||
isCustomList | ||
entries { | ||
status | ||
score | ||
media { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
'; | ||
|
||
$variables = [ | ||
'userName' => $this->getUsername(), | ||
]; | ||
|
||
$response = Http::post('https://graphql.anilist.co', [ | ||
'query' => $query, | ||
'variables' => $variables, | ||
]) | ||
->throw() | ||
->json(); | ||
|
||
return $response; | ||
|
||
} catch (RequestException $e) { | ||
Log::error($e->getMessage()); | ||
|
||
throw $e; | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
app/Actions/Models/List/ExternalProfile/ExternalEntryAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Actions\Models\List\ExternalProfile; | ||
|
||
use App\Enums\Models\List\ExternalProfileSite; | ||
use App\Enums\Models\Wiki\ResourceSite; | ||
use Illuminate\Support\Arr; | ||
|
||
/** | ||
* Class ExternalEntryAction | ||
*/ | ||
abstract class ExternalEntryAction | ||
{ | ||
/** | ||
* Create a new action instance. | ||
* | ||
* @param array $profileParameters | ||
*/ | ||
public function __construct(protected array $profileParameters) | ||
{ | ||
} | ||
|
||
/** | ||
* Get the profile site. | ||
* | ||
* @return ExternalProfileSite | ||
*/ | ||
public function getProfileSite(): ExternalProfileSite | ||
{ | ||
return ExternalProfileSite::fromLocalizedName(Arr::get($this->profileParameters, 'site')); | ||
} | ||
|
||
/** | ||
* Get the resource site of the profile site. | ||
* | ||
* @return ResourceSite | ||
*/ | ||
protected function getResourceSite(): ResourceSite | ||
{ | ||
return $this->getProfileSite()->getResourceSite(); | ||
} | ||
|
||
/** | ||
* Get the username of the profile. | ||
* | ||
* @return string | ||
*/ | ||
public function getUsername(): string | ||
{ | ||
return Arr::get($this->profileParameters, 'name'); | ||
} | ||
|
||
/** | ||
* Get the entries of the response. | ||
* | ||
* @return array | ||
*/ | ||
abstract public function getEntries(): array; | ||
|
||
/** | ||
* Make the request to the external api. | ||
* | ||
* @return array|null | ||
*/ | ||
abstract public function makeRequest(): ?array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.