Skip to content

Commit 2137d7e

Browse files
0xCAB0Copilot
andauthored
Fix/Auth and API Changes (#147)
* Added new Vinted hosts * Improved tests * Fixed Euro method * Fixed auth * Fixed model changes * Update src/tests/queries.rs Handling better CookieErrors Co-authored-by: Copilot <[email protected]> * Fix clippy * Fix copilot mess * CookiesError should still be unreacheable --------- Co-authored-by: Copilot <[email protected]>
1 parent adf6717 commit 2137d7e

File tree

4 files changed

+99
-28
lines changed

4 files changed

+99
-28
lines changed

examples/filter_example/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async fn main() {
3636
POSTGRES_DB.clone()
3737
);
3838

39-
let db = DbController::new(&db_uri, 5, NoTls)
39+
let db = DbController::new(db_uri, 5, NoTls)
4040
.await
4141
.expect("Broken connection to Database, please set it up correctly");
4242

src/model/item.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,31 @@ pub struct Item {
1313
pub title: String,
1414
pub size_title: String,
1515
pub brand_title: String,
16-
pub currency: String,
17-
pub price: String,
16+
pub price: Price,
1817
pub photo: Option<Photo>,
1918
pub url: String,
2019
pub is_visible: bool,
2120
pub promoted: bool,
2221
pub favourite_count: i32,
2322
}
2423

24+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Hash, Eq)]
25+
pub struct Price {
26+
pub amount: String,
27+
pub currency_code: String,
28+
}
29+
2530
impl fmt::Display for Item {
2631
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2732
writeln!(f, "ID: {}", self.id)?;
2833
writeln!(f, "Title: {}", self.title)?;
2934
writeln!(f, "Size Title: {}", self.size_title)?;
3035
writeln!(f, "Brand Title: {}", self.brand_title)?;
31-
writeln!(f, "Currency: {}", self.currency)?;
32-
writeln!(f, "Price: {}", self.price)?;
36+
writeln!(
37+
f,
38+
"Price: {} {}",
39+
self.price.amount, self.price.currency_code
40+
)?;
3341
if let Some(ph) = &self.photo {
3442
writeln!(f, "Photo: {}", ph)?;
3543
}

src/queries.rs

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,17 @@ impl From<VintedWrapperError> for FangError {
108108
}
109109
}
110110

