diff options
Diffstat (limited to 'app/Libraries/Background.php')
| -rw-r--r-- | app/Libraries/Background.php | 80 |
1 files changed, 76 insertions, 4 deletions
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 @@ <?php namespace App\Libraries; +use Illuminate\Support\Facades\DB; use Carbon\Carbon; +use Jikan\MyAnimeList\MalClient; use App\Libraries\AnimeSchedule; use App\Libraries\AnimeSeason; @@ -16,7 +18,7 @@ use Eluceo\iCal\Component\Event; class Background { - public function setAiring(Anime $anime) { + public function saveAiring(Anime $anime) { $stats = $anime->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]); + } + } + } } |
