summaryrefslogtreecommitdiffstats
path: root/backend/api.php
blob: 25457073ec24269df6173bb7c8dd9fb4103c118e (plain) (blame)
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
<?php
//1.0 API, which is now outdated.
//As soon as API is stabilized, this should be updated
class AnyClipError {
	public $code;
	public $message;
	public function __construct($xml) {
		ereg('<code>(.*)</code><description>(.*)</description>', $xml, $regs);
		$this->code = intval($regs[1]);
		$this->message = $regs[2];
	}
}
class AnyClip {
	private $token;
	private $tokenPath;
	private $apikey;
	public function __construct($apikey) {
		$this->apikey = $apikey;
		$this->tokenPath = md5($apikey).".token";
		$this->token = @file_get_contents($this->tokenPath);
		if ($this->token == "")
			$this->token = false;
	}
	private function request() {
		$args = func_get_args();
		if ($args[0] == "authenticate")
			$url = "http://api.anyclip.com/ac_auth/v1/json/authenticate/$args[1]/";
		elseif (func_num_args() >= 1) {
			if ($this->token === false)
				$this->authenticate();
			$url = "http://api.anyclip.com/ac_api/v1/{$this->token}/json/$args[0]/";
			for ($i = 1; $i < func_num_args(); $i++)
				$url .= urlencode(utf8_encode($args[$i]))."/"; //The API says to encode arguments as utf-8...
		}
		else return null;
		$response = @file_get_contents($url);
		if($response == "" || substr($response, 0, 6) == "<error") {
			$error = new AnyClipError($response);
			if ($error->code == 1001) {
				$this->authenticate();
				return call_user_func_array(array(&$this, 'request'), $args);
			}
			return $error;
		}
		return json_decode($response);
	}
	private function authenticate() {
		$this->token = $this->request("authenticate", $this->apikey)->Token;
		@file_put_contents($this->tokenPath, $this->token);
		if ($this->token == "")
			$this->token = false;
	}
	public function tagTypes($start = 0, $max = 20) {
		return $this->request("tagtypes", $start, $max);
	}
	public function tags($start = 0, $max = 20) {
		return $this->request("tags", $start, $max);
	}
	public function studios() {
		return $this->request("studios");
	}
	public function genres() {
		return $this->request("genres");
	}
	public function castTypes() {
		return $this->request("castTypes");
	}
	public function titleTypes() {
		return $this->request("titleTypes");
	}
	public function clipsForCast($castCode, $start = 0, $max = 20) {
		return $this->request("cast", $castCode, "clips", $start, $max);
	}
	public function castForTitle($titleCode, $start = 0, $max = 20) {
		return $this->request("title", $titleCode, "cast", $start, $max);
	}
	public function titlesForCast($castCode, $start = 0, $max = 20) {
		return $this->request("cast", $castCode, "titles", $start, $max);
	}
	public function tagsForClip($clipCode, $start = 0, $max = 20) {
		return $this->request("clip", $clipCode, "tags", $start, $max);
	}
	public function clipsForTagType($tagType, $start = 0, $max = 20) {
		return $this->request("clips", "tagType", $tagType, $start, $max);
	}
	public function clipsForTag($tag, $start = 0, $max = 20) {
		return $this->request("clips", "tag", $tag, $start, $max);
	}
	public function titlesForTitleType($titleTypeID, $start = 0, $max = 20) {
		return $this->request("titles", "titleType", $titleTypeID, $start, $max);
	}
	public function titles($start = 0, $max = 20) {
		return $this->request("titles", $start, $max);
	}
	public function title($titleCode) {
		return $this->request("title", $titleCode);
	}
	public function titlesStartWith($startsWith, $start = 0, $max = 20) {
		return $this->request("titles", "name", $start, $max);
	}
	public function titlesForGenre($genreID, $start = 0, $max = 20) {
		return $this->request("titles", "genre", $genreID, $start, $max);
	}
	public function clip($clipCode) {
		return $this->request("clip", $clipCode);
	}
	public function clipsForTitle($titleCode, $start = 0, $max = 20) {
		return $this->request("title", $titleCode, "clips", $start, $max);
	}
	public function clipsForTitleType($titleTypeID, $start = 0, $max = 20) {
		return $this->request("titles", $titleTypeID, "clips", $start, $max);
	}
	public function searchTitles($titleQuery, $start = 0, $max = 20) {
		return $this->request("titles", "search", $titleQuery, $start, $max);
	}
	public function searchCasts($castQuery, $start = 0, $max = 20) {
		return $this->request("cast", "search", $castQuery, $start, $max);
	}
	public function searchClips($query, $start = 0, $max = 20) {
		return $this->request("search", $query, $start, $max);
	}
}
?>