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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
<?php
namespace App\Libraries;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Jikan\MyAnimeList\MalClient;
use Jikan\Helper\Constants;
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 saveAiring(Anime $anime) {
if ( $anime->airing_status != env("ANIME_IS_AIRING") ) {
echo "saveAiring: Anime (" . $anime->mal_id . ") is not airing. Skipping.\n";
return;
}
$stats = $anime->getStats()->get()->last();
if ( is_null($stats) || is_null($stats->score) ) {
echo "saveAiring: getStats() is null. returning for anime:" . $anime->title_pref . " (" . $anime->mal_id . ")\n";
return;
}
$airing = Airing::where('mal_id', $stats->mal_id)->get()->last();
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 "saveAiring: Getting Data from Anilist failed for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
return;
}
if ( "" == $airing_data ) {
echo "saveAiring: 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 == Carbon::createFromTimestamp($airing_data->nextAiringEpisode->airingAt) ) {
/**
* Double entry.
*/
echo "saveAiring: Double entry: (" . $anime->mal_id . ") Episode: " . $airing->episode . "\n";
return;
} else {
/**
* We need to delete/reinsert because the airing date was postponed.
* Keep in mind that it's still the same episode which should be aired.
*/
echo "saveAiring: The airing was postponed: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
echo "saveAiring: Original: " . $airing->episode . "/" . $airing->aired_at . " New: " . Carbon::createFromTimestamp($airing_data->nextAiringEpisode->airingAt) ."\n";
DB::table('airing')
->where('mal_id', $anime->mal_id)
->where('episode', $airing->episode)
->where('aired_at', $airing->aired_at)
->delete();
}
}
}
$new_airing = new Airing();
$new_airing->mal_id = $anime->mal_id;
if ( ! is_null($airing_data->nextAiringEpisode) ) {
$new_airing->episode = $airing_data->nextAiringEpisode->episode;
}
if ( ! is_null($airing_data->nextAiringEpisode) ) {
$new_airing->aired_at = Carbon::createFromTimestamp($airing_data->nextAiringEpisode->airingAt);
}
if ( ! is_null($airing_data) ) {
$new_airing->duration = $airing_data->duration;
}
if ( is_null($airing_data->duration) ) {
echo "saveAiring: No duration found for anime: " . $anime->title_pref . " (" . $anime->mal_id . "). (Autoset to 20.)\n";
$new_airing->duration = 20;
}
if ( is_null($new_airing->aired_at) ) {
echo "saveAiring: No airing date found for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
return;
}
$new_airing->save();
}
public function saveAiringForAll() {
$anime_all = Anime::where('airing_status', env('ANIME_IS_AIRING'))->get();
foreach( $anime_all as $anime ) {
$this->saveAiring($anime);
sleep(1);
}
}
/**
* Save entire anime season in database.
*/
public function saveSeason() {
$jikan = new MalClient;
$season = $jikan->getSeasonal(
(new \Jikan\Request\Seasonal\SeasonalRequest(
))
);
sleep(10);
foreach($season->anime as $entry) {
$this->saveAnime($entry);
sleep(10);
}
}
public function saveAnimeStats(Anime $anime = null) {
if ( is_null($anime) ) {
$anime = Anime::get();
}
$counter = 0;
$failure = 0;
foreach($anime as $entry ) {
# skip if we have crawled stats from today
if ( ! empty($entry->getStats()->get()->last()) ) {
$date = $entry->getStats()->get()->last()->created_at->toDateTimeString();
if (date('Ymd') == date('Ymd', strtotime($date)) ) {
continue;
}
}
$animeStats = new AnimeStats();
if ( $animeStats->fill($entry->mal_id) ) {
# Only save if no error
$animeStats->save();
$counter++;
} else {
$failure++;
}
if ( 3 < $failure ) {
echo "Got " . $failure . " failures and " . $counter . " entries. Returning...\n";
return;
}
# sleep to avoid 403
sleep(5);
}
if ( 0 < $counter ) {
echo "Got stats for " . $counter . " anime.\n";
}
}
public function checkIfIsAiring() {
$jikan = new Malclient;
foreach( Anime::where('airing_status', '!=', env('ANIME_FINISHED_AIRING'))->get() as $anime) {
try {
$animeInfo = $jikan->getAnime(
(new \Jikan\Request\Anime\AnimeRequest( $anime->mal_id ))
);
} catch (\Exception $e) {
echo "checkIfIsAiring: Problem requesting AnimeInfo for (" . $anime->mal_id . ") continue\n";
echo $e->getMessage();
echo "\n\n";
sleep(10);
continue;
}
$airing_status = getAiringStatusCode( $animeInfo->getStatus() );
if ( $anime->airing_status != $airing_status ) {
DB::table('anime')
->where('mal_id', $anime->mal_id)
->update(['airing_status' => $airing_status]);
}
sleep(5);
}
}
public function addEnhancementToAnime( Anime $anime ) {
/**
* Try to get Data from Anilist.
* On error return early.
*/
try {
$enhancement = $anime->getEnhancementFromAnilist();
} catch( \Exception $e ) {
echo "addEnhancementToAnime: Getting Data from Anilist failed for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
return;
}
if ( "" == $enhancement ) {
return;
}
$update = array();
if ( ! is_null( $enhancement->description ) ) {
$update["synopsis"] = str_replace('<br>', '%%br%%', $enhancement->description);
}
if ( ! is_null( $enhancement->hashtag ) ) {
$update["hashtag"] = $enhancement->hashtag;
}
if ( ! is_null( $enhancement->title->userPreferred ) ) {
$update["title_pref"] = $enhancement->title->userPreferred;
}
if ( ! is_null( $enhancement->duration ) ) {
$update["duration"] = $enhancement->duration;
}
#echo "Updating " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
DB::table('anime')
->where('mal_id', $anime->mal_id)
->update( $update );
}
public function saveEnhancementForAll() {
#$anime_all = Anime::where('airing_status', env('ANIME_IS_AIRING'))->where('synopsis', '')->get();
$anime_all = Anime::where('synopsis', '')->orWhereNull('duration')->get();
foreach( $anime_all as $anime ) {
$this->addEnhancementToAnime($anime);
sleep(1);
}
}
public function saveTopAnime($startpage = 1, $endpage = 10) {
$jikan = new MalClient;
for ( $page = $startpage; $page <= $endpage; $page++) {
$topAnime = $jikan->getTopAnime(
(new \Jikan\Request\Top\TopAnimeRequest($page))
);
sleep(5);
foreach ( $topAnime as $anime) {
if ( $this->saveAnime($anime) ) {
echo "saveTopAnime: Saved (" . $anime->getMalId() . ")\n";
}
sleep(5);
}
}
}
public function saveTopAnimeByPopularity($startpage = 1, $endpage = 10) {
$jikan = new MalClient;
for ( $page = $startpage; $page <= $endpage; $page++) {
$topAnime = $jikan->getTopAnime(
(new \Jikan\Request\Top\TopAnimeRequest(
$page,
Constants::TOP_BY_POPULARITY))
);
sleep(5);
foreach ( $topAnime as $anime) {
if ( $this->saveAnime($anime) ) {
echo "saveTopAnime: Saved (" . $anime->getMalId() . ")\n";
}
sleep(5);
}
}
}
private function saveAnime($entry, $caller = "saveAnime") {
if ( Anime::where('mal_id', $entry->getMalID())->exists() ) {
/**
* We already have this anime saved.
*/
echo $caller . ": Duplicate entry: (" . $entry->getMalID() . ") Continue\n";
return false;
}
$anime = new Anime();
/**
* Sleep to avoid 403 by MAL.
*/
$anime->fill( $entry->getMalID() );
if ( "" == $anime->url ) {
echo $caller . ": url is empty for: " . $entry->getMalId() . "\n";
return false;
}
if( ! DB::table('anime')->where('mal_id', $entry->getMalID() )->exists() ) {
$anime->save();
}
return true;
}
public function addSeasonToAllAnime() {
$jikan = new Malclient;
foreach( Anime::where('airing_status', '!=', env('ANIME_NOT_YET_AIRED'))->where(
function($q){ $q->where('season_name', '=', '')->orWhereNull('season_name'); }
)->get() as $anime )
{
try {
$animeInfo = $jikan->getAnime(
(new \Jikan\Request\Anime\AnimeRequest( $anime->mal_id ))
);
} catch (\Exception $e) {
echo "addSeasonToAllAnime(): Problem requesting AnimeInfo for (" . $anime->mal_id . ") continue\n";
echo $e->getMessage();
echo "\n\n";
sleep(10);
continue;
}
if ( is_null( $animeInfo->getPremiered() ) ) {
$tmp_date = getSeasonFromDate( $animeInfo->getAired()->getFrom() );
$season_name = $tmp_date["name"];
$season_year = $tmp_date["year"];
} else {
$season_name = explode(" ", $animeInfo->getPremiered())[0];
$season_year = explode(" ", $animeInfo->getPremiered())[1];
}
DB::table('anime')
->where('mal_id', $anime->mal_id)
->update([
'season_name' => $season_name,
'season_year' => $season_year,
]);
sleep(5);
}
}
public function checkImage(Anime $anime = null) {
if ( is_null($anime) ) {
$anime = Anime::where('season_year', '=', date("Y"))
->orWhere('season_year', '=', date("Y")-1)
->orWhereNull('season_year')->get();
}
$count = 0;
foreach( $anime as $entry ) {
$count++;
$fill = new Anime;
$fill->fill( $entry->mal_id, $skip_if_unpopular=false ); # check every anime we have, even with less than 10k members
if ( $fill->image_url != $entry->image_url ) {
echo "Image URL (" . $entry->mal_id . ", " . $entry->title_pref . ") changed from " . $entry->image_url . " to " . $fill->image_url . "\n";
DB::table('anime')
->where('mal_id', $fill->mal_id)
->update(['image_url' => $fill->image_url]);
}
sleep(5);
}
echo $count;
}
}
|