summaryrefslogtreecommitdiff
path: root/app/Anime.php
blob: 9e6ac58b58347fc958f242179c0151b949dac532 (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
<?php
/**
 * https://stackoverflow.com/questions/26863439/in-laravel-eloquent-inserts-an-empty-record-to-table
 */

namespace App;
use Illuminate\Database\Eloquent\Model;

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',
		'is_airing'
		];

	public function __construct() {
	}

	public function fill( $id ) {
		$this->mal_id = $id;

		$jikan = new Malclient;

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

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

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

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

		$this->episodes = $this->animeInfo->getEpisodes();
		$this->is_airing = $this->animeInfo->isAiring();
	}

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

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

	protected function getInfo() {
		return $this->animeInfo;
	}

	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 "Problem with Guzzle connecting to Anilist on anime: (" . $this->mal_id . ")\n";
			return "";
		}
		return $data;
	}

}