diff options
| -rw-r--r-- | app/Anime.php | 35 | ||||
| -rw-r--r-- | app/Console/Kernel.php | 6 | ||||
| -rw-r--r-- | app/Libraries/Background.php | 44 | ||||
| -rw-r--r-- | database/migrations/2020_03_19_021956_add_enhancement_to_anime.php | 33 |
4 files changed, 118 insertions, 0 deletions
diff --git a/app/Anime.php b/app/Anime.php index 5fd4db3..76eca09 100644 --- a/app/Anime.php +++ b/app/Anime.php @@ -126,4 +126,39 @@ class Anime extends Model { return $data; } + public function getEnhancementFromAnilist() { + + $query = ' + query($id: Int!) { + Media(idMal: $id, type: ANIME) { + title { + userPreferred + } + description + hashtag + } + } + '; + $variables = [ + "id" => $this->mal_id, + ]; + + // Make the HTTP Api request + try { + $http = new \GuzzleHttp\Client; + $response = $http->post('https://graphql.anilist.co', [ + 'json' => [ + 'query' => $query, + 'variables' => $variables, + ] + ]); + + $data = json_decode( $response->getBody() )->data->Media; + } catch (\Exception $e) { + echo "Problem with Guzzle connecting to Anilist on anime: (" . $this->mal_id . ")\n"; + return ""; + } + return $data; + + } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index a1cac43..2bb1fea 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -58,6 +58,12 @@ class Kernel extends ConsoleKernel $helper->setCalendarForAll(); })->dailyAt('19:30'); #})->everyMinute(); + + $schedule->call( function(){ + $background = new Background(); + $background->saveEnhancementForAll(); + })->weeklyOn(1, '15:30'); + #})->everyMinute(); } /** diff --git a/app/Libraries/Background.php b/app/Libraries/Background.php index 1c271f1..d0126f0 100644 --- a/app/Libraries/Background.php +++ b/app/Libraries/Background.php @@ -138,6 +138,10 @@ class Background { sleep(10); $anime->fill( $entry->getMalID(), $is_airing=true ); + if ( "" == $anime->mal_url ) { + continue; + } + if( ! DB::table('anime')->where('mal_id', $entry->getMalID() )->exists() ) { $anime->save(); } @@ -178,4 +182,44 @@ class Background { } } } + + 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; + } + + 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('is_airing', true)->where('synopsis', '')->get(); + foreach( $anime_all as $anime ) { + $this->addEnhancementToAnime($anime); + sleep(1); + } + } } diff --git a/database/migrations/2020_03_19_021956_add_enhancement_to_anime.php b/database/migrations/2020_03_19_021956_add_enhancement_to_anime.php new file mode 100644 index 0000000..01b4aaa --- /dev/null +++ b/database/migrations/2020_03_19_021956_add_enhancement_to_anime.php @@ -0,0 +1,33 @@ +<?php + +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; + +class AddEnhancementToAnime extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::table('anime', function (Blueprint $table) { + $table->text('synopsis')->nullable()->default(''); + $table->string('hashtag')->nullable()->default(''); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('anime', function (Blueprint $table) { + // + }); + } +} |
