From 2cd1589d6ae7095770e8c5dda10b138861d70501 Mon Sep 17 00:00:00 2001 From: horus Date: Sun, 23 Feb 2020 23:11:03 +0100 Subject: Initial commit. Calendar is working. --- app/Libraries/AnimeSchedule.php | 138 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 app/Libraries/AnimeSchedule.php (limited to 'app/Libraries/AnimeSchedule.php') diff --git a/app/Libraries/AnimeSchedule.php b/app/Libraries/AnimeSchedule.php new file mode 100644 index 0000000..c81d0f7 --- /dev/null +++ b/app/Libraries/AnimeSchedule.php @@ -0,0 +1,138 @@ +jikan = new MalClient; + + $this->userProfile = $this->jikan->getUserProfile( + new \Jikan\Request\User\UserProfileRequest( $username ) + ); + $this->animeList = $this->jikan->getUserAnimelist( + new \Jikan\Request\User\UserAnimeListRequest( $this->userProfile->getUsername(), 1 ) + ); + + foreach ( $this->animeList as $entry ) { + // currently watching + if ( $this::STATUS_WATCHING != $entry->getAiringStatus() ){ + continue; + } + + $data = $this->getDataFromAnilist( $entry->getMalID() ); + + // check if anilist has no info about airing next episode (this happens!) + if ( is_null( $data->nextAiringEpisode ) ) { + continue; + } + + $animeInfo = $this->jikan->getAnime( + (new \Jikan\Request\Anime\AnimeRequest( $entry->getMalID() )) + ); + + $anime = new Anime( $entry->getMalID() ); + + $anime->title_eng = $data->title->english; + $anime->title_rom = $data->title->romaji; + $anime->title_nat = $data->title->native; + $anime->title_pref = $data->title->userPreferred; + + $anime->airing_at = Carbon::createFromTimestamp($data->nextAiringEpisode->airingAt); + $anime->time_until_airing = Carbon::createFromTimestamp($data->nextAiringEpisode->timeUntilAiring); + $anime->episode = $data->nextAiringEpisode->episode; + $anime->duration = $data->duration; + + $anime->episodes_complete = $data->episodes; + $anime->episodes_watched = $entry->getWatchedEpisodes(); + $anime->score_user = $entry->getScore(); + $anime->score = $animeInfo->getScore(); + + $anime->rank = $animeInfo->getRank(); + $anime->popularity = $animeInfo->getPopularity(); + + $this->animeSchedule[] = $anime; + } + } + + public function getSchedule() { + return $this->animeSchedule; + } + + public function getCalendar() { + $vCalendar = new Calendar('animes.iamfabulous.de'); + $vCalendar->setName('Anime Schedule'); + + foreach ( $this->animeSchedule as $anime ) { + $vEvent = new Event(); + + $vEvent + ->setDtStart(new \DateTime($anime->airing_at)) + ->setDtEnd( new \DateTime($anime->airing_at->add($anime->duration, 'minutes')) ) + ->setNoTime(false) + ->setSummary( $anime->title_pref . " (" . $anime->episode . "/" . $anime->episodes_complete . ")" ) + ->setUrl( env("APP_URL") ) + ->setDescription( "(Episode " . $anime->episode . "/" . $anime->episodes_complete . ") You have watched " . $anime->episodes_watched . " episodes of " . $anime->title_pref . " and scored it with " . $anime->score_user . ". (Public score: ". $anime->score .")" ); + + $vCalendar->addComponent($vEvent); + } + + return $vCalendar->render(); + } + + private function getDataFromAnilist( $malID ) { + $query = ' + query($id: Int!) { + Media(idMal: $id, type: ANIME) { + title { + romaji + english + native + userPreferred + } + nextAiringEpisode { + airingAt + timeUntilAiring + episode + } + episodes + duration + } + } + '; + + // Define our query variables and values that will be used in the query request + $variables = [ + "id" => $malID, + ]; + + // Make the HTTP Api request + $http = new \GuzzleHttp\Client; + $response = $http->post('https://graphql.anilist.co', [ + 'json' => [ + 'query' => $query, + 'variables' => $variables, + ] + ]); + + $data = json_decode( $response->getBody() )->data->Media; + return $data; + } +} -- cgit v1.2.3