Skip to content

Commit

Permalink
Merge pull request #156
Browse files Browse the repository at this point in the history
[Update] Import ID
  • Loading branch information
kiritokatklian authored Jun 14, 2021
2 parents 54658d1 + 1e048ef commit 8a0ffd2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
84 changes: 74 additions & 10 deletions app/Console/Commands/ImportAnimeID.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Models\Anime;
use Http;
use Illuminate\Console\Command;
use JsonMachine\JsonDecoder\ExtJsonDecoder;
use JsonMachine\JsonMachine;
Expand Down Expand Up @@ -48,20 +49,22 @@ public function handle(): int
$progressBar->start();

foreach ($animes as $data) {
$sources = $this->filterSources($data->sources);
if ($animes->getPosition() > 10111900) {
$sources = $this->filterSources($data->sources);

if (array_key_exists('mal_id', $sources)) {
$anime = Anime::firstWhere([
['mal_id', $sources['mal_id']],
]);
if (array_key_exists('mal_id', $sources) && array_key_exists('notify_id', $sources)) {
$anime = Anime::firstWhere([
['mal_id', $sources['mal_id']],
]);

if (!empty($anime)) {
$anime->update($sources);
if (!empty($anime)) {
$anime->update($sources);
}
}
}

$progress = $animes->getPosition();
$progressBar->setProgress($progress);
$progress = $animes->getPosition();
$progressBar->setProgress($progress);
}
}

$progressBar->finish();
Expand All @@ -79,6 +82,7 @@ protected function filterSources(array $sources): array
if (empty($sources)) {
return $sources;
}

$regexSearch = [
'anidb_id' => '#https:\/\/anidb.net\/anime\/(\w+)$#i',
'anilist_id' => '#https:\/\/anilist.co\/anime\/(\w+)$#i',
Expand All @@ -99,9 +103,69 @@ protected function filterSources(array $sources): array
$matchedSources[$key] = $match[1];
}
}

if (array_key_exists('notify_id', $matchedSources)) {
$matchedSources = array_merge($matchedSources, $this->getIDsFromNotify($matchedSources['notify_id']));
}
}
}

return $matchedSources;
}

/**
* Returns an array of the IDs from the notify API.
*
* @param string $notifyID
* @return array
*/
protected function getIDsFromNotify(string $notifyID): array
{
$response = Http::get('https://notify.moe/api/anime/' . $notifyID);
$cleanSources = [];

if ($response->failed()) {
return $cleanSources;
}

$sources = $response->json('mappings');

foreach ($sources as $source) {
switch ($source['service']) {
case 'imdb/anime':
$cleanSources['imdb_id'] = $source['serviceId'];
break;
case 'thetvdb/anime':
$cleanSources['tvdb_id'] = $this->getTVDBId($source['serviceId']);
break;
case 'shoboi/anime':
$cleanSources['syoboi_id'] = $source['serviceId'];
break;
case 'trakt/anime':
$cleanSources['trakt_id'] = $source['serviceId'];
break;
default: break;
}
}

return $cleanSources;
}


/**
* Cleans and returns the TVDB ID from the given string.
*
* @param string $id
* @return string
*/
protected function getTVDBId(string $id): string
{
$matchCount = preg_match('/^(\d+)\/.*/i', $id, $tvdbId);

if ($matchCount) {
return $tvdbId[1];
}

return $id;
}
}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
| or any other location as required by the application or its packages.
*/

'version' => '1.2.0-alpha.91',
'version' => '1.2.0-alpha.92',

/*
|--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function up()
$table->unsignedInteger('kitsu_id')->unique()->nullable();
$table->unsignedInteger('mal_id')->unique()->nullable();
$table->string('notify_id')->unique()->nullable();
$table->unsignedInteger('tvdb_id')->unique()->nullable();
$table->unsignedInteger('syoboi_id')->nullable();
$table->unsignedInteger('trakt_id')->nullable();
$table->unsignedInteger('tvdb_id')->nullable();
$table->string('slug');
$table->string('original_title');
$table->json('synonym_titles')->nullable();
Expand Down

0 comments on commit 8a0ffd2

Please sign in to comment.