getStats()->get()->first(); if ( is_null($stats->score) ) { return; } $airing = Airing::where('mal_id', $stats->mal_id)->get()->first(); if ( is_null($airing) ) { $airing = new Airing(); } /** * Try to get Data from Anilist. * On error return early. */ try { $airing_data = $anime->getDataFromAnilist(); } catch( \Exception $e ) { echo "Getting Data from Anilist failed for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n"; return; } if ( "" == $airing_data ) { echo "Got empty data from Anilist for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n"; return; } /** * Check if we need to save the airing data or if we already have it in database. */ if ( ! is_null( $airing->episode ) ) { if ( ! is_null( $airing_data->nextAiringEpisode ) && ( $airing->episode == $airing_data->nextAiringEpisode->episode) ) { if ( $airing->aired_at == $airing_data->nextAiringEpisode->airingAt ) { /** * Double entry. */ return; } /** * We need to update because the airing data was postponed. */ } } $airing->mal_id = $anime->mal_id; if ( ! is_null($airing_data->nextAiringEpisode) ) { $airing->episode = $airing_data->nextAiringEpisode->episode; } if ( ! is_null($airing_data->nextAiringEpisode) ) { $airing->aired_at = Carbon::createFromTimestamp($airing_data->nextAiringEpisode->airingAt); } if ( ! is_null($airing_data) ) { $airing->duration = $airing_data->duration; } if ( is_null($airing_data->duration) ) { echo "No duration found for anime: " . $anime->title_pref . " (" . $anime->mal_id . "). (Autoset to 20.)\n"; $airing->duration = 20; } if ( is_null($airing->aired_at) ) { echo "No airing date found for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n"; return; } $airing->save(); } 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]); } } } }