summaryrefslogtreecommitdiff
path: root/bridges/CastorusBridge.php
blob: 3ed1331ecf22e01a62d6c209e474cbdb1140684f (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
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
<?php
class CastorusBridge extends BridgeAbstract {
	const MAINTAINER = 'logmanoriginal';
	const NAME = 'Castorus Bridge';
	const URI = 'http://www.castorus.com';
	const CACHE_TIMEOUT = 600; // 10min
	const DESCRIPTION = 'Returns the latest changes';

	const PARAMETERS = array(
		'Get latest changes' => array(),
		'Get latest changes via ZIP code' => array(
			'zip' => array(
				'name' => 'ZIP code',
				'type' => 'text',
				'required' => true,
				'exampleValue' => '74910, 74',
				'title' => 'Insert ZIP code (complete or partial)'
			)
		),
		'Get latest changes via city name' => array(
			'city' => array(
				'name' => 'City name',
				'type' => 'text',
				'required' => true,
				'exampleValue' => 'Seyssel, Seys',
				'title' => 'Insert city name (complete or partial)'
			)
		)
	);

	// Extracts the title from an actitiy
	private function extractActivityTitle($activity){
		$title = $activity->find('a', 0);

		if(!$title)
			returnServerError('Cannot find title!');

		return htmlspecialchars(trim($title->plaintext));
	}

	// Extracts the url from an actitiy
	private function extractActivityUrl($activity){
		$url = $activity->find('a', 0);

		if(!$url)
			returnServerError('Cannot find url!');

		return self::URI . $url->href;
	}

	// Extracts the time from an activity
	private function extractActivityTime($activity){
		// Unfortunately the time is part of the parent node,
		// so we have to clear all child nodes first
		$nodes = $activity->find('*');

		if(!$nodes)
			returnServerError('Cannot find nodes!');

		foreach($nodes as $node) {
			$node->outertext = '';
		}

		return strtotime($activity->innertext);
	}

	// Extracts the price change
	private function extractActivityPrice($activity){
		$price = $activity->find('span', 1);

		if(!$price)
			returnServerError('Cannot find price!');

		return $price->innertext;
	}

	public function collectData(){
		$zip_filter = trim($this->getInput('zip'));
		$city_filter = trim($this->getInput('city'));

		$html = getSimpleHTMLDOM(self::URI);

		if(!$html)
			returnServerError('Could not load data from ' . self::URI . '!');

		$activities = $html->find('div#activite/li');

		if(!$activities)
			returnServerError('Failed to find activities!');

		foreach($activities as $activity) {
			$item = array();

			$item['title'] = $this->extractActivityTitle($activity);
			$item['uri'] = $this->extractActivityUrl($activity);
			$item['timestamp'] = $this->extractActivityTime($activity);
			$item['content'] = '<a href="'
			. $item['uri']
			. '">'
			. $item['title']
			. '</a><br><p>'
			. $this->extractActivityPrice($activity)
			. '</p>';

			if(isset($zip_filter)
			&& !(substr($item['title'], 0, strlen($zip_filter)) === $zip_filter)) {
				continue; // Skip this item
			}

			if(isset($city_filter)
			&& !(substr($item['title'], strpos($item['title'], ' ') + 1, strlen($city_filter)) === $city_filter)) {
				continue; // Skip this item
			}

			$this->items[] = $item;
		}
	}
}