From 98736ba280573b66eb3d5bf026ed943dc8ceb663 Mon Sep 17 00:00:00 2001 From: horus Date: Thu, 30 Jul 2020 21:27:51 +0200 Subject: Crawls top anime. --- app/Console/Kernel.php | 16 ++++++- app/Http/Controllers/AnimeController.php | 19 ++++++++ app/Libraries/Background.php | 76 ++++++++++++++++++++---------- app/helpers.php | 17 +++++++ resources/views/index_anime.blade.php | 27 ----------- resources/views/surprising_anime.blade.php | 31 ++++++++++++ resources/views/top_anime.blade.php | 29 ++++++++++++ routes/web.php | 2 + 8 files changed, 165 insertions(+), 52 deletions(-) create mode 100644 resources/views/surprising_anime.blade.php create mode 100644 resources/views/top_anime.blade.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 8582678..7663b67 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -37,7 +37,9 @@ class Kernel extends ConsoleKernel $background = new Background(); $background->saveAnimeStats(); })->everyThirtyMinutes()->name('saveStats')->withoutOverlapping(); - #})->everyMinute(); + #})->everyThirtyMinutes()->name('saveStats5')->withoutOverlapping(); + #})->everyMinute()->name('saveStats3')->withoutOverlapping(); + #})->everyMinute()->name('saveStats5')->withoutOverlapping(); $schedule->call( function(){ $background = new Background(); @@ -64,6 +66,8 @@ class Kernel extends ConsoleKernel $schedule->call( function(){ $background = new Background(); + echo "saveTopAnime()"; + $background->saveTopAnime(); echo"saveEnhancementForAll()\n"; $background->saveEnhancementForAll(); })->weeklyOn(1, '15:30'); @@ -79,6 +83,16 @@ class Kernel extends ConsoleKernel })->everyMinute(); #})->twiceDaily(9, 23); */ + + /* + $schedule->call( function(){ + $background = new Background(); + #$background->checkIfIsAiring(); + $background->saveTopAnime(); + #$anime = Anime::where('mal_id', 40591)->get()->first(); + #$background->saveAiring( $anime ); + })->everyMinute()->name('saveTopAnime2')->withoutOverlapping(); + */ } /** diff --git a/app/Http/Controllers/AnimeController.php b/app/Http/Controllers/AnimeController.php index 461b583..1dd7c35 100644 --- a/app/Http/Controllers/AnimeController.php +++ b/app/Http/Controllers/AnimeController.php @@ -98,6 +98,8 @@ class AnimeController extends Controller { #$anime = $anime->setTable('anime'); $anime = $anime->where('airing_status', env('ANIME_IS_AIRING')); + $anime = $anime->orderBy('season_year', 'desc'); + $anime = $anime->orderByRaw('FIELD(season_name, "Summer", "Fall", "Winter", "Spring")'); $anime = $anime->orderBy('watching', 'desc'); $anime = $anime->orderBy('members', 'desc'); $anime = $anime->orderBy('score_today', 'desc'); @@ -105,4 +107,21 @@ class AnimeController extends Controller { return view('index_anime', ["all_anime" => $anime, "most_suprising_anime" => $most_surprising_anime]); } + + public function showSurprisingAnime() { + $anime = new Anime; + $most_surprising_anime = $anime->setTable('view_anime_index'); + $most_surprising_anime = $most_surprising_anime->simplePaginate(10); + + return view('surprising_anime', ["surprising_anime" => $most_surprising_anime]); + } + + public function showTopAnime() { + $anime = new Anime; + $anime = $anime->setTable('view_anime_index'); + $anime = $anime->orderBy('score_today', 'desc'); + $top_anime = $anime->simplePaginate(10); + + return view('top_anime', ["top_anime" => $top_anime]); + } } diff --git a/app/Libraries/Background.php b/app/Libraries/Background.php index 9c9f336..8cfe9e1 100644 --- a/app/Libraries/Background.php +++ b/app/Libraries/Background.php @@ -126,31 +126,9 @@ class Background { sleep(10); foreach($season->anime as $entry) { - if ( Anime::where('mal_id', $entry->getMalID())->exists() ) { - /** - * We already have this anime saved. - */ - echo "saveSeason: Duplicate entry: (" . $entry->getMalID() . ") Continue\n"; - continue; - } - - $anime = new Anime(); - /** - * Sleep to avoid 403 by MAL. - */ - sleep(10); - $anime->fill( $entry->getMalID() ); - - if ( "" == $anime->url ) { - echo "saveSeason: url is empty for: " . $entry->getMalId() . "\n"; - continue; - } - - if( ! DB::table('anime')->where('mal_id', $entry->getMalID() )->exists() ) { - $anime->save(); - } - sleep(10); + $this->saveAnime($entry); + sleep(10); } } @@ -245,4 +223,54 @@ class Background { sleep(1); } } + + public function saveTopAnime() { + $jikan = new MalClient; + + for ( $page = 1; $page <= 10; $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); + } + } + + } + + 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; + } + } diff --git a/app/helpers.php b/app/helpers.php index 7ac1fd6..9246362 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -66,3 +66,20 @@ function getAiringStatusCode($airing_status) { } return $status[0]->id; } + +function getCurrentSeason(){ + switch( date("m") ) { + case 1: + case 2: + case 3: return "Winter"; + case 4: + case 5: + case 6: return "Spring"; + case 7: + case 8: + case 9: return "Summer"; + case 10: + case 11: + case 12: return "Fall"; + } +} diff --git a/resources/views/index_anime.blade.php b/resources/views/index_anime.blade.php index 4fa6b72..2bb30de 100644 --- a/resources/views/index_anime.blade.php +++ b/resources/views/index_anime.blade.php @@ -3,33 +3,6 @@ @section('content')
-

