Skip to content

Commit

Permalink
MAO-39 closed #39 fixed a bug where the MAL crawler raised an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaishiyoku committed Aug 16, 2018
1 parent ed30fc1 commit a6a8945
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 37 deletions.
54 changes: 31 additions & 23 deletions app/Console/Commands/FetchMalItem.php
Expand Up @@ -4,7 +4,9 @@

use App\Models\MalItem;
use Illuminate\Console\Command;
use Jikan\Jikan;
use Jikan\Exception\ParserException;
use Jikan\MyAnimeList\MalClient;
use Jikan\Request\Manga\MangaRequest;

class FetchMalItem extends Command
{
Expand Down Expand Up @@ -39,31 +41,37 @@ public function __construct()
*/
public function handle()
{
$jikan = new Jikan();
$malItem = MalItem::firstOrNew(['mal_id' => (int) $this->argument('mal_id')]);
try {
$jikan = new MalClient();

$this->line(' #' . $malItem->mal_id);
$malItem = MalItem::firstOrNew(['mal_id' => (int) $this->argument('mal_id')]);

$data = $jikan->Manga($malItem->mal_id)->response;
$this->line(' #' . $malItem->mal_id);

$malItem->link_canonical = $data['link_canonical'];
$malItem->title = $data['title'];
$malItem->title_english = $data['title_english'];
$malItem->title_japanese = $data['title_japanese'];
$malItem->title_synonyms = $data['title_synonyms'];
$malItem->status = $data['status'];
$malItem->image_url = $data['image_url'];
$malItem->volumes = $data['volumes'];
$malItem->chapters = $data['chapters'];
$malItem->publishing = $data['publishing'];
$malItem->rank = $data['rank'];
$malItem->score = $data['score'];
$malItem->scored_by = $data['scored_by'];
$malItem->popularity = $data['popularity'];
$malItem->members = $data['members'];
$malItem->favorites = $data['favorites'];
$malItem->synopsis = $data['synopsis'];
$mangaItem = $jikan->getManga(new MangaRequest($malItem->mal_id));

$malItem->save();
$malItem->url = $mangaItem->getUrl();
$malItem->title = $mangaItem->getTitle();
$malItem->title_english = $mangaItem->getTitleEnglish();
$malItem->title_japanese = $mangaItem->getTitleJapanese();
$malItem->title_synonyms = implode(';', $mangaItem->getTitleSynonyms());
$malItem->status = $mangaItem->getStatus();
$malItem->image_url = $mangaItem->getImageUrl();
$malItem->volumes = $mangaItem->getVolumes();
$malItem->chapters = $mangaItem->getChapters();
$malItem->publishing = $mangaItem->isPublishing();
$malItem->rank = $mangaItem->getRank();
$malItem->score = $mangaItem->getScore();
$malItem->scored_by = $mangaItem->getScoredBy();
$malItem->popularity = $mangaItem->getPopularity();
$malItem->members = $mangaItem->getMembers();
$malItem->favorites = $mangaItem->getFavorites();
$malItem->synopsis = $mangaItem->getSynopsis();

$malItem->save();
} catch (ParserException $e) {
$this->error('Could not fetch data.');
$this->error($e);
}
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/MangaController.php
Expand Up @@ -213,7 +213,7 @@ private function createMalItemIfNecessary($request)
$malId = $request->get('mal_id', null);
$malItem = MalItem::find($malId);

if (!$malItem || !$malItem->link_canonical) {
if (!$malItem || !$malItem->url) {
Artisan::call("mal:get_item", ['mal_id' => $malId]);
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/Models/MalItem.php
Expand Up @@ -49,6 +49,8 @@
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MalItem whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MalItem whereVolumes($value)
* @mixin \Eloquent
* @property string|null $url
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MalItem whereUrl($value)
*/
class MalItem extends Model
{
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -13,7 +13,7 @@
"albertcht/invisible-recaptcha": "1.8.3",
"doctrine/dbal": "2.7.2",
"fideloper/proxy": "4.0.0",
"jikan-me/jikan": "1.16.4",
"jikan-me/jikan": "2.0.0-rc.1",
"kaishiyoku/laravel-menu": "2.0.0",
"kaishiyoku/yaml-translation": "5.6.0",
"laracasts/flash": "3.0.2",
Expand Down
192 changes: 183 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class RenameLinkCanonicalToUrlAtMalItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('mal_items', function (Blueprint $table) {
$table->renameColumn('link_canonical', 'url');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('mal_items', function (Blueprint $table) {
$table->renameColumn('url', 'link_canonical');
});
}
}
6 changes: 3 additions & 3 deletions resources/views/manga/index.blade.php
Expand Up @@ -25,12 +25,12 @@
<h5 class="card-title">
{{ $manga->name }}

@if ($manga->malItem && $manga->malItem->link_canonical)
{!! Html::decode(Html::link($manga->malItem->link_canonical, '<i class="fas fa-external-link"></i>', ['title' => __('manga.index.manga_at_myanimelist', ['name' => $manga->name])])) !!}
@if ($manga->malItem && $manga->malItem->url)
{!! Html::decode(Html::link($manga->malItem->url, '<i class="fas fa-external-link"></i>', ['title' => __('manga.index.manga_at_myanimelist', ['name' => $manga->name])])) !!}
@endif
</h5>

@if ($manga->malItem && $manga->malItem->link_canonical)
@if ($manga->malItem && $manga->malItem->url)
<div>{{ __('manga.index.mal_score') }}: {{ formatNumber($manga->malItem->score, 2) }}</div>
@endif

Expand Down

0 comments on commit a6a8945

Please sign in to comment.