summaryrefslogtreecommitdiff
path: root/app/Anime.php
blob: ab5a3fe4089b3d61af849a9598fb24b5e0c4bac6 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
 * https://stackoverflow.com/questions/26863439/in-laravel-eloquent-inserts-an-empty-record-to-table
 */

namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

use Jikan\MyAnimeList\MalClient;

class Anime extends Model {

	/**
	 * Eloquent ORM
	 */
	#protected $primarykey = 'mal_id';
	protected $table = 'anime';
	protected $fillable = [
		'mal_id',
		'url',
		'image_url',
		'title_eng',
		'title_rom',
		'title_nat',
		'title_pref',
		'anime_type',
		'broadcasted',
		'episodes',
		'airing_status',
		'season_year',
		'season_name'
		];

	public function __construct() {
	}

	public function fillStats( $id, $skip_if_unpopular = true ) {
		$this->mal_id = $id;

		$jikan = new Malclient;

		try {
			$animeInfo = $jikan->getAnime( 
				(new \Jikan\Request\Anime\AnimeRequest( $this->mal_id ))
			);
		} catch (\Exception $e) {
			echo "Problem requesting AnimeInfo for (" . $this->mal_id . ") continue\n";
			echo $e->getMessage();
			echo "\n\n";
		}

		if ( $skip_if_unpopular && 10000 > $animeInfo->getMembers() ) {
			echo "Anime (" . $this->mal_id . ") has only " . $animeInfo->getMembers() . " Members. Not worth to keep. Skipping\n";
			return;
		}

		/*
		if ( strtolower("not yet aired") == strtolower($animeInfo->getStatus()) ) {
			echo "Anime (" . $this->mal_id . ") has not aired yet. Skipping\n";
			return;
		}
		 */

		$this->url = $animeInfo->GetUrl();
		$this->image_url = $animeInfo->getImageUrl();

		$this->title_eng = $animeInfo->getTitleEnglish();
		$this->title_rom = $animeInfo->getTitle();
		$this->title_nat = $animeInfo->getTitleJapanese();
		$this->title_pref = $animeInfo->getTitle();

		$this->anime_type = $animeInfo->getType();
		$this->broadcasted = $animeInfo->getBroadcast();

		$this->episodes = $animeInfo->getEpisodes();

		$this->airing_status = getAiringStatusCode($animeInfo->getStatus());


		if ( is_null($animeInfo->getPremiered()) ) {
			if ( env('ANIME_NOT_YET_AIRED') != $this->airing_status ) {
				$tmp_date = getSeasonFromDate( $animeInfo->getAired()->getFrom() );

				$this->season_name = $tmp_date["name"];
				$this->season_year = $tmp_date["year"];
			}
		} else {
			$this->season_name = explode(" ", $animeInfo->getPremiered())[0];
			$this->season_year = explode(" ", $animeInfo->getPremiered())[1];
		}
	}

	public function getStats() {
		return $this->hasMany('App\AnimeStats', 'mal_id', 'mal_id');
	}

	public function getAiring() {
		return $this->hasMany('App\Airing', 'mal_id', 'mal_id');
	} 

	public function user() {
		return $this->belongsToMany('App\MALUser', 'is_watching', 'mal_id', 'user_id')
			->as('anime')
			->withPivot('episodes_watched', 'score_user')
			->withTimestamps();
	}

	public function getDataFromAnilist() {
		$query = '
		query($id: Int!) {
		      Media(idMal: $id, type: ANIME) {
			title {
			  romaji
			  english
			  native
			  userPreferred
			}
			nextAiringEpisode {
			  airingAt
			  timeUntilAiring
			  episode
			}
			episodes
			duration
		      }
		    }
		';

		// Define our query variables and values that will be used in the query request
		$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 "getDataFromAnilist: Problem with Guzzle connecting to Anilist on anime: (" . $this->mal_id . ")\n";
			return "";
		}
		return $data;
	}

	public function getEnhancementFromAnilist()  {

		$query = '
		query($id: Int!) {
		      Media(idMal: $id, type: ANIME) {
			title {
			  userPreferred
			}
			description
			hashtag
			duration
		      }
		    }
		';
		$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;

			$rate_limit = $response->getHeader("X-RateLimit-Remaining")[0];
			echo "Try Block: Rate Limit: " . $rate_limit . "\n";
			Log::info("getEnhancementFromAnilist: Try Block: Rate Limit: " . $rate_limit . "\n");
			if ( $rate_limit <= 10 ) {
				echo " sleep 5 seconds\n\n";
				sleep(5);
			}
		} catch (\Exception $e) {
			echo "getEnhancementFromAnilist: Problem with Guzzle connecting to Anilist on anime: (" . $this->mal_id . ")\n";
			echo $e->getMessage();
			echo "\n";
			return "";
		}
		echo "got enhancement for anime " . $this->mal_id . "\n\n";
		Log::info("got enhancement for anime " . $this->mal_id . "\n");
		return $data;

	}
}