summaryrefslogtreecommitdiff
path: root/bridges/LeBonCoinBridge.php
blob: 927b43e0d2d62dbbc905c18d3d963c455ab1720f (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
class LeBonCoinBridge extends BridgeAbstract {

	const MAINTAINER = 'jacknumber';
	const NAME = 'LeBonCoin';
	const URI = 'https://www.leboncoin.fr/';
	const DESCRIPTION = 'Returns most recent results from LeBonCoin';

	const PARAMETERS = array(
		array(
			'k' => array('name' => 'Mot Clé'),
			'r' => array(
				'name' => 'Région',
				'type' => 'list',
				'values' => array(
					'Toute la France' => '',
					'Alsace' => '1',
					'Aquitaine' => '2',
					'Auvergne' => '3',
					'Basse Normandie' => '4',
					'Bourgogne' => '5',
					'Bretagne' => '6',
					'Centre' => '7',
					'Champagne Ardenne' => '8',
					'Corse' => '9',
					'Franche Comté' => '10',
					'Haute Normandie' => '11',
					'Ile de France' => '12',
					'Languedoc Roussillon' => '13',
					'Limousin' => '14',
					'Lorraine' => '15',
					'Midi Pyrénées' => '16',
					'Nord Pas De Calais' => '17',
					'Pays de la Loire' => '18',
					'Picardie' => '19',
					'Poitou Charentes' => '20',
					'Provence Alpes Côte d\'Azur' => '21',
					'Rhône-Alpes' => '22',
					'Guadeloupe' => '23',
					'Martinique' => '24',
					'Guyane' => '25',
					'Réunion' => '26'
				)
			),
			'c' => array(
				'name' => 'Catégorie',
				'type' => 'list',
				'values' => array(
					'Toutes catégories' => '',
					'EMPLOI' => array(
						'Emploi et recrutement' => '71',
						'Offres d\'emploi et jobs' => '33'
					),
					'VEHICULES' => array(
						'Tous' => '1',
						'Voitures' => '2',
						'Motos' => '3',
						'Caravaning' => '4',
						'Utilitaires' => '5',
						'Equipement Auto' => '6',
						'Equipement Moto' => '44',
						'Equipement Caravaning' => '50',
						'Nautisme' => '7',
						'Equipement Nautisme' => '51'
					),
					'IMMOBILIER' => array(
						'Tous' => '8',
						'Ventes immobilières' => '9',
						'Locations' => '10',
						'Colocations' => '11',
						'Bureaux & Commerces' => '13'
					),
					'VACANCES' => array(
						'Tous' => '66',
						'Locations & Gîtes' => '12',
						'Chambres d\'hôtes' => '67',
						'Campings' => '68',
						'Hôtels' => '69',
						'Hébergements insolites' => '70'
					),
					'MULTIMEDIA' => array(
						'Tous' => '14',
						'Informatique' => '15',
						'Consoles & Jeux vidéo' => '43',
						'Image & Son' => '16',
						'Téléphonie' => '17'
					),
					'LOISIRS' => array(
						'Tous' => '24',
						'DVD / Films' => '25',
						'CD / Musique' => '26',
						'Livres' => '27',
						'Animaux' => '28',
						'Vélos' => '55',
						'Sports & Hobbies' => '29',
						'Instruments de musique' => '30',
						'Collection' => '40',
						'Jeux & Jouets' => '41',
						'Vins & Gastronomie' => '48'
					),
					'MATERIEL PROFESSIONNEL' => array(
						'Tous' => '56',
						'Matériel Agricole' => '57',
						'Transport - Manutention' => '58',
						'BTP - Chantier Gros-oeuvre' => '59',
						'Outillage - Matériaux 2nd-oeuvre' => '60',
						'Équipements Industriels' => '32',
						'Restauration - Hôtellerie' => '61',
						'Fournitures de Bureau' => '62',
						'Commerces & Marchés' => '63',
						'Matériel Médical' => '64'
					),
					'SERVICES' => array(
						'Tous' => '31',
						'Prestations de services' => '34',
						'Billetterie' => '35',
						'Evénements' => '49',
						'Cours particuliers' => '36',
						'Covoiturage' => '65'
					),
					'MAISON' => array(
						'Tous' => '18',
						'Ameublement' => '19',
						'Electroménager' => '20',
						'Arts de la table' => '45',
						'Décoration' => '39',
						'Linge de maison' => '46',
						'Bricolage' => '21',
						'Jardinage' => '52',
						'Vêtements' => '22',
						'Chaussures' => '53',
						'Accessoires & Bagagerie' => '47',
						'Montres & Bijoux' => '42',
						'Equipement bébé' => '23',
						'Vêtements bébé' => '54',
					),
					'AUTRES' => '37'
				)
			),
			'o' => array(
				'name' => 'Vendeur',
				'type' => 'list',
				'values' => array(
					'Tous' => '',
					'Particuliers' => 'private',
					'Professionnels' => 'pro',
				)
			)
		)
	);

	public function collectData(){

		$params = array(
			'text' => $this->getInput('k'),
			'region' => $this->getInput('r'),
			'category' => $this->getInput('c'),
			'owner_type' => $this->getInput('o'),
		);

		$url = self::URI . 'recherche/?' . http_build_query($params);
		$html = getContents($url)
			or returnServerError('Could not request LeBonCoin. Tried: ' . $url);

		if(!preg_match('/^<script>window.FLUX_STATE[^\r\n]*/m', $html, $matches)) {
			returnServerError('Could not parse JSON in page content.');
		}

		$clean_match = str_replace(
			array('</script>', '<script>window.FLUX_STATE = '),
			array('', ''),
			$matches[0]
		);
		$json = json_decode($clean_match);

		if($json->adSearch->data->total === 0) {
			return;
		}

		foreach($json->adSearch->data->ads as $element) {

			$item['title'] = $element->subject;
			$item['content'] = $element->body;
			$item['date'] = $element->index_date;
			$item['timestamp'] = strtotime($element->index_date);
			$item['uri'] = $element->url;
			$item['ad_type'] = $element->ad_type;
			$item['author'] = $element->owner->name;

			if(isset($element->location->city)) {

				$item['city'] = $element->location->city;
				$item['content'] .= ' -- ' . $element->location->city;

			}

			if(isset($element->location->zipcode)) {
				$item['zipcode'] = $element->location->zipcode;
			}

			if(isset($element->price)) {

				$item['price'] = $element->price[0];
				$item['content'] .= ' -- ' . current($element->price) . '€';

			}

			if(isset($element->images->urls)) {

				$item['thumbnail'] = $element->images->thumb_url;
				$item['enclosures'] = array();

				foreach($element->images->urls as $image) {
					$item['enclosures'][] = $image;
				}

			}

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