mal_id . " | ";
if ( $anime->airing_status != env("ANIME_IS_AIRING") ) {
echo "saveAiring: Anime (" . $anime->mal_id . ") is not airing. Skipping.\n";
return;
}
$stats = $anime->getStats()->get()->last();
if ( is_null($stats) || is_null($stats->score) ) {
echo "saveAiring: getStats() is null. returning for anime:" . $anime->title_pref . " (" . $anime->mal_id . ")\n";
return;
}
$airing = Airing::where('mal_id', $stats->mal_id)->get()->last();
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 "saveAiring: Getting Data from Anilist failed for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
return;
}
if ( "" == $airing_data ) {
echo "saveAiring: 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 ) ){
if ( $airing->aired_at == Carbon::createFromTimestamp($airing_data->nextAiringEpisode->airingAt) ) {
/**
* Double entry.
*/
echo "saveAiring: Double entry: (" . $anime->mal_id . ") Episode: " . $airing->episode . "\n";
return;
} else {
/**
* We need to delete/reinsert because the airing date was postponed.
* Keep in mind that it's still the same episode which should be aired.
*/
echo "saveAiring: The airing was postponed: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
echo "saveAiring: Original: " . $airing->episode . "/" . $airing->aired_at . " New: " . Carbon::createFromTimestamp($airing_data->nextAiringEpisode->airingAt) ."\n";
DB::table('airing')
->where('mal_id', $anime->mal_id)
->where('episode', $airing->episode)
->where('aired_at', $airing->aired_at)
->delete();
}
}
} else {
echo "Episode data is null. (" . $anime->mal_id . "). | ";
}
$new_airing = new Airing();
$new_airing->mal_id = $anime->mal_id;
if ( ! is_null($airing_data->nextAiringEpisode) ) {
$new_airing->episode = $airing_data->nextAiringEpisode->episode;
}
if ( ! is_null($airing_data->nextAiringEpisode) ) {
$new_airing->aired_at = Carbon::createFromTimestamp($airing_data->nextAiringEpisode->airingAt);
}
if ( ! is_null($airing_data) ) {
$new_airing->duration = $airing_data->duration;
}
if ( is_null($airing_data->duration) ) {
echo "saveAiring: No duration found for anime: " . $anime->title_pref . " (" . $anime->mal_id . "). (Autoset to 20.)\n";
$new_airing->duration = 20;
}
if ( is_null($new_airing->aired_at) ) {
echo "saveAiring: No airing date found for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
return;
}
$new_airing->save();
}
public function saveAiringForAll() {
$anime_all = Anime::where('airing_status', env('ANIME_IS_AIRING'))->get();
foreach( $anime_all as $anime ) {
$this->saveAiring($anime);
sleep(1);
}
}
/**
* Save entire anime season in database.
*/
public function saveSeason() {
$jikan = new MalClient;
$season = $jikan->getSeasonal(
(new \Jikan\Request\Seasonal\SeasonalRequest(
))
);
sleep(10);
foreach($season->anime as $entry) {
$this->saveAnime($entry);
sleep(10);
}
}
public function saveAnimeStats(Anime $anime = null) {
if ( is_null($anime) ) {
$anime = Anime::get();
}
$counter = 0;
$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();
if (date('Ymd') == date('Ymd', strtotime($date)) ) {
continue;
}
}
$animeStats = new AnimeStats();
if ( $animeStats->fill($entry->mal_id) ) {
# Only save if no error
$animeStats->save();
$counter++;
} else {
$failure++;
}
if ( 3 < $failure ) {
echo "Got " . $failure . " failures and " . $counter . " entries. Returning...\n";
return;
}
# sleep to avoid 403
sleep(5);
}
if ( 0 < $counter ) {
echo "Got stats for " . $counter . " anime.\n";
}
}
public function checkIfIsAiring() {
$jikan = new Malclient;
foreach( Anime::where('airing_status', '!=', env('ANIME_FINISHED_AIRING'))->get() as $anime) {
try {
$animeInfo = $jikan->getAnime(
(new \Jikan\Request\Anime\AnimeRequest( $anime->mal_id ))
);
} catch (\Exception $e) {
echo "checkIfIsAiring: Problem requesting AnimeInfo for (" . $anime->mal_id . ") continue\n";
echo $e->getMessage();
echo "\n\n";
sleep(10);
continue;
}
$airing_status = getAiringStatusCode( $animeInfo->getStatus() );
if ( $anime->airing_status != $airing_status ) {
DB::table('anime')
->where('mal_id', $anime->mal_id)
->update(['airing_status' => $airing_status]);
}
sleep(5);
}
}
public function addEnhancementToAnime( Anime $anime ) {
/**
* Try to get Data from Anilist.
* On error return early.
*/
try {
$enhancement = $anime->getEnhancementFromAnilist();
} catch( \Exception $e ) {
echo "addEnhancementToAnime: Getting Data from Anilist failed for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
return;
}
if ( "" == $enhancement ) {
return;
}
$update = array();
if ( ! is_null( $enhancement->description ) ) {
$update["synopsis"] = str_replace('
', '%%br%%', $enhancement->description);
}
if ( ! is_null( $enhancement->hashtag ) ) {
$update["hashtag"] = $enhancement->hashtag;
}
if ( ! is_null( $enhancement->title->userPreferred ) ) {
$update["title_pref"] = $enhancement->title->userPreferred;
}
if ( ! is_null( $enhancement->duration ) ) {
$update["duration"] = $enhancement->duration;
}
if ( isset( $update["synopsis"] ) ) {
$update["synopsis"] = str_replace('', '', $update["synopsis"]);
}
#echo "Updating " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
DB::table('anime')
->where('mal_id', $anime->mal_id)
->update( $update );
}
public function saveEnhancementForAll() {
#$anime_all = Anime::where('airing_status', env('ANIME_IS_AIRING'))->where('synopsis', '')->get();
$anime_all = Anime::where('synopsis', '')->orWhereNull('duration')->get();
foreach( $anime_all as $anime ) {
$this->addEnhancementToAnime($anime);
sleep(1);
}
}
public function saveTopAnime($startpage = 1, $endpage = 10) {
$jikan = new MalClient;
for ( $page = $startpage; $page <= $endpage; $page++) {
$topAnime = $jikan->getTopAnime(
(new \Jikan\Request\Top\TopAnimeRequest($page))
);
sleep(5);
foreach ( $topAnime as $anime) {
if ( $this->saveAnime($anime) ) {
echo "saveTopAnime: Saved (" . $anime->getMalId() . ")\n";
}
sleep(5);
}
}
}
public function saveTopAnimeByPopularity($startpage = 1, $endpage = 10) {
$jikan = new MalClient;
for ( $page = $startpage; $page <= $endpage; $page++) {
$topAnime = $jikan->getTopAnime(
(new \Jikan\Request\Top\TopAnimeRequest(
$page,
Constants::TOP_BY_POPULARITY))
);
sleep(5);
foreach ( $topAnime as $anime) {
if ( $this->saveAnime($anime) ) {
echo "saveTopAnime: Saved (" . $anime->getMalId() . ")\n";
}
sleep(5);
}
}
}
private function saveAnime($entry, $caller = "saveAnime") {
if ( Anime::where('mal_id', $entry->getMalID())->exists() ) {
/**
* We already have this anime saved.
*/
echo $caller . ": Duplicate entry: (" . $entry->getMalID() . ") Continue\n";
return false;
}
$anime = new Anime();
/**
* Sleep to avoid 403 by MAL.
*/
$anime->fill( $entry->getMalID() );
if ( "" == $anime->url ) {
echo $caller . ": url is empty for: " . $entry->getMalId() . "\n";
return false;
}
if( ! DB::table('anime')->where('mal_id', $entry->getMalID() )->exists() ) {
$anime->save();
}
return true;
}
public function addSeasonToAllAnime() {
$jikan = new Malclient;
foreach( Anime::where('airing_status', '!=', env('ANIME_NOT_YET_AIRED'))->where(
function($q){ $q->where('season_name', '=', '')->orWhereNull('season_name'); }
)->get() as $anime )
{
try {
$animeInfo = $jikan->getAnime(
(new \Jikan\Request\Anime\AnimeRequest( $anime->mal_id ))
);
} catch (\Exception $e) {
echo "addSeasonToAllAnime(): Problem requesting AnimeInfo for (" . $anime->mal_id . ") continue\n";
echo $e->getMessage();
echo "\n\n";
sleep(10);
continue;
}
if ( is_null( $animeInfo->getPremiered() ) ) {
$tmp_date = getSeasonFromDate( $animeInfo->getAired()->getFrom() );
$season_name = $tmp_date["name"];
$season_year = $tmp_date["year"];
} else {
$season_name = explode(" ", $animeInfo->getPremiered())[0];
$season_year = explode(" ", $animeInfo->getPremiered())[1];
}
DB::table('anime')
->where('mal_id', $anime->mal_id)
->update([
'season_name' => $season_name,
'season_year' => $season_year,
]);
sleep(5);
}
}
public function checkImage(Anime $anime = null) {
if ( is_null($anime) ) {
$anime = Anime::where('season_year', '=', date("Y"))
->orWhere('season_year', '=', date("Y")-1)
->orWhereNull('season_year')->get();
}
$count = 0;
foreach( $anime as $entry ) {
$count++;
$fill = new Anime;
$fill->fill( $entry->mal_id, $skip_if_unpopular=false ); # check every anime we have, even with less than 10k members
if ( $fill->image_url != $entry->image_url ) {
echo "Image URL (" . $entry->mal_id . ", " . $entry->title_pref . ") changed from " . $entry->image_url . " to " . $fill->image_url . "\n";
DB::table('anime')
->where('mal_id', $fill->mal_id)
->update(['image_url' => $fill->image_url]);
}
sleep(5);
}
echo $count;
}
}