diff options
Diffstat (limited to 'app/Libraries')
| -rw-r--r-- | app/Libraries/Background.php | 34 | ||||
| -rw-r--r-- | app/Libraries/Helper.php | 58 |
2 files changed, 80 insertions, 12 deletions
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 @@ <?php namespace App\Libraries; +use Illuminate\Support\Facades\DB; use Carbon\Carbon; @@ -49,7 +50,17 @@ class Helper { public function setCalendar($username) { $user = MALUser::where('username', $username)->get()->first(); - echo "<pre>"; + + 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; + } } |
