summaryrefslogtreecommitdiff
path: root/app/MALUser.php
blob: 6c62e30787cceb804664355d1c6202b668b64072 (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
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Jikan\MyAnimeList\MalClient;

class MALUser extends Model
{
	protected $table = 'malusers';
	protected $fillable = 'username';

	public function __construct() {
	}

	public function set( $username ) {
		$this->username = $username;
	}

	public function get() {
		return $this->username;
	}

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

	public function calendar() {
		return $this->belongsTo('App\Calendar', 'id', 'user_id');
	}

	public function getIsWatching() {

		$jikan = new MalClient;

		$animeList = $jikan->getUserAnimelist(
			new \Jikan\Request\User\UserAnimeListRequest( $this->username, 1, 1 )
		);

		$anime = array();

		foreach ( $animeList as $entry ) {
			// currently watching
			if ( 1 != $entry->getAiringStatus() ){
				continue;
			}

			$anime[] = array(
				"user_id" => $this->id,
				"mal_id" => $entry->getMalID(),
				"episodes_watched" => $entry->getWatchedEpisodes(),
				"score_user" => $entry->getScore(),
			);
		}

		return $anime;
	}

	public function createCalendar() {
	}

	public function getCalendar() {
		$vCalendar = new Calendar('animes.iamfabulous.de');
		$vCalendar->setName('Anime Schedule');

		foreach ( $this->animeSchedule as $anime ) {
			$vEvent = new Event();

			$vEvent
				->setDtStart(new \DateTime($anime->airing_at))
                                ->setDtEnd( new \DateTime($anime->airing_at->add($anime->duration, 'minutes')) )
                                ->setNoTime(false)
                                ->setSummary( $anime->title_pref . " (" . $anime->episode . "/" . $anime->episodes_complete . ")" )
                                ->setUrl( env("APP_URL") )
                                ->setDescription( "(Episode " . $anime->episode . "/" . $anime->episodes_complete . ") You have watched " . $anime->episodes_watched . " episodes of " . $anime->title_pref . " and scored it with " . $anime->score_user . ". (Public score: ". $anime->score .")\n\n" . $anime->url );

			$vCalendar->addComponent($vEvent);
		}

		return $vCalendar->render();
	}

	public function test() {
		echo "<pre>";
		var_dump( $this->getIsWatching() );
	}
}