diff options
| -rw-r--r-- | app/AnimeDetails.php | 21 | ||||
| -rw-r--r-- | app/Libraries/Background.php | 29 |
2 files changed, 50 insertions, 0 deletions
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 @@ +<?php + +namespace App; + +use Illuminate\Database\Eloquent\Model; + +class AnimeDetails extends Model +{ + protected $table = 'anime_details'; + protected $fillable = [ + 'mal_id', + 'score_begin', + 'score_today', + 'watching', + 'members' + ]; + + public function anime() { + return $this->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); } |
