-
Notifications
You must be signed in to change notification settings - Fork 1
/
carzone_ie.rs
192 lines (170 loc) · 4.99 KB
/
carzone_ie.rs
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
#![allow(non_snake_case)]
use serde::{Deserialize, Serialize};
use std::io::{Error, ErrorKind};
use super::{SearchResult, Searcher};
use crate::{
hit::{Hit, Mileage, Price},
query::Query,
};
const API_ROOT: &str = "https://www.carzone.ie/rest/1.0/Car/stock";
#[derive(Serialize)]
struct CarzoneQueryParams {
#[serde(skip_serializing_if = "Option::is_none")]
make: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
model: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
minPrice: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
maxPrice: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
minYear: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
maxYear: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
minMileage: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
maxMileage: Option<String>,
showPoa: String,
page: String,
size: String,
}
#[derive(Deserialize)]
struct PriceDetail {
gbpPrice: Option<i32>,
euroPrice: Option<i32>,
}
#[derive(Deserialize)]
struct VehicleMileage {
mileageKm: i32,
}
#[derive(Deserialize)]
struct Vehicle {
mileage: VehicleMileage,
registrationYear: u16,
}
#[derive(Deserialize)]
struct MakeModel {
cleanMake: String,
cleanModel: String,
}
#[derive(Deserialize)]
struct SearchDetailSummary {
mmv: MakeModel,
}
#[derive(Deserialize)]
struct Summary {
publicReference: String,
priceDetail: PriceDetail,
vehicle: Vehicle,
searchDetailSummary: SearchDetailSummary,
}
#[derive(Deserialize)]
struct CarzoneAd {
summary: Summary,
}
#[derive(Deserialize)]
struct CarzoneResult {
items: Vec<CarzoneAd>,
}
#[derive(Deserialize)]
struct CarzoneResponse {
totalPages: u16,
results: Vec<CarzoneResult>,
}
pub struct CarZoneIE {}
fn params_from_query(query: &Query, page: u16) -> CarzoneQueryParams {
CarzoneQueryParams {
make: query.make.clone(),
model: query.model.clone(),
minPrice: query.min_price.clone(),
maxPrice: query.max_price.clone(),
minYear: query.min_year.clone(),
maxYear: query.max_year.clone(),
minMileage: query.min_kms.clone(),
maxMileage: query.max_kms.clone(),
showPoa: "false".to_string(),
page: page.to_string(),
size: "30".to_string(),
}
}
#[async_recursion::async_recursion]
async fn recursive_fetch(
client: &reqwest::Client,
query: &Query,
page: u16,
mut collected: Vec<CarzoneAd>,
) -> Result<Vec<CarzoneAd>, Error> {
let params = params_from_query(query, page);
let res = client
.get(API_ROOT)
.query(¶ms)
.send()
.await
.map_err(|error| Error::new(ErrorKind::Other, error))?
.json::<CarzoneResponse>()
.await
.map_err(|error| Error::new(ErrorKind::Other, error))?;
let mut current_ads = res.results.into_iter().flat_map(|r| r.items).collect();
collected.append(&mut current_ads);
if page < res.totalPages {
Ok(recursive_fetch(client, query, page + 1, collected).await?)
} else {
Ok(collected)
}
}
#[async_trait::async_trait]
impl Searcher for CarZoneIE {
async fn search(&self, query: &Query) -> SearchResult {
let client = reqwest::Client::new();
let ads = recursive_fetch(&client, query, 1, vec![]).await?;
let mapped = ads
.iter()
// Note: Carzone places premium ads of different makes in the search returns.
// Let's make sure we filter those out.
.filter(|x| {
query.make.is_none()
|| x.summary.searchDetailSummary.mmv.cleanMake.to_lowercase()
== query.make.as_ref().unwrap().to_lowercase()
})
.map(Hit::from)
.collect();
Ok(mapped)
}
}
impl From<&CarzoneAd> for Price {
fn from(ad: &CarzoneAd) -> Self {
ad.summary
.priceDetail
.euroPrice
.map(Price::Eur)
.or_else(|| ad.summary.priceDetail.gbpPrice.map(Price::Gbp))
.unwrap_or(Price::Unknown)
}
}
impl From<&CarzoneAd> for Mileage {
fn from(ad: &CarzoneAd) -> Self {
Mileage::Km(ad.summary.vehicle.mileage.mileageKm)
}
}
impl From<&CarzoneAd> for Hit {
fn from(ad: &CarzoneAd) -> Self {
let make = ad.summary.searchDetailSummary.mmv.cleanMake.clone();
let model = ad.summary.searchDetailSummary.mmv.cleanModel.clone();
let url = format!(
"https://www.carzone.ie/used-cars/{}/{}/fpa/{}",
urlencoding::encode(&make),
urlencoding::encode(&model),
ad.summary.publicReference
);
Hit {
mileage: ad.into(),
year: ad.summary.vehicle.registrationYear,
search_engine: "carzone.ie".to_string(),
make,
model,
price: ad.into(),
url,
}
}
}