From 38ad5415db862e24da03ee194ef723540f9848a6 Mon Sep 17 00:00:00 2001 From: horus Date: Thu, 5 Mar 2020 22:24:28 +0100 Subject: Basic usage implemented. --- app/Anime.php | 34 ++++++++++-------- app/AnimeStats.php | 13 ++++--- app/Console/Kernel.php | 3 ++ app/Libraries/AnimeSeason.php | 21 +++++------- app/Libraries/Background.php | 80 ++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 117 insertions(+), 34 deletions(-) (limited to 'app') 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 "
";
-			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 @@
 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]);
+			}
+		}
+	}
 }
-- 
cgit v1.2.3