diff options
Diffstat (limited to 'app/Libraries')
| -rw-r--r-- | app/Libraries/AnimeSchedule.php | 56 | ||||
| -rw-r--r-- | app/Libraries/AnimeSeason.php | 22 | ||||
| -rw-r--r-- | app/Libraries/Background.php | 94 | ||||
| -rw-r--r-- | app/Libraries/Helper.php | 89 |
4 files changed, 251 insertions, 10 deletions
diff --git a/app/Libraries/AnimeSchedule.php b/app/Libraries/AnimeSchedule.php index 4c16f11..3b22494 100644 --- a/app/Libraries/AnimeSchedule.php +++ b/app/Libraries/AnimeSchedule.php @@ -16,19 +16,22 @@ class AnimeSchedule { private $animeSchedule = array(); private $jikan; - private $userProfile; private $animeList; private $animeInfo; private const STATUS_WATCHING = 1; - public function __construct( $username ) { + public function __construct( $username = "", $autoparse = true ) { + + if ( $autoparse && "" != $username ) { + $this->parse( $username ); + } + } + + public function parse( $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 ) + new \Jikan\Request\User\UserAnimeListRequest( $username, 1, 1 ) ); foreach ( $this->animeList as $entry ) { @@ -50,6 +53,8 @@ class AnimeSchedule { $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; @@ -89,7 +94,7 @@ class AnimeSchedule { ->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 .")" ); + ->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); } @@ -135,4 +140,41 @@ class AnimeSchedule { $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-') ); + } } diff --git a/app/Libraries/AnimeSeason.php b/app/Libraries/AnimeSeason.php index 1bc3655..f0296af 100644 --- a/app/Libraries/AnimeSeason.php +++ b/app/Libraries/AnimeSeason.php @@ -21,7 +21,10 @@ class AnimeSeason { public $anime; public function __construct() { - #Anime::where('mal_id', 2 ); + } + + public function save() { + #Anime::where('mal_id', 2 ); $jikan = new MalClient; $season = $jikan->getSeasonal( @@ -34,10 +37,23 @@ class AnimeSeason { $count = 0; foreach($season->anime as $entry) { + /** + * Debug + if ( DB::table('stats')->where('mal_id', $entry->getMalID())->exists() ) { + continue; + } + #*/ + + /** + * Sleep to avoid 403 by MAL. + */ + sleep(10); + $count++; #Anime::where('mal_id', $entry->getMalID() ); - $anime = new Anime( $entry->getMalID() ); + $anime = new Anime(); + $anime->fill( $entry->getMalID() ); if( ! DB::table('anime')->where('mal_id', $entry->getMalID() )->exists() ) { $anime->save(); @@ -51,11 +67,11 @@ class AnimeSeason { /* echo "<pre>"; var_dump($animeStats); - */ if ( $count == 1) { return; } + */ } } diff --git a/app/Libraries/Background.php b/app/Libraries/Background.php new file mode 100644 index 0000000..6c84e97 --- /dev/null +++ b/app/Libraries/Background.php @@ -0,0 +1,94 @@ +<?php +namespace App\Libraries; + +use Carbon\Carbon; + +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 setAiring(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 setAiringForAll() { + $anime_all = Anime::get(); + foreach( $anime_all as $anime ) { + $this->setAiring($anime); + sleep(1); + } + } +} diff --git a/app/Libraries/Helper.php b/app/Libraries/Helper.php new file mode 100644 index 0000000..0652570 --- /dev/null +++ b/app/Libraries/Helper.php @@ -0,0 +1,89 @@ +<?php +namespace App\Libraries; + +use Carbon\Carbon; + +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 Helper { + public function getCalendar( $username ) { + echo "<pre>"; + $user = MALUser::where('username', $username)->get()->first(); + + $vCalendar = new iCalendar('animes.iamfabulous.de'); + $vCalendar->setName('Anime Schedule'); + + foreach( $user->calendar()->get() as $anime ) { + + if ( is_null($anime->episodes_completes) ) { + $episodes_complete = '?'; + } else { + $episodes_complete = $anime->episodes_completes; + } + + $vEvent = new Event(); + + $vEvent + ->setDtStart(new \DateTime($anime->airing_at)) + ->setDtEnd( new \DateTime(Carbon::create($anime->airing_at)->add($anime->duration, 'minutes')) ) + ->setNoTime(false) + ->setSummary( $anime->title . " (" . $anime->episode . "/" . $episodes_complete . ")" ) + ->setUrl( env("APP_URL") ) + ->setDescription( "(Episode " . $anime->episode . "/" . $episodes_complete . ") You have watched " . $anime->episodes_watched . " episodes of " . $anime->title . " and scored it with " . $anime->score_user . ". (Public score: ". $anime->score .")\n\n" . $anime->url ); + + $vCalendar->addComponent($vEvent); + } + + return $vCalendar->render(); + + } + + public function setCalendar($username) { + #$this->setAiring(); return; + $user = MALUser::where('username', $username)->get()->first(); + echo "<pre>"; + foreach( $user->IsWatching as $anime ) { + $stats = $anime->getStats()->get()->first(); + $airing = $anime->getAiring()->get()->first(); + + /** + * Check for duplicate entry. + */ + $check = Calendar::where('mal_id', $anime->mal_id)->where('user_id', $user->id)->where( + function($q) use ($airing){ + $q->where('episode', $airing->episode)->orWhere('airing_at', $airing->aired_at); + })->get()->first(); + + if ( ! is_null( $check ) ) { + echo "duplicate entry. ".$anime->mal_id." continue\n"; continue; + } + + $calendar = new Calendar(); + $calendar->mal_id = $anime->mal_id; + $calendar->user_id = $anime->Watching->user_id; + $calendar->username = $username; + $calendar->airing_at = $airing->aired_at; + $calendar->duration = $airing->duration; + $calendar->episode = $airing->episode; + $calendar->episodes_watched = $anime->Watching->episodes_watched; + $calendar->episodes_complete = $anime->episodes; + $calendar->score = $stats->score; + $calendar->score_user = $anime->Watching->score_user; + $calendar->title = $anime->title_pref; + $calendar->mal_url = $anime->url; + + $calendar->save(); + } + } + + +} |
