From 2edac1f5607839efc70be2d98237a6de4b28d6d8 Mon Sep 17 00:00:00 2001 From: horus Date: Fri, 6 Mar 2020 00:59:24 +0100 Subject: Background jobs should be okay. --- app/Anime.php | 31 +++++++++--------- app/Console/Kernel.php | 16 ++++++--- app/Http/Controllers/TestController.php | 15 ++------- app/Libraries/Background.php | 34 ++++++++++++------- app/Libraries/Helper.php | 58 ++++++++++++++++++++++++++++++++- 5 files changed, 109 insertions(+), 45 deletions(-) (limited to 'app') diff --git a/app/Anime.php b/app/Anime.php index 9e6ac58..5fd4db3 100644 --- a/app/Anime.php +++ b/app/Anime.php @@ -38,7 +38,7 @@ class Anime extends Model { $jikan = new Malclient; try { - $this->animeInfo = $jikan->getAnime( + $animeInfo = $jikan->getAnime( (new \Jikan\Request\Anime\AnimeRequest( $this->mal_id )) ); } catch (\Exception $e) { @@ -47,19 +47,24 @@ class Anime extends Model { echo "\n\n"; } - $this->url = $this->animeInfo->GetUrl(); - $this->image_url = $this->animeInfo->getImageUrl(); + if ( strtolower("not yet aired") == strtolower($animeInfo->getStatus()) ) { + echo "Anime (" . $this->mal_id . ") has not aired yet. Skipping\n"; + return; + } + + $this->url = $animeInfo->GetUrl(); + $this->image_url = $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 = $animeInfo->getTitleEnglish(); + $this->title_rom = $animeInfo->getTitle(); + $this->title_nat = $animeInfo->getTitleJapanese(); + $this->title_pref = $animeInfo->getTitle(); - $this->anime_type = $this->animeInfo->getType(); - $this->broadcasted = $this->animeInfo->getBroadcast(); + $this->anime_type = $animeInfo->getType(); + $this->broadcasted = $animeInfo->getBroadcast(); - $this->episodes = $this->animeInfo->getEpisodes(); - $this->is_airing = $this->animeInfo->isAiring(); + $this->episodes = $animeInfo->getEpisodes(); + $this->is_airing = $animeInfo->isAiring(); } public function getStats() { @@ -70,10 +75,6 @@ class Anime extends Model { return $this->hasMany('App\Airing', 'mal_id', 'mal_id'); } - protected function getInfo() { - return $this->animeInfo; - } - public function user() { return $this->belongsToMany('App\MALUser', 'is_watching', 'mal_id', 'user_id') ->as('anime') diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index e3fb5c4..1f24208 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -33,11 +33,17 @@ class Kernel extends ConsoleKernel $schedule->call( function(){ #$season = new AnimeSeason(); #$season->save(); - #$helper = new Helper(); - #$helper->setAiringForAll(); - #$helper->setCalendar( 'll-' ); - $background = new Background(); - #$background->checkIfIsAiring(); + + #$background = new Background(); + #$background->saveSeason(); + #$background->saveAnimeStats(); + #$background->saveAiringForAll(); + + $helper = new Helper(); + $helper->createUser( 'll-' ); + $helper->setIsWatching( 'll-' ); + $helper->setCalendar( 'll-' ); +#$background->checkIfIsAiring(); }); } diff --git a/app/Http/Controllers/TestController.php b/app/Http/Controllers/TestController.php index 0ac9ba2..3254983 100644 --- a/app/Http/Controllers/TestController.php +++ b/app/Http/Controllers/TestController.php @@ -96,18 +96,7 @@ class TestController extends Controller { } public function createUser( $username ) { - $user = MALUser::where('username', $username)->get()->first(); - var_dump( $user ); - - if ( is_null($user) ) { - $user = new MALUser(); - $user->set( $username ); - - var_dump( $user ); - - $user->save(); - } - - echo $user->get(); + $helper = new Helper(); + $helper->createUser( $username ); } } diff --git a/app/Libraries/Background.php b/app/Libraries/Background.php index 79d3ab1..626cc6f 100644 --- a/app/Libraries/Background.php +++ b/app/Libraries/Background.php @@ -19,6 +19,11 @@ use Eluceo\iCal\Component\Event; class Background { public function saveAiring(Anime $anime) { + if ( ! $anime->is_airing ) { + echo "Anime (" . $anime->mal_id . ") is not airing. Skipping.\n"; + return; + } + $stats = $anime->getStats()->get()->first(); if ( is_null($stats->score) ) { return; @@ -55,10 +60,17 @@ class Background { * Double entry. */ 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. + */ + DB::table('airing') + ->where('mal_id', $anime->mal_id) + ->where('episode', $airing->episode) + ->where('aired_at', $airing->aired_at) + ->delete(); } - /** - * We need to update because the airing data was postponed. - */ } } @@ -89,7 +101,7 @@ class Background { public function saveAiringForAll() { $anime_all = Anime::where('is_airing', true)->get(); foreach( $anime_all as $anime ) { - $this->setAiring($anime); + $this->saveAiring($anime); sleep(1); } } @@ -106,13 +118,8 @@ class Background { ); 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) ) { + if ( Anime::where('mal_id', $entry->getMalID())->exists() ) { /** * We already have this anime saved. */ @@ -121,6 +128,11 @@ class Background { } $anime = new Anime(); + + /** + * Sleep to avoid 403 by MAL. + */ + sleep(10); $anime->fill( $entry->getMalID(), $is_airing=true ); if( ! DB::table('anime')->where('mal_id', $entry->getMalID() )->exists() ) { @@ -136,7 +148,7 @@ class Background { foreach($anime as $entry ) { $animeStats = new AnimeStats(); - $animeStats->fill( $entry->getMalID() ); + $animeStats->fill( $entry->mal_id ); $animeStats->save(); } diff --git a/app/Libraries/Helper.php b/app/Libraries/Helper.php index 43b6ff3..88650b4 100644 --- a/app/Libraries/Helper.php +++ b/app/Libraries/Helper.php @@ -1,5 +1,6 @@ get()->first(); - echo "
";
+
+		if ( is_null($user) ) {
+			echo "User (" .  $username . ") does not exists. Skipping\n";
+			return;
+		}
+
+		if ( is_null($user->IsWatching) ) {
+			echo "User (" .  $username . ") does not watch any anime. Skipping\n";
+			return;
+		}
+
                 foreach( $user->IsWatching as $anime ) {
                         $stats = $anime->getStats()->get()->first();
                         $airing = $anime->getAiring()->get()->first();
@@ -84,5 +95,50 @@ class Helper {
                 }
         }
 
+	public function setIsWatching($username) {
+		$user = MALUser::where('username', $username)->get()->first();
+
+		if ( is_null($user) ) {
+			echo "User (" .  $username . ") does not exists. Skipping\n";
+			return;
+		}
+		
+		$actually_watching = array();
+
+		foreach( $user->getIsWatching() as $anime_details ) {
+			$check = DB::table('is_watching')
+				->where('mal_id', $anime_details["mal_id"])
+				->where('user_id', $user->id)
+				->exists();
+			if ( $check ) {
+				$actually_watching[] = $anime_details["mal_id"];
+				continue;
+			}
+			$anime = Anime::where('mal_id', $anime_details["mal_id"])->get()->first();
+			$user->IsWatching()->save( $anime, $anime_details );
+
+			$actually_watching[] = $anime_details["mal_id"];
+		}
+
+		/**
+		 * Check for anime which aren't watched anymore and delete them.
+		 */
+		$not_watched = DB::table('is_watching')
+				->whereNotIn('mal_id', $actually_watching)
+				->delete();
+	}
+
+	public function createUser( $username ) {
+		$user = MALUser::where('username', $username)->get()->first();
+
+		if ( is_null($user) ) {
+			$user = new MALUser();
+			$user->set( $username );
+
+			$user->save();
+		}
+
+		return $user;
+	}
 
 }
-- 
cgit v1.2.3