summaryrefslogtreecommitdiff
path: root/app/Libraries
diff options
context:
space:
mode:
Diffstat (limited to 'app/Libraries')
-rw-r--r--app/Libraries/AnimeSchedule.php138
-rw-r--r--app/Libraries/AnimeSeason.php32
2 files changed, 170 insertions, 0 deletions
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 @@
+<?php
+
+namespace App\Libraries;
+use App\Libraries\AnimeSchedule;
+use App\Anime;
+
+use Carbon\Carbon;
+use Jikan\MyAnimeList\MalClient;
+
+use Eluceo\iCal\Component\Calendar;
+use Eluceo\iCal\Component\Event;
+
+
+class AnimeSchedule {
+
+ private $animeSchedule = array();
+
+ private $jikan;
+ private $userProfile;
+ private $animeList;
+ private $animeInfo;
+ private const STATUS_WATCHING = 1;
+
+ public function __construct( $username ) {
+ $this->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;
+ }
+}
diff --git a/app/Libraries/AnimeSeason.php b/app/Libraries/AnimeSeason.php
new file mode 100644
index 0000000..cc01ff7
--- /dev/null
+++ b/app/Libraries/AnimeSeason.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Libraries;
+use App\Libraries\AnimeSeason;
+use App\Anime;
+
+use Carbon\Carbon;
+
+use Jikan\MyAnimeList\MalClient;
+use Jikan\Helper\Constants;
+
+
+class AnimeSeason {
+
+ public $year;
+ public $name;
+
+ public $anime;
+
+ public function __construct() {
+ $jikan = new MalClient;
+
+ $season = $jikan->getSeasonal(
+ (new \Jikan\Request\Seasonal\SeasonalRequest(
+ ))
+ );
+
+ $this->year = $season->seasonYear;
+ $this->name= $season->seasonName;
+ }
+
+}