Most surprising anime

-@foreach($most_suprising_anime as $anime) -
-
- - - -
-
-

{{ $anime->title_pref }}

- Score diff: {{ $anime->score_today - $anime->score_begin }} Points -
- First score: {{ $anime->score_begin }} -
- Score now: {{ $anime->score_today }} -
- - Click for more data - -
-
-@endforeach - -
-
-
-

Top anime this season

@foreach($all_anime as $anime)
diff --git a/resources/views/surprising_anime.blade.php b/resources/views/surprising_anime.blade.php new file mode 100644 index 0000000..5fe0648 --- /dev/null +++ b/resources/views/surprising_anime.blade.php @@ -0,0 +1,31 @@ +@extends('layouts.app') + +@section('content') +
+ +

Most surprising anime

+@foreach($surprising_anime as $anime) +
+
+ + + +
+
+

{{ $anime->title_pref }}

+ Score diff: {{ $anime->score_today - $anime->score_begin }} Points +
+ First score: {{ $anime->score_begin }} +
+ Score now: {{ $anime->score_today }} +
+ + Click for more data + +
+
+@endforeach + +{{ $surprising_anime ->links() }} +
+@endsection diff --git a/resources/views/top_anime.blade.php b/resources/views/top_anime.blade.php new file mode 100644 index 0000000..3ffea89 --- /dev/null +++ b/resources/views/top_anime.blade.php @@ -0,0 +1,29 @@ +@extends('layouts.app') + +@section('content') +
+ +

Top Anime

+@foreach($top_anime as $anime) +
+
+ + + +
+
+

{{ $anime->title_pref }}

+ Score: {{ $anime->score_today }} +
+ Popularity {{ $anime->score_today }} +
+ + Click for more data + +
+
+@endforeach + +{{ $top_anime->links() }} +
+@endsection diff --git a/routes/web.php b/routes/web.php index 6ab6f19..8244ce6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,6 +18,8 @@ Route::get('/save', 'IndexController@saveWatchingAnime'); Route::get('/test/getcal/{username}', 'TestController@getCalendar'); Route::get('/test/setcal/{username}', 'TestController@setCalendar'); Route::get('/api/anime/{mal_id}', 'ApiController@getAnime'); +Route::get('/anime/surprising/', 'AnimeController@showSurprisingAnime')->name('surprising_anime'); +Route::get('/anime/top/', 'AnimeController@showTopAnime')->name('top_anime'); Route::get('/anime/{mal_id}/{slug?}', 'AnimeController@showAnime')->name('anime'); Route::get('/anime/', 'AnimeController@showAllAnime')->name('anime_index'); -- cgit v1.2.3