Spaces:
Running
Running
Upload 2 files
Browse files- src/main.rs +31 -16
src/main.rs
CHANGED
|
@@ -477,6 +477,12 @@ async fn gw_light_call(
|
|
| 477 |
Ok(json.results)
|
| 478 |
}
|
| 479 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
async fn login(Json(req): Json<LoginReq>) -> impl IntoResponse {
|
| 481 |
let email = req.email.trim().to_string();
|
| 482 |
let pass = req.password_md5.trim().to_string();
|
|
@@ -484,18 +490,18 @@ async fn login(Json(req): Json<LoginReq>) -> impl IntoResponse {
|
|
| 484 |
if email.is_empty() || pass.is_empty() {
|
| 485 |
return json_error(StatusCode::BAD_REQUEST, "Email and password_md5 required");
|
| 486 |
}
|
| 487 |
-
let jar = Jar::default();
|
| 488 |
-
let
|
| 489 |
-
|
|
|
|
| 490 |
let client = reqwest::Client::builder()
|
| 491 |
-
.cookie_provider(
|
| 492 |
.timeout(std::time::Duration::from_secs(15))
|
| 493 |
.build()
|
| 494 |
.unwrap();
|
| 495 |
|
| 496 |
-
let mut oauth_ok = false;
|
| 497 |
if let Ok(access_token) = oauth_access_token(&client, &email, &pass).await {
|
| 498 |
-
let
|
| 499 |
.get("https://api.deezer.com/platform/generic/track/80085")
|
| 500 |
.header("Authorization", format!("Bearer {}", access_token))
|
| 501 |
.header(ACCEPT, "*/*")
|
|
@@ -506,10 +512,19 @@ async fn login(Json(req): Json<LoginReq>) -> impl IntoResponse {
|
|
| 506 |
)
|
| 507 |
.send()
|
| 508 |
.await;
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
}
|
| 514 |
}
|
| 515 |
|
|
@@ -530,11 +545,8 @@ async fn login(Json(req): Json<LoginReq>) -> impl IntoResponse {
|
|
| 530 |
|
| 531 |
let arl_v = match gw_light_call(&client, "user.getArl", &token, json!({})).await {
|
| 532 |
Ok(v) => v,
|
| 533 |
-
Err(
|
| 534 |
-
|
| 535 |
-
return json_error(StatusCode::BAD_GATEWAY, e);
|
| 536 |
-
}
|
| 537 |
-
let _ = gw_light_call(
|
| 538 |
&client,
|
| 539 |
"user.checkCredentials",
|
| 540 |
&token,
|
|
@@ -545,9 +557,12 @@ async fn login(Json(req): Json<LoginReq>) -> impl IntoResponse {
|
|
| 545 |
}),
|
| 546 |
)
|
| 547 |
.await;
|
|
|
|
|
|
|
|
|
|
| 548 |
match gw_light_call(&client, "user.getArl", &token, json!({})).await {
|
| 549 |
Ok(v) => v,
|
| 550 |
-
Err(
|
| 551 |
}
|
| 552 |
}
|
| 553 |
};
|
|
|
|
| 477 |
Ok(json.results)
|
| 478 |
}
|
| 479 |
|
| 480 |
+
fn jar_has_cookie(jar: &Jar, url: &Url, cookie_prefix: &str) -> bool {
|
| 481 |
+
let Some(hv) = jar.cookies(url) else { return false };
|
| 482 |
+
let Ok(s) = hv.to_str() else { return false };
|
| 483 |
+
s.split(';').any(|p| p.trim_start().starts_with(cookie_prefix))
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
async fn login(Json(req): Json<LoginReq>) -> impl IntoResponse {
|
| 487 |
let email = req.email.trim().to_string();
|
| 488 |
let pass = req.password_md5.trim().to_string();
|
|
|
|
| 490 |
if email.is_empty() || pass.is_empty() {
|
| 491 |
return json_error(StatusCode::BAD_REQUEST, "Email and password_md5 required");
|
| 492 |
}
|
| 493 |
+
let jar = Arc::new(Jar::default());
|
| 494 |
+
let url_deezer = "https://www.deezer.com".parse::<Url>().unwrap();
|
| 495 |
+
let url_api = "https://api.deezer.com".parse::<Url>().unwrap();
|
| 496 |
+
jar.add_cookie_str("comeback=1; Domain=.deezer.com", &url_deezer);
|
| 497 |
let client = reqwest::Client::builder()
|
| 498 |
+
.cookie_provider(jar.clone())
|
| 499 |
.timeout(std::time::Duration::from_secs(15))
|
| 500 |
.build()
|
| 501 |
.unwrap();
|
| 502 |
|
|
|
|
| 503 |
if let Ok(access_token) = oauth_access_token(&client, &email, &pass).await {
|
| 504 |
+
let _ = client
|
| 505 |
.get("https://api.deezer.com/platform/generic/track/80085")
|
| 506 |
.header("Authorization", format!("Bearer {}", access_token))
|
| 507 |
.header(ACCEPT, "*/*")
|
|
|
|
| 512 |
)
|
| 513 |
.send()
|
| 514 |
.await;
|
| 515 |
+
|
| 516 |
+
if !jar_has_cookie(&jar, &url_deezer, "sid=") && !jar_has_cookie(&jar, &url_api, "sid=") {
|
| 517 |
+
let _ = client
|
| 518 |
+
.get("https://api.deezer.com/platform/generic/track/3135556")
|
| 519 |
+
.header("Authorization", format!("Bearer {}", access_token))
|
| 520 |
+
.header(ACCEPT, "*/*")
|
| 521 |
+
.header("Accept-Language", "en-US,en;q=0.9")
|
| 522 |
+
.header(
|
| 523 |
+
"User-Agent",
|
| 524 |
+
"Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0",
|
| 525 |
+
)
|
| 526 |
+
.send()
|
| 527 |
+
.await;
|
| 528 |
}
|
| 529 |
}
|
| 530 |
|
|
|
|
| 545 |
|
| 546 |
let arl_v = match gw_light_call(&client, "user.getArl", &token, json!({})).await {
|
| 547 |
Ok(v) => v,
|
| 548 |
+
Err(e1) => {
|
| 549 |
+
let cred = gw_light_call(
|
|
|
|
|
|
|
|
|
|
| 550 |
&client,
|
| 551 |
"user.checkCredentials",
|
| 552 |
&token,
|
|
|
|
| 557 |
}),
|
| 558 |
)
|
| 559 |
.await;
|
| 560 |
+
if let Err(e) = cred {
|
| 561 |
+
return json_error(StatusCode::UNAUTHORIZED, e);
|
| 562 |
+
}
|
| 563 |
match gw_light_call(&client, "user.getArl", &token, json!({})).await {
|
| 564 |
Ok(v) => v,
|
| 565 |
+
Err(e2) => return json_error(StatusCode::BAD_GATEWAY, format!("getArl:{e1}; retry:{e2}")),
|
| 566 |
}
|
| 567 |
}
|
| 568 |
};
|