From 0c521f4907ee74cda8d4b6810a5ccb21c175f857 Mon Sep 17 00:00:00 2001 From: horus Date: Thu, 17 Sep 2020 23:37:00 +0200 Subject: add season to anime model --- app/Anime.php | 16 +++++++- app/Console/Kernel.php | 11 ++++-- app/Http/Controllers/AnimeController.php | 10 +++-- app/Libraries/Background.php | 46 ++++++++++++++++++++++ app/helpers.php | 18 ++++++++- .../2020_09_17_193457_add_season_to_anime.php | 33 ++++++++++++++++ resources/views/anime.blade.php | 6 +++ 7 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 database/migrations/2020_09_17_193457_add_season_to_anime.php diff --git a/app/Anime.php b/app/Anime.php index 08c0dd1..2c2e57d 100644 --- a/app/Anime.php +++ b/app/Anime.php @@ -26,7 +26,9 @@ class Anime extends Model { 'anime_type', 'broadcasted', 'episodes', - 'airing_status' + 'airing_status', + 'season_year', + 'season_name' ]; public function __construct() { @@ -73,6 +75,18 @@ class Anime extends Model { $this->episodes = $animeInfo->getEpisodes(); $this->airing_status = getAiringStatusCode($animeInfo->getStatus()); + + if ( is_null($animeInfo->getPremiered()) ) { + if ( env('ANIME_NOT_YET_AIRED') != $this->airing_status ) { + $tmp_date = getSeasonFromDate( $animeInfo->getAired()->getFrom() ); + + $this->season_name = $tmp_date["name"]; + $this->season_year = $tmp_date["year"]; + } + } else { + $this->season_name = explode(" ", $this->getPremiered())[0]; + $this->season_year = explode(" ", $this->getPremiered())[1]; + } } public function getStats() { diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index dc671fd..b120ddc 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -66,12 +66,14 @@ class Kernel extends ConsoleKernel $schedule->call( function(){ $background = new Background(); - echo "saveTopAnime()"; + echo "saveTopAnime()\n"; $background->saveTopAnime(); - echo "saveTopAnimeByPopularity()"; + echo "saveTopAnimeByPopularity()\n"; $background->saveTopAnimeByPopularity(); echo"saveEnhancementForAll()\n"; $background->saveEnhancementForAll(); + echo "addSeasonToAllAnime()\n" + $background->addSeasonToAllAnime(); })->weeklyOn(1, '15:30')->name('saveTopAnime')->withoutOverlapping(); #})->everyMinute(); @@ -80,10 +82,11 @@ class Kernel extends ConsoleKernel $background = new Background(); #$background->checkIfIsAiring(); #$background->saveSeason(); - $background->saveEnhancementForAll(); + #$background->saveEnhancementForAll(); + #$background->addSeasonToAllAnime(); #$anime = Anime::where('mal_id', 40591)->get()->first(); #$background->saveAiring( $anime ); - })->everyMinute()->name("enhance")->withoutOverlapping(); + })->everyMinute()->name("enhance6")->withoutOverlapping(); #})->twiceDaily(9, 23); */ diff --git a/app/Http/Controllers/AnimeController.php b/app/Http/Controllers/AnimeController.php index 1a0809f..3c1c5c2 100644 --- a/app/Http/Controllers/AnimeController.php +++ b/app/Http/Controllers/AnimeController.php @@ -64,8 +64,13 @@ class AnimeController extends Controller { $score_10[] = $stats->score_10; } - $anime["score"] = $score; - $anime["rank"] = $rank; + + if ( isset($score) ) { + $anime["score"] = $score; + $anime["rank"] = $rank; + + $anime["chart_label_score"] = $created_at_score; + } $anime["popularity"] = $popularity; $anime["members"] = $members; $anime["favorites"] = $favorites; @@ -88,7 +93,6 @@ class AnimeController extends Controller { $anime["score_10"] = $score_10; $anime["chart_label"] = $created_at; - $anime["chart_label_score"] = $created_at_score; $anime["basic_data"] = DB::select('select score, rank, popularity, members, favorites from anime join stats on stats.id = ( select id from stats where anime.mal_id = stats.mal_id order by created_at desc limit 1) where anime.mal_id = ?', [$mal_id])[0]; diff --git a/app/Libraries/Background.php b/app/Libraries/Background.php index b23e519..50efe9b 100644 --- a/app/Libraries/Background.php +++ b/app/Libraries/Background.php @@ -296,4 +296,50 @@ class Background { 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; + } + + echo "Going to update " . $animeInfo->getTitle() . " ( " . $anime->mal_id . ")\n"; + + 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, + ]); + + echo "Updated " . $animeInfo->getTitle() . " to " . $season_name . " " . $season_year . ".\n"; + + + sleep(5); + } + } + } diff --git a/app/helpers.php b/app/helpers.php index 5c65c57..9d1586c 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -1,4 +1,5 @@ ", $string); @@ -67,8 +68,11 @@ function getAiringStatusCode($airing_status) { return $status[0]->id; } -function getCurrentSeason(){ - switch( date("m") ) { +function getSeason( $month = null ){ + if ( is_null($month) ) { + $month = date("m"); + } + switch( $month ) { case 1: case 2: case 3: return "Winter"; @@ -102,3 +106,13 @@ function compare($str1, $str2) { } return true; } + +function getSeasonFromDate($aired_at) { + if ( is_null($ared_at) ) { + return array("name" => null, "year" => null); + } + + $aired_from = Carbon::instance($aired_at); + + return array("name" => getSeason($aired_from->month), "year" => $aired_from->year); +} diff --git a/database/migrations/2020_09_17_193457_add_season_to_anime.php b/database/migrations/2020_09_17_193457_add_season_to_anime.php new file mode 100644 index 0000000..deb2114 --- /dev/null +++ b/database/migrations/2020_09_17_193457_add_season_to_anime.php @@ -0,0 +1,33 @@ +string('season_name')->nullable()->default(''); + $table->integer('season_year')->nullable()->default(DB::raw( 'YEAR(CURDATE())' )); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('anime', function (Blueprint $table) { + // + }); + } +} diff --git a/resources/views/anime.blade.php b/resources/views/anime.blade.php index 2bc0692..0e9da1e 100644 --- a/resources/views/anime.blade.php +++ b/resources/views/anime.blade.php @@ -25,10 +25,12 @@ +@if ( ! is_null($anime["score"]) )

Score History

+@endif

Popularity History

@@ -52,6 +54,7 @@