summaryrefslogtreecommitdiff
path: root/app/Libraries/AnimeSeason.php
blob: f0296af0e467628c94aec6f881203607ee9ba46a (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
<?php

namespace App\Libraries;
use Illuminate\Support\Facades\DB;

use App\Libraries\AnimeSeason;
use App\Anime;
use App\AnimeStats;

use Carbon\Carbon;

use Jikan\MyAnimeList\MalClient;
use Jikan\Helper\Constants;


class AnimeSeason {

	public $year;
	public $name;

	public $anime;

	public function __construct() {
	}

	public function save() {
		#Anime::where('mal_id', 2 );
		$jikan = new MalClient;

		$season = $jikan->getSeasonal(
		    (new \Jikan\Request\Seasonal\SeasonalRequest(
		        ))
		);

		$this->year = $season->seasonYear;
		$this->name = $season->seasonName;

		$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();
			$anime->fill( $entry->getMalID() );

			if( ! DB::table('anime')->where('mal_id', $entry->getMalID() )->exists() ) {
				$anime->save();
			}

			$animeStats = new AnimeStats( $entry->getMalID(), $this->year, $this->name );
			$this->anime[] = $animeStats;

			$animeStats->save();

			/*
			echo "<pre>";
			var_dump($animeStats);

			if ( $count == 1) {
				return;
			}
			 */
		}
	}

}