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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
<?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 $animeList;
private $animeInfo;
private const STATUS_WATCHING = 1;
public function __construct( $username = "", $autoparse = true ) {
if ( $autoparse && "" != $username ) {
$this->parse( $username );
}
}
public function parse( $username ) {
$this->jikan = new MalClient;
$this->animeList = $this->jikan->getUserAnimelist(
new \Jikan\Request\User\UserAnimeListRequest( $username, 1, 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->url = $entry->getUrl();
$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 .")\n\n" . $anime->url );
$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;
}
protected function getIsWatching( $username ) {
$animeList = $this->getUserAnimeList( $username );
$anime = array();
foreach ( $animeList as $entry ) {
// currently watching
if ( $this::STATUS_WATCHING != $entry->getAiringStatus() ){
continue;
}
$anime[] = array(
"mal_id" => $entry->getMalID(),
"episodes_watched" => $entry->getWatchedEpisodes(),
"score_user" => $entry->getScore(),
);
}
return $anime;
}
protected function getUserAnimeList( $username ) {
$jikan = new MalClient;
$animeList = $jikan->getUserAnimelist(
new \Jikan\Request\User\UserAnimeListRequest( $username, 1, 1 )
);
return $animeList;
}
public function test() {
echo "<pre>";
var_dump( $this->getIsWatching('ll-') );
}
}
|