summaryrefslogtreecommitdiff
path: root/app/Libraries/Background.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Libraries/Background.php')
-rw-r--r--app/Libraries/Background.php94
1 files changed, 94 insertions, 0 deletions
diff --git a/app/Libraries/Background.php b/app/Libraries/Background.php
new file mode 100644
index 0000000..6c84e97
--- /dev/null
+++ b/app/Libraries/Background.php
@@ -0,0 +1,94 @@
+<?php
+namespace App\Libraries;
+
+use Carbon\Carbon;
+
+use App\Libraries\AnimeSchedule;
+use App\Libraries\AnimeSeason;
+use App\Anime;
+use App\AnimeStats;
+use App\MALUser;
+use App\Calendar;
+use App\Airing;
+
+use Eluceo\iCal\Component\Calendar as iCalendar;
+use Eluceo\iCal\Component\Event;
+
+class Background {
+
+ public function setAiring(Anime $anime) {
+ $stats = $anime->getStats()->get()->first();
+ if ( is_null($stats->score) ) {
+ return;
+ }
+
+ $airing = Airing::where('mal_id', $stats->mal_id)->get()->first();
+ if ( is_null($airing) ) {
+ $airing = new Airing();
+ }
+
+ /**
+ * Try to get Data from Anilist.
+ * On error return early.
+ */
+ try {
+ $airing_data = $anime->getDataFromAnilist();
+ } catch( Exception $e ) {
+ echo "Getting Data from Anilist failed for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
+ return;
+ }
+ if ( "" == $airing_data ) {
+ echo "Got empty data from Anilist for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
+ return;
+ }
+
+ /**
+ * Check if we need to save the airing data or if we already have it in database.
+ */
+ if ( ! is_null( $airing->episode ) ) {
+ if ( ! is_null( $airing_data->nextAiringEpisode ) &&
+ ( $airing->episode == $airing_data->nextAiringEpisode->episode) ) {
+ if ( $airing->aired_at == $airing_data->nextAiringEpisode->airingAt ) {
+ /**
+ * Double entry.
+ */
+ return;
+ }
+ /**
+ * We need to update because the airing data was postponed.
+ */
+ }
+ }
+
+ $airing->mal_id = $anime->mal_id;
+ if ( ! is_null($airing_data->nextAiringEpisode) ) {
+ $airing->episode = $airing_data->nextAiringEpisode->episode;
+ }
+ if ( ! is_null($airing_data->nextAiringEpisode) ) {
+ $airing->aired_at = Carbon::createFromTimestamp($airing_data->nextAiringEpisode->airingAt);
+ }
+ if ( ! is_null($airing_data) ) {
+ $airing->duration = $airing_data->duration;
+ }
+
+ if ( is_null($airing_data->duration) ) {
+ echo "No duration found for anime: " . $anime->title_pref . " (" . $anime->mal_id . "). (Autoset to 20.)\n";
+ $airing->duration = 20;
+ }
+
+ if ( is_null($airing->aired_at) ) {
+ echo "No airing date found for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
+ return;
+ }
+
+ $airing->save();
+ }
+
+ public function setAiringForAll() {
+ $anime_all = Anime::get();
+ foreach( $anime_all as $anime ) {
+ $this->setAiring($anime);
+ sleep(1);
+ }
+ }
+}