summaryrefslogtreecommitdiff
path: root/app/Libraries/AnimeSchedule.php
blob: 4c16f1136db4ed452d1c252b23a336a0d4de0bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

namespace App\Libraries;
use App\Libraries\AnimeSchedule;
use App\AnimeWatching;

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 AnimeWatching( $entry->getMalID(), false );

			$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;
	}
}