diff options
| author | horus | 2020-03-05 22:24:28 +0100 |
|---|---|---|
| committer | horus | 2020-03-05 22:24:36 +0100 |
| commit | 38ad5415db862e24da03ee194ef723540f9848a6 (patch) | |
| tree | 2f9a5bf2f53029bc21c338d9ef865638d69c8454 | |
| parent | 095aba3e990afe5b181524e4830dc4d85e9475be (diff) | |
| download | senpai-38ad5415db862e24da03ee194ef723540f9848a6.tar.gz | |
Basic usage implemented.
| -rw-r--r-- | app/Anime.php | 34 | ||||
| -rw-r--r-- | app/AnimeStats.php | 13 | ||||
| -rw-r--r-- | app/Console/Kernel.php | 3 | ||||
| -rw-r--r-- | app/Libraries/AnimeSeason.php | 21 | ||||
| -rw-r--r-- | app/Libraries/Background.php | 80 | ||||
| -rw-r--r-- | database/migrations/2020_03_05_185234_add_isairing_to_anime.php | 32 | ||||
| -rw-r--r-- | todo.txt | 11 |
7 files changed, 156 insertions, 38 deletions
diff --git a/app/Anime.php b/app/Anime.php index 7971430..9e6ac58 100644 --- a/app/Anime.php +++ b/app/Anime.php @@ -25,35 +25,41 @@ class Anime extends Model { 'title_pref', 'anime_type', 'broadcasted', - 'episodes' + 'episodes', + 'is_airing' ]; public function __construct() { } - public function fill( $id, $parse_info = true ) { + public function fill( $id ) { $this->mal_id = $id; - if ( $parse_info ) { - $jikan = new Malclient; + $jikan = new Malclient; + try { $this->animeInfo = $jikan->getAnime( (new \Jikan\Request\Anime\AnimeRequest( $this->mal_id )) ); + } catch (\Exception $e) { + echo "Problem requesting AnimeInfo for (" . $anime->mal_id . ") continue\n"; + echo $e->getMessage(); + echo "\n\n"; + } - $this->url = $this->animeInfo->GetUrl(); - $this->image_url = $this->animeInfo->getImageUrl(); + $this->url = $this->animeInfo->GetUrl(); + $this->image_url = $this->animeInfo->getImageUrl(); - $this->title_eng = $this->animeInfo->getTitleEnglish(); - $this->title_rom = $this->animeInfo->getTitle(); - $this->title_nat = $this->animeInfo->getTitleJapanese(); - $this->title_pref = $this->animeInfo->getTitle(); + $this->title_eng = $this->animeInfo->getTitleEnglish(); + $this->title_rom = $this->animeInfo->getTitle(); + $this->title_nat = $this->animeInfo->getTitleJapanese(); + $this->title_pref = $this->animeInfo->getTitle(); - $this->anime_type = $this->animeInfo->getType(); - $this->broadcasted = $this->animeInfo->getBroadcast(); + $this->anime_type = $this->animeInfo->getType(); + $this->broadcasted = $this->animeInfo->getBroadcast(); - $this->episodes = $this->animeInfo->getEpisodes(); - } + $this->episodes = $this->animeInfo->getEpisodes(); + $this->is_airing = $this->animeInfo->isAiring(); } public function getStats() { diff --git a/app/AnimeStats.php b/app/AnimeStats.php index 827032c..765d24f 100644 --- a/app/AnimeStats.php +++ b/app/AnimeStats.php @@ -94,7 +94,6 @@ class AnimeStats extends Model { $this->season_name= $season_name; } - #$animeInfo = $this->getInfo(); $animeInfo = $jikan->getAnime( (new \Jikan\Request\Anime\AnimeRequest( $this->mal_id )) ); @@ -105,9 +104,15 @@ class AnimeStats extends Model { $this->members = $animeInfo->getMembers(); $this->favorites = $animeInfo->getFavorites(); - $animeStats = $jikan->getAnimeStats( - (new \Jikan\Request\Anime\AnimeStatsRequest( $this->mal_id )) - ); + try { + $animeStats = $jikan->getAnimeStats( + (new \Jikan\Request\Anime\AnimeStatsRequest( $this->mal_id )) + ); + } catch (\Exception $e) { + echo "Error requesting AnimeStats for " . $this->mal_id ."\n"; + echo "Message: " . $e->getMessage(); + echo "\n\n"; + } $this->watching = $animeStats->getWatching(); $this->completed = $animeStats->getCompleted(); diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index fa58874..e3fb5c4 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -7,6 +7,7 @@ use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use App\Libraries\AnimeSeason; use App\Libraries\Helper; +use App\Libraries\Background; class Kernel extends ConsoleKernel { @@ -35,6 +36,8 @@ class Kernel extends ConsoleKernel #$helper = new Helper(); #$helper->setAiringForAll(); #$helper->setCalendar( 'll-' ); + $background = new Background(); + #$background->checkIfIsAiring(); }); } diff --git a/app/Libraries/AnimeSeason.php b/app/Libraries/AnimeSeason.php index f0296af..96afa13 100644 --- a/app/Libraries/AnimeSeason.php +++ b/app/Libraries/AnimeSeason.php @@ -35,7 +35,6 @@ class AnimeSeason { $this->year = $season->seasonYear; $this->name = $season->seasonName; - $count = 0; foreach($season->anime as $entry) { /** * Debug @@ -51,7 +50,13 @@ class AnimeSeason { $count++; - #Anime::where('mal_id', $entry->getMalID() ); + $check = Anime::where('mal_id', $entry->getMalID() )->first()->get(); + if ( ! is_null($check) ) { + /** + * We already have this anime saved. + */ + } + $anime = new Anime(); $anime->fill( $entry->getMalID() ); @@ -59,19 +64,11 @@ class AnimeSeason { $anime->save(); } - $animeStats = new AnimeStats( $entry->getMalID(), $this->year, $this->name ); + $animeStats = new AnimeStats(); + $animeStats->fill( $entry->getMalID(), $this->year, $this->name ); $this->anime[] = $animeStats; $animeStats->save(); - - /* - echo "<pre>"; - var_dump($animeStats); - - if ( $count == 1) { - return; - } - */ } } 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]); + } + } + } } diff --git a/database/migrations/2020_03_05_185234_add_isairing_to_anime.php b/database/migrations/2020_03_05_185234_add_isairing_to_anime.php new file mode 100644 index 0000000..09d8753 --- /dev/null +++ b/database/migrations/2020_03_05_185234_add_isairing_to_anime.php @@ -0,0 +1,32 @@ +<?php + +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; + +class AddIsairingToAnime extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::table('anime', function (Blueprint $table) { + $table->boolean('is_airing')->default(false); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('anime', function (Blueprint $table) { + // + }); + } +} @@ -1,8 +1,11 @@ Background jobs: -1. Alle Anime der Season downloaden und in Datenbank einpflegen -2. Für jeden Anime die Statistiken downloadnen -3. Für alle aktuellen Anime den nächsten Airing Zeitpunkt einpflegen -4. Für jeden User einen Kalender generieren +[x] 1. Alle Anime der Season downloaden und in Datenbank einpflegen [App\Background] +[x] 2. Für jeden Anime die Statistiken downloadnen [App\Background] +[x] 3. Für alle *aktuellen* Anime den nächsten Airing Zeitpunkt einpflegen [App\Background] +[x] 4. Für jeden User einen Kalender generieren [App\Helper] + 5. Die Top 1000 der populärsten Anime in die Datenbank übertragen 6. Dasselbe für die Top 1000 der besten Anime. + +[x] 7. Per Flag kontrollieren, ob ein Anime gerade läuft, also aktuell ist. [App\Background] |
