summaryrefslogtreecommitdiff
path: root/app/Anime.php
diff options
context:
space:
mode:
authorhorus2020-03-05 16:02:10 +0100
committerhorus2020-03-05 16:02:10 +0100
commit5dda561d73c9a5698386d643d56a142aa4bbdeec (patch)
tree55afe2db98d0fa15fc845cc3da3afe6b517bbd3d /app/Anime.php
parent420e44e0fe4623a439e26dfd0526ee5ef606a170 (diff)
downloadsenpai-5dda561d73c9a5698386d643d56a142aa4bbdeec.tar.gz
Committing intermediate state.
Diffstat (limited to 'app/Anime.php')
-rw-r--r--app/Anime.php100
1 files changed, 77 insertions, 23 deletions
diff --git a/app/Anime.php b/app/Anime.php
index ac5c650..7971430 100644
--- a/app/Anime.php
+++ b/app/Anime.php
@@ -10,30 +10,28 @@ use Jikan\MyAnimeList\MalClient;
class Anime extends Model {
- /*
- public $id;
- public $mal_id;
-
- public $url;
- public $image_url;
-
- public $title_eng;
- public $title_rom;
- public $title_nat;
- public $title_pref;
-
- public $type;
- */
-
- private $animeInfo;
-
/**
* Eloquent ORM
*/
+ #protected $primarykey = 'mal_id';
protected $table = 'anime';
- protected $fillable = ['mal_id','url','image_url','title_eng','title_rom','title_nat','title_pref','type',];
+ protected $fillable = [
+ 'mal_id',
+ 'url',
+ 'image_url',
+ 'title_eng',
+ 'title_rom',
+ 'title_nat',
+ 'title_pref',
+ 'anime_type',
+ 'broadcasted',
+ 'episodes'
+ ];
+
+ public function __construct() {
+ }
- public function __construct( $id, $parse_info = true ) {
+ public function fill( $id, $parse_info = true ) {
$this->mal_id = $id;
if ( $parse_info ) {
@@ -51,18 +49,74 @@ class Anime extends Model {
$this->title_nat = $this->animeInfo->getTitleJapanese();
$this->title_pref = $this->animeInfo->getTitle();
- $this->type = $this->animeInfo->getType();
+ $this->anime_type = $this->animeInfo->getType();
+ $this->broadcasted = $this->animeInfo->getBroadcast();
+
+ $this->episodes = $this->animeInfo->getEpisodes();
}
}
- /*
public function getStats() {
- return $this->hasMany('App\AnimeStats');
+ 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;
+ }
+
}