blob: 79d3ab19c97c1d539d610198fd53553b5e23eb06 (
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
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
|
<?php
namespace App\Libraries;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Jikan\MyAnimeList\MalClient;
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) {
$stats = $anime->getStats()->get()->first();
if ( is_null($stats->score) ) {
return;
}
$airing = Airing::where('mal_id', $stats->mal_id)->get()->first();
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 "Getting Data from Anilist failed for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
return;
}
if ( "" == $airing_data ) {
echo "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 == $airing_data->nextAiringEpisode->airingAt ) {
/**
* Double entry.
*/
return;
}
/**
* We need to update because the airing data was postponed.
*/
}
}
$airing->mal_id = $anime->mal_id;
if ( ! is_null($airing_data->nextAiringEpisode) ) {
$airing->episode = $airing_data->nextAiringEpisode->episode;
}
if ( ! is_null($airing_data->nextAiringEpisode) ) {
$airing->aired_at = Carbon::createFromTimestamp($airing_data->nextAiringEpisode->airingAt);
}
if ( ! is_null($airing_data) ) {
$airing->duration = $airing_data->duration;
}
if ( is_null($airing_data->duration) ) {
echo "No duration found for anime: " . $anime->title_pref . " (" . $anime->mal_id . "). (Autoset to 20.)\n";
$airing->duration = 20;
}
if ( is_null($airing->aired_at) ) {
echo "No airing date found for anime: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
return;
}
$airing->save();
}
public function saveAiringForAll() {
$anime_all = Anime::where('is_airing', true)->get();
foreach( $anime_all as $anime ) {
$this->setAiring($anime);
sleep(1);
}
}
/**
* Save entire anime season in database.
*/
public function saveSeason() {
$jikan = new MalClient;
$season = $jikan->getSeasonal(
(new \Jikan\Request\Seasonal\SeasonalRequest(
))
);
foreach($season->anime as $entry) {
/**
* Sleep to avoid 403 by MAL.
*/
sleep(10);
$check = Anime::where('mal_id', $entry->getMalID() )->first()->get();
if ( ! is_null($check) ) {
/**
* We already have this anime saved.
*/
echo "Duplicate entry: (" . $entry->getMalID() . ") Continue\n";
continue;
}
$anime = new Anime();
$anime->fill( $entry->getMalID(), $is_airing=true );
if( ! DB::table('anime')->where('mal_id', $entry->getMalID() )->exists() ) {
$anime->save();
}
}
}
public function saveAnimeStats(Anime $anime = null) {
if ( is_null($anime) ) {
$anime = Anime::get();
}
foreach($anime as $entry ) {
$animeStats = new AnimeStats();
$animeStats->fill( $entry->getMalID() );
$animeStats->save();
}
}
public function checkIfIsAiring() {
$jikan = new Malclient;
foreach( Anime::where('is_airing', true)->get() as $anime) {
try {
$animeInfo = $jikan->getAnime(
(new \Jikan\Request\Anime\AnimeRequest( $anime->mal_id ))
);
} catch (\Exception $e) {
echo "Problem requesting AnimeInfo for (" . $anime->mal_id . ") continue\n";
echo $e->getMessage();
echo "\n\n";
}
if ( ! $animeInfo->isAiring() ) {
DB::table('anime')
->where('mal_id', $anime->mal_id)
->update(['is_airing' => false]);
}
}
}
}
|