From 480515cf08d81040e416a943b046a09f9918937a Mon Sep 17 00:00:00 2001 From: horus Date: Fri, 11 Nov 2022 22:03:52 +0100 Subject: add model and logic for anime_details --- app/AnimeDetails.php | 21 +++++++++++++++++++++ app/Libraries/Background.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 app/AnimeDetails.php diff --git a/app/AnimeDetails.php b/app/AnimeDetails.php new file mode 100644 index 0000000..2d377bc --- /dev/null +++ b/app/AnimeDetails.php @@ -0,0 +1,21 @@ +belongsTo('App\Anime', 'mal_id', 'mal_id'); + } +} diff --git a/app/Libraries/Background.php b/app/Libraries/Background.php index 794ca02..91d9d75 100644 --- a/app/Libraries/Background.php +++ b/app/Libraries/Background.php @@ -10,6 +10,7 @@ use App\Libraries\AnimeSchedule; use App\Libraries\AnimeSeason; use App\Anime; use App\AnimeStats; +use App\AnimeDetails; use App\MALUser; use App\Calendar; use App\Airing; @@ -144,6 +145,7 @@ class Background { $failure = 0; foreach($anime as $entry ) { + # skip if we have crawled stats from today if ( ! empty($entry->getStats()->get()->last()) ) { $date = $entry->getStats()->get()->last()->created_at->toDateTimeString(); @@ -168,6 +170,33 @@ class Background { return; } + $animeDetail = AnimeDetails::where('mal_id', $entry->mal_id); + + if ( ! $animeDetail->exists() ) { + + $model_score_begin = $entry->getStats()->whereNotNull('score')->orderBy('created_at', 'ASC')->limit(1)->get()->first(); + if ( ! is_null($model_score_begin) ) { + $score_begin = $model_score_begin->score; + } else { + $score_begin = null; + } + + $animeDetail->insert([ + "mal_id" => $animeStats->mal_id, + "score_begin" => $score_begin, + "score_today" => $animeStats->score, + "watching" => $animeStats->watching, + "members" => $animeStats->members + ]); + } else { + $animeDetail->update([ + "mal_id" => $animeStats->mal_id, + "score_today" => $animeStats->score, + "watching" => $animeStats->watching, + "members" => $animeStats->members + ]); + } + # sleep to avoid 403 sleep(5); } -- cgit v1.2.3