111-
const DOMAINS: [&str; 17] = [
111+
const DOMAINS: [&str; 22] = [
112112
"fr", "es", "lu", "nl", "lt", "de", "at", "it", "co.uk", "pt", "com", "cz", "sk", "pl", "se",
113-
"ro", "hu",
113+
"ro", "hu", "fi", "gr", "ie", "hr", "dk",
114114
//"be",
115115
];
116116

117-
const DEFAULT_USER_AGENT: &str = "PostmanRuntime/7.32.3";
117+
const DEFAULT_USER_AGENT: &str = "*/*";
118118

119119
#[derive(Debug, Clone)]
120120
pub enum Host {
121121
Fr,
122-
//Be,
123122
Es,
124123
Lu,
125124
Nl,
@@ -136,14 +135,31 @@ pub enum Host {
136135
Se,
137136
Ro,
138137
Hu,
138+
Fi,
139+
Gr,
140+
Ie,
141+
Hr,
142+
Dk,
143+
//Be,
139144
}
140145

141146
impl Host {
142147
/// Returns true if a Host has the Euro as the currency
143148
pub fn is_euro_host(&self) -> bool {
144-
!matches!(
149+
matches!(
145150
self,
146-
Host::Com | Host::Uk | Host::Cz | Host::Pl | Host::Se | Host::Ro | Host::Hu
151+
Host::Es
152+
| Host::It
153+
| Host::Fr
154+
| Host::Pt
155+
| Host::Fi
156+
| Host::Gr
157+
| Host::Ie
158+
| Host::Lu
159+
| Host::Nl
160+
| Host::At
161+
| Host::De
162+
| Host::Lt // | Host::Be
147163
)
148164
}
149165

@@ -181,6 +197,11 @@ impl From<&str> for Host {
181197
"se" => Host::Se,
182198
"ro" => Host::Ro,
183199
"hu" => Host::Hu,
200+
"fi" => Host::Fi,
201+
"gr" => Host::Gr,
202+
"ie" => Host::Ie,
203+
"hr" => Host::Hr,
204+
"dk" => Host::Dk,
184205
//"be" => Host::Be,
185206
_ => panic!("Not valid host"),
186207
}
@@ -207,7 +228,12 @@ impl From<Host> for &str {
207228
Host::Se => DOMAINS[14],
208229
Host::Ro => DOMAINS[15],
209230
Host::Hu => DOMAINS[16],
210-
//Host::Be => DOMAINS[17],
231+
Host::Fi => DOMAINS[17],
232+
Host::Gr => DOMAINS[18],
233+
Host::Ie => DOMAINS[19],
234+
Host::Hr => DOMAINS[20],
235+
Host::Dk => DOMAINS[21],
236+
//Host::Be => DOMAINS[22],
211237
}
212238
}
213239
}
@@ -302,6 +328,12 @@ impl VintedWrappers<'_> {
302328
Host::Se,
303329
Host::Ro,
304330
Host::Hu,
331+
Host::Fi,
332+
Host::Gr,
333+
Host::Ie,
334+
Host::Hr,
335+
Host::Dk,
336+
//Host::Be,
305337
];
306338
VintedWrappers::new_with_hosts(hosts)
307339
}
@@ -471,6 +503,37 @@ impl VintedWrapper<'_> {
471503
self.host = host_str;
472504
}
473505
}
506+
507+
pub async fn get_cookies(
508+
&self,
509+
user_agent: Option<&str>,
510+
proxy: Option<Proxy>,
511+
) -> Result<(), CookieError> {
512+
let client = get_client(user_agent, proxy).await;
513+
514+
let request = format!("https://www.vinted.{}/", self.host);
515+
516+
let mut response_cookies = client.get(&request).send().await?;
517+
let max_retries = 3;
518+
let mut i = 0;
519+
520+
while response_cookies.status() != StatusCode::OK && i < max_retries {
521+
response_cookies = client.post(&request).send().await?;
522+
// tokio::time::sleep(Duration::from_millis(100)).await;
523+
i += 1;
524+
}
525+
526+
if response_cookies.status() != StatusCode::OK {
527+
return Err(CookieError::GetCookiesError((
528+
response_cookies.status(),
529+
String::from(self.get_host()),
530+
user_agent.unwrap_or(DEFAULT_USER_AGENT).to_string(),
531+
)));
532+
}
533+
534+
Ok(())
535+
}
536+
474537
/// Refreshes the cookies for the Vinted API.
475538
///
476539
/// The `refresh_cookies` method clears the existing cookies, sends a request to refresh the cookies from the Vinted API, and retrieves the updated cookies.
@@ -607,7 +670,7 @@ impl VintedWrapper<'_> {
607670
"[{}] POST_GET_COOKIES -> Get {} items @ {}",
608671
self.id, num, self.host
609672
);
610-
self.refresh_cookies(user_agent, proxy_cookies).await?;
673+
self.get_cookies(user_agent, proxy_cookies).await?;
611674
}
612675

613676
let mut first = true;
@@ -780,7 +843,7 @@ impl VintedWrapper<'_> {
780843
"[{}] POST_GET_COOKIES -> Get item {} @ {}",
781844
self.id, item_id, self.host
782845
);
783-
self.refresh_cookies(user_agent, proxy_cookies).await?;
846+
self.get_cookies(user_agent, proxy_cookies).await?;
784847
}
785848

786849
let url = format!("https://www.vinted.{}/api/v2/items/{}", self.host, item_id);

src/tests/queries.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async fn test_get_item_query_text() {
4646
match err {
4747
VintedWrapperError::ItemNumberError => unreachable!(),
4848
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
49-
VintedWrapperError::CookiesError(_) => (),
49+
VintedWrapperError::CookiesError(_) => unreachable!(),
5050
VintedWrapperError::SerdeError(_) => unreachable!(),
5151
VintedWrapperError::ReqWestError(_) => unreachable!(),
5252
}
@@ -78,7 +78,7 @@ async fn test_get_item_brands() {
7878
match err {
7979
VintedWrapperError::ItemNumberError => unreachable!(),
8080
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
81-
VintedWrapperError::CookiesError(_) => (),
81+
VintedWrapperError::CookiesError(_) => unreachable!(),
8282
VintedWrapperError::SerdeError(_) => unreachable!(),
8383
VintedWrapperError::ReqWestError(_) => unreachable!(),
8484
}
@@ -107,7 +107,7 @@ async fn test_get_items_brands() {
107107
match err {
108108
VintedWrapperError::ItemNumberError => unreachable!(),
109109
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
110-
VintedWrapperError::CookiesError(_) => (),
110+
VintedWrapperError::CookiesError(_) => unreachable!(),
111111
VintedWrapperError::SerdeError(_) => unreachable!(),
112112
VintedWrapperError::ReqWestError(_) => unreachable!(),
113113
}
@@ -147,7 +147,7 @@ async fn test_get_items_catalogs_no_db() {
147147
match err {
148148
VintedWrapperError::ItemNumberError => unreachable!(),
149149
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
150-
VintedWrapperError::CookiesError(_) => (),
150+
VintedWrapperError::CookiesError(_) => unreachable!(),
151151
VintedWrapperError::SerdeError(_) => unreachable!(),
152152
VintedWrapperError::ReqWestError(_) => unreachable!(),
153153
}
@@ -170,7 +170,7 @@ async fn test_get_items_by_price() {
170170
Ok(items) => {
171171
assert!(items.items.len() <= 10);
172172
let ok: bool = items.items.iter().all(|item| {
173-
let price: f32 = item.price.parse().unwrap();
173+
let price: f32 = item.price.amount.parse().unwrap();
174174
price <= max && price >= min
175175
});
176176

@@ -181,7 +181,7 @@ async fn test_get_items_by_price() {
181181
match err {
182182
VintedWrapperError::ItemNumberError => unreachable!(),
183183
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
184-
VintedWrapperError::CookiesError(_) => (),
184+
VintedWrapperError::CookiesError(_) => unreachable!(),
185185
VintedWrapperError::SerdeError(_) => unreachable!(),
186186
VintedWrapperError::ReqWestError(_) => unreachable!(),
187187
}
@@ -209,7 +209,7 @@ async fn test_get_items_by_size_no_db() {
209209
match err {
210210
VintedWrapperError::ItemNumberError => unreachable!(),
211211
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
212-
VintedWrapperError::CookiesError(_) => (),
212+
VintedWrapperError::CookiesError(_) => unreachable!(),
213213
VintedWrapperError::SerdeError(_) => unreachable!(),
214214
VintedWrapperError::ReqWestError(_) => unreachable!(),
215215
}
@@ -249,7 +249,7 @@ async fn test_get_items_by_size() {
249249
match err {
250250
VintedWrapperError::ItemNumberError => unreachable!(),
251251
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
252-
VintedWrapperError::CookiesError(_) => (),
252+
VintedWrapperError::CookiesError(_) => unreachable!(),
253253
VintedWrapperError::SerdeError(_) => unreachable!(),
254254
VintedWrapperError::ReqWestError(_) => unreachable!(),
255255
}
@@ -277,7 +277,7 @@ async fn test_get_items_by_material() {
277277
match err {
278278
VintedWrapperError::ItemNumberError => unreachable!(),
279279
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
280-
VintedWrapperError::CookiesError(_) => (),
280+
VintedWrapperError::CookiesError(_) => unreachable!(),
281281
VintedWrapperError::SerdeError(_) => unreachable!(),
282282
VintedWrapperError::ReqWestError(_) => unreachable!(),
283283
}
@@ -309,7 +309,7 @@ async fn test_get_items_by_color() {
309309
match err {
310310
VintedWrapperError::ItemNumberError => unreachable!(),
311311
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
312-
VintedWrapperError::CookiesError(_) => (),
312+
VintedWrapperError::CookiesError(_) => unreachable!(),
313313
VintedWrapperError::SerdeError(_) => unreachable!(),
314314
VintedWrapperError::ReqWestError(_) => unreachable!(),
315315
}
@@ -333,7 +333,7 @@ async fn test_get_items_by_currency() {
333333
assert!(items.items.len() <= num);
334334
let ok: bool = items.items.iter().all(|item| {
335335
let c: &str = Currency::CZK.into();
336-
item.currency == c
336+
item.price.currency_code == c
337337
});
338338

339339
assert!(ok);
@@ -343,7 +343,7 @@ async fn test_get_items_by_currency() {
343343
match err {
344344
VintedWrapperError::ItemNumberError => unreachable!(),
345345
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
346-
VintedWrapperError::CookiesError(_) => (),
346+
VintedWrapperError::CookiesError(_) => unreachable!(),
347347
VintedWrapperError::SerdeError(_) => unreachable!(),
348348
VintedWrapperError::ReqWestError(_) => unreachable!(),
349349
}
@@ -383,7 +383,7 @@ async fn test_get_advanced_items() {
383383
match err {
384384
VintedWrapperError::ItemNumberError => unreachable!(),
385385
VintedWrapperError::ItemError(_, _, _) => (),
386-
VintedWrapperError::CookiesError(_) => (),
386+
VintedWrapperError::CookiesError(_) => unreachable!(),
387387
VintedWrapperError::SerdeError(_) => unreachable!(),
388388
VintedWrapperError::ReqWestError(_) => unreachable!(),
389389
}
@@ -397,7 +397,7 @@ async fn test_get_advanced_items() {
397397
match err {
398398
VintedWrapperError::ItemNumberError => unreachable!(),
399399
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
400-
VintedWrapperError::CookiesError(_) => (),
400+
VintedWrapperError::CookiesError(_) => unreachable!(),
401401
VintedWrapperError::SerdeError(_) => unreachable!(),
402402
VintedWrapperError::ReqWestError(_) => unreachable!(),
403403
}

0 commit comments

Comments
 (0)