summaryrefslogtreecommitdiff
path: root/bridges/AmazonPriceTrackerBridge.php
blob: e31a03bb45de04895239ddf07eb640916071c491 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php

class AmazonPriceTrackerBridge extends BridgeAbstract {
	const MAINTAINER = 'captn3m0';
	const NAME = 'Amazon Price Tracker';
	const URI = 'https://www.amazon.com/';
	const CACHE_TIMEOUT = 3600; // 1h
	const DESCRIPTION = 'Tracks price for a single product on Amazon';

	const PARAMETERS = array(
		array(
		'asin' => array(
			'name' 			=> 'ASIN',
			'required' 		=> true,
			'exampleValue' 	=> 'B071GB1VMQ',
			// https://stackoverflow.com/a/12827734
			'pattern'		=> 'B[\dA-Z]{9}|\d{9}(X|\d)',
		),
		'tld' => array(
			'name' => 'Country',
			'type' => 'list',
			'required' => true,
			'values' => array(
				'Australia' 	=> 'com.au',
				'Brazil' 		=> 'com.br',
				'Canada' 		=> 'ca',
				'China' 		=> 'cn',
				'France' 		=> 'fr',
				'Germany' 		=> 'de',
				'India' 		=> 'in',
				'Italy' 		=> 'it',
				'Japan' 		=> 'co.jp',
				'Mexico' 		=> 'com.mx',
				'Netherlands'	=> 'nl',
				'Spain' 		=> 'es',
				'United Kingdom'	=> 'co.uk',
				'United States'		=> 'com',
			),
			'defaultValue' => 'com',
		),
	));

	protected $title;

	/**
	 * Generates domain name given a amazon TLD
	 */
	private function getDomainName() {
		return 'https://www.amazon.' . $this->getInput('tld');
	}

	/**
	 * Generates URI for a Amazon product page
	 */
	public function getURI() {
		if (!is_null($this->getInput('asin'))) {
			return $this->getDomainName() . '/dp/' . $this->getInput('asin') . '/';
		}
		return parent::getURI();
	}

	/**
	 * Scrapes the product title from the html page
	 * returns the default title if scraping fails
	 */
	private function getTitle($html) {
		$titleTag = $html->find('#productTitle', 0);

		if (!$titleTag) {
			return $this->getDefaultTitle();
		} else {
			return trim(html_entity_decode($titleTag->innertext, ENT_QUOTES));
		}
	}

	/**
	 * Title used by the feed if none could be found
	 */
	private function getDefaultTitle() {
		return 'Amazon.' . $this->getInput('tld') . ': ' . $this->getInput('asin');
	}

	/**
	 * Returns name for the feed
	 * Uses title (already scraped) if it has one
	 */
	public function getName() {
		if (isset($this->title)) {
			return $this->title;
		} else {
			return parent::getName();
		}
	}

	private function parseDynamicImage($attribute) {
		$json = json_decode(html_entity_decode($attribute), true);

		if ($json and count($json) > 0) {
			return array_keys($json)[0];
		}
	}

	/**
	 * Returns a generated image tag for the product
	 */
	private function getImage($html) {
		$imageSrc = $html->find('#main-image-container img', 0);

		if ($imageSrc) {
			$hiresImage = $imageSrc->getAttribute('data-old-hires');
			$dynamicImageAttribute = $imageSrc->getAttribute('data-a-dynamic-image');
			$image = $hiresImage ?: $this->parseDynamicImage($dynamicImageAttribute);
		}
		$image = $image ?: 'https://placekitten.com/200/300';

		return <<<EOT
<img width="300" style="max-width:300;max-height:300" src="$image" alt="{$this->title}" />
EOT;
	}

	/**
	 * Return \simple_html_dom object
	 * for the entire html of the product page
	 */
	private function getHtml() {
		$uri = $this->getURI();

		return getSimpleHTMLDOM($uri) ?: returnServerError('Could not request Amazon.');
	}

	private function scrapePriceFromMetrics($html) {
		$asinData = $html->find('#cerberus-data-metrics', 0);

		// <div id="cerberus-data-metrics" style="display: none;"
		// 	data-asin="B00WTHJ5SU" data-asin-price="14.99" data-asin-shipping="0"
		// 	data-asin-currency-code="USD" data-substitute-count="-1" ... />
		if ($asinData) {
			return [
				'price' 	=> $asinData->getAttribute('data-asin-price'),
				'currency'	=> $asinData->getAttribute('data-asin-currency-code'),
				'shipping'	=> $asinData->getAttribute('data-asin-shipping')
			];
		}

		return false;
	}

	private function scrapePriceGeneric($html) {
		$priceDiv = $html->find('span.offer-price', 0) ?: $html->find('.a-color-price', 0);

		preg_match('/^\s*([A-Z]{3}|£|\$)\s?([\d.,]+)\s*$/', $priceDiv->plaintext, $matches);

		if (count($matches) === 3) {
			return [
				'price' 	=> $matches[2],
				'currency'	=> $matches[1],
				'shipping'	=> '0'
			];
		}

		return false;
	}

	/**
	 * Scrape method for Amazon product page
	 * @return [type] [description]
	 */
	public function collectData() {
		$html = $this->getHtml();
		$this->title = $this->getTitle($html);
		$imageTag = $this->getImage($html);

		$data = $this->scrapePriceFromMetrics($html) ?: $this->scrapePriceGeneric($html);

		$item = array(
			'title' 	=> $this->title,
			'uri' 		=> $this->getURI(),
			'content' 	=> "$imageTag<br/>Price: {$data['price']} {$data['currency']}",
		);

		if ($data['shipping'] !== '0') {
			$item['content'] .= "<br>Shipping: {$data['shipping']} {$data['currency']}</br>";
		}

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