summaryrefslogtreecommitdiff
path: root/bridges/ArtStationBridge.php
blob: 9c12add5ed954a6f9dda8edb11a1aba9a01a39ba (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
<?php
class ArtStationBridge extends BridgeAbstract {
	const NAME = 'ArtStation';
	const URI = 'https://www.artstation.com';
	const DESCRIPTION = 'Fetches the latest ten artworks from a search query on ArtStation.';
	const MAINTAINER = 'thefranke';
	const CACHE_TIMEOUT = 3600; // 1h

	const PARAMETERS = array(
		'Search Query' => array(
			'q' => array(
				'name' => 'Search term',
				'required' => true
			)
		)
	);

	public function getIcon() {
		return 'https://www.artstation.com/assets/favicon-58653022bc38c1905ac7aa1b10bffa6b.ico';
	}

	public function getName() {
		return self::NAME . ': ' . $this->getInput('q');
	}

	private function fetchSearch($searchQuery) {
		$data = '{"query":"' . $searchQuery . '","page":1,"per_page":50,"sorting":"date",';
		$data .= '"pro_first":"1","filters":[],"additional_fields":[]}';

		$header = array(
			'Content-Type: application/json',
			'Accept: application/json'
		);

		$opts = array(
			CURLOPT_POST => true,
			CURLOPT_POSTFIELDS => $data,
			CURLOPT_RETURNTRANSFER => true
		);

		$jsonSearchURL = self::URI . '/api/v2/search/projects.json';
		$jsonSearchStr = getContents($jsonSearchURL, $header, $opts)
			or returnServerError('Could not fetch JSON for search query.');
		return json_decode($jsonSearchStr);
	}

	private function fetchProject($hashID) {
		$jsonProjectURL = self::URI . '/projects/' . $hashID . '.json';
		$jsonProjectStr = getContents($jsonProjectURL)
			or returnServerError('Could not fetch JSON for project.');
		return json_decode($jsonProjectStr);
	}

	public function collectData() {
		$searchTerm = $this->getInput('q');
		$jsonQuery = $this->fetchSearch($searchTerm);

		foreach($jsonQuery->data as $media) {
			// get detailed info about media item
			$jsonProject = $this->fetchProject($media->hash_id);

			// create item
			$item = array();
			$item['title'] = $media->title;
			$item['uri'] = $media->url;
			$item['timestamp'] = strtotime($jsonProject->published_at);
			$item['author'] = $media->user->full_name;
			$item['categories'] = implode(',', $jsonProject->tags);

			$item['content'] = '<a href="'
				. $media->url
				. '"><img style="max-width: 100%" src="'
				. $jsonProject->cover_url
				. '"></a><p>'
				. $jsonProject->description
				. '</p>';

			$numAssets = count($jsonProject->assets);

			if ($numAssets > 1)
				$item['content'] .= '<p><a href="'
					. $media->url
					. '">Project contains '
					. ($numAssets - 1)
					. ' more item(s).</a></p>';

			$this->items[] = $item;

			if (count($this->items) >= 10)
				break;
		}
	}
}