aboutsummaryrefslogtreecommitdiff
path: root/bridges/WorldCosplayBridge.php
blob: b60d7948ac124684c969159c85900abcfaa83a23 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
class WorldCosplayBridge extends BridgeAbstract {
	const NAME = 'WorldCosplay Bridge';
	const URI = 'https://worldcosplay.net/';
	const DESCRIPTION = 'Returns WorldCosplay photos';
	const MAINTAINER = 'AxorPL';

	const API_CHARACTER = 'api/photo/list.json?character_id=%u&limit=%u';
	const API_COSPLAYER = 'api/member/photos.json?member_id=%u&limit=%u';
	const API_SERIES = 'api/photo/list.json?title_id=%u&limit=%u';
	const API_TAG = 'api/tag/photo_list.json?id=%u&limit=%u';

	const CONTENT_HTML
		= '<a href="%s" target="_blank"><img src="%s" alt="%s" title="%s"></a>';

	const ERR_CONTEXT = 'No context provided';
	const ERR_QUERY = 'Unable to query: %s';

	const LIMIT_MIN = 1;
	const LIMIT_MAX = 24;

	const PARAMETERS = array(
		'Character' => array(
			'cid' => array(
				'name' => 'Character ID',
				'type' => 'number',
				'required' => true,
				'title' => 'WorldCosplay character ID',
				'exampleValue' => 18204
			)
		),
		'Cosplayer' => array(
			'uid' => array(
				'name' => 'Cosplayer ID',
				'type' => 'number',
				'required' => true,
				'title' => 'Cosplayer\'s WorldCosplay profile ID',
				'exampleValue' => 406782
			)
		),
		'Series' => array(
			'sid' => array(
				'name' => 'Series ID',
				'type' => 'number',
				'required' => true,
				'title' => 'WorldCosplay series ID',
				'exampleValue' => 3139
			)
		),
		'Tag' => array(
			'tid' => array(
				'name' => 'Tag ID',
				'type' => 'number',
				'required' => true,
				'title' => 'WorldCosplay tag ID',
				'exampleValue' => 33643
			)
		),
		'global' => array(
			'limit' => array(
				'name' => 'Limit',
				'type' => 'number',
				'required' => false,
				'title' => 'Maximum number of photos to return',
				'exampleValue' => 5,
				'defaultValue' => 5
			)
		)
	);

	public function collectData() {
		$limit = $this->getInput('limit');
		$limit = min(self::LIMIT_MAX, max(self::LIMIT_MIN, $limit));
		switch($this->queriedContext) {
			case 'Character':
				$id = $this->getInput('cid');
				$url = self::API_CHARACTER;
				break;
			case 'Cosplayer':
				$id = $this->getInput('uid');
				$url = self::API_COSPLAYER;
				break;
			case 'Series':
				$id = $this->getInput('sid');
				$url = self::API_SERIES;
				break;
			case 'Tag':
				$id = $this->getInput('tid');
				$url = self::API_TAG;
				break;
			default:
				returnClientError(self::ERR_CONTEXT);
		}
		$url = self::URI . sprintf($url, $id, $limit);

		$json = json_decode(getContents($url));
		if($json->has_error) {
			returnServerError($json->message);
		}
		$list = $json->list;

		foreach($list as $img) {
			$image = isset($img->photo) ? $img->photo : $img;
			$item = array(
				'uri' => self::URI . substr($image->url, 1),
				'title' => $image->subject,
				'timestamp' => $image->created_at,
				'author' => $img->member->global_name,
				'enclosures' => array($image->large_url),
				'uid' => $image->id,
			);
			$item['content'] = sprintf(
				self::CONTENT_HTML,
				$item['uri'],
				$item['enclosures'][0],
				$item['title'],
				$item['title']
			);
			$this->items[] = $item;
		}
	}

	public function getName() {
		switch($this->queriedContext) {
			case 'Character':
				$id = $this->getInput('cid');
				break;
			case 'Cosplayer':
				$id = $this->getInput('uid');
				break;
			case 'Series':
				$id = $this->getInput('sid');
				break;
			case 'Tag':
				$id = $this->getInput('tid');
				break;
			default:
				return parent::getName();
		}
		return sprintf('%s %u - ', $this->queriedContext, $id) . self::NAME;
	}
}