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/Libraries/AnimeSeason.php | 21 +++++------- app/Libraries/Background.php | 80 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 85 insertions(+), 16 deletions(-) (limited to 'app/Libraries') 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