From 38ad5415db862e24da03ee194ef723540f9848a6 Mon Sep 17 00:00:00 2001 From: horus Date: Thu, 5 Mar 2020 22:24:28 +0100 Subject: Basic usage implemented. --- app/Libraries/Background.php | 80 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 4 deletions(-) (limited to 'app/Libraries/Background.php') diff --git a/app/Libraries/Background.php b/app/Libraries/Background.php index 6c84e97..79d3ab1 100644 --- a/app/Libraries/Background.php +++ b/app/Libraries/Background.php @@ -1,7 +1,9 @@ getStats()->get()->first(); if ( is_null($stats->score) ) { return; @@ -33,7 +35,7 @@ class Background { */ try { $airing_data = $anime->getDataFromAnilist(); - } catch( Exception $e ) { + } catch( \Exception $e ) { echo "Getting Data from Anilist failed for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n"; return; } @@ -84,11 +86,81 @@ class Background { $airing->save(); } - public function setAiringForAll() { - $anime_all = Anime::get(); + public function saveAiringForAll() { + $anime_all = Anime::where('is_airing', true)->get(); foreach( $anime_all as $anime ) { $this->setAiring($anime); sleep(1); } } + + /** + * Save entire anime season in database. + */ + public function saveSeason() { + $jikan = new MalClient; + + $season = $jikan->getSeasonal( + (new \Jikan\Request\Seasonal\SeasonalRequest( + )) + ); + + foreach($season->anime as $entry) { + /** + * Sleep to avoid 403 by MAL. + */ + sleep(10); + + $check = Anime::where('mal_id', $entry->getMalID() )->first()->get(); + if ( ! is_null($check) ) { + /** + * We already have this anime saved. + */ + echo "Duplicate entry: (" . $entry->getMalID() . ") Continue\n"; + continue; + } + + $anime = new Anime(); + $anime->fill( $entry->getMalID(), $is_airing=true ); + + if( ! DB::table('anime')->where('mal_id', $entry->getMalID() )->exists() ) { + $anime->save(); + } + } + } + + public function saveAnimeStats(Anime $anime = null) { + if ( is_null($anime) ) { + $anime = Anime::get(); + } + + foreach($anime as $entry ) { + $animeStats = new AnimeStats(); + $animeStats->fill( $entry->getMalID() ); + + $animeStats->save(); + } + } + + public function checkIfIsAiring() { + $jikan = new Malclient; + + foreach( Anime::where('is_airing', true)->get() as $anime) { + try { + $animeInfo = $jikan->getAnime( + (new \Jikan\Request\Anime\AnimeRequest( $anime->mal_id )) + ); + } catch (\Exception $e) { + echo "Problem requesting AnimeInfo for (" . $anime->mal_id . ") continue\n"; + echo $e->getMessage(); + echo "\n\n"; + } + + if ( ! $animeInfo->isAiring() ) { + DB::table('anime') + ->where('mal_id', $anime->mal_id) + ->update(['is_airing' => false]); + } + } + } } -- cgit v1.2.3