From 656aef28f356411ff25cee14214ea0e677563537 Mon Sep 17 00:00:00 2001 From: "alterwain@protonmail.com" Date: Thu, 13 Feb 2025 03:49:06 +0300 Subject: [PATCH] modified: Cargo.lock modified: Cargo.toml modified: src/lib.rs --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 79 +++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 57 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 447eecd..e6f2a8b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1036,7 +1036,7 @@ dependencies = [ [[package]] name = "soundcloud" -version = "0.1.7" +version = "0.1.8" dependencies = [ "hyper-util", "regex", diff --git a/Cargo.toml b/Cargo.toml index 434a93d..e512271 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "soundcloud" -version = "0.1.7" +version = "0.1.8" edition = "2021" description = "A small rust crate for fetching data from soundcloud without developer account" authors = ["alterwain"] diff --git a/src/lib.rs b/src/lib.rs index 368e0c7..9e149fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ use std::error::Error; use regex::Regex; use reqwest::header::USER_AGENT; -use sobjects::CloudPlaylists; +use sobjects::{CloudPlaylists, CloudTrack}; pub mod sobjects; @@ -21,12 +21,16 @@ async fn main() -> Result<(), Box> { let client_id = get_client_id().await.unwrap().unwrap(); let user_id: u64 = 774639751; - + println!("Playlists: {:#?}", get_playlists(user_id, client_id, app_version).await.unwrap()); Ok(()) }*/ -pub async fn get_likes(user_id: u64, client_id: String, app_version: String) -> Result, Box> { +pub async fn get_likes( + user_id: u64, + client_id: String, + app_version: String, +) -> Result, Box> { let client = reqwest::Client::new(); let resp = client.get(format!("https://{}/users/{}/likes?client_id={}&limit=10&offset=0&linked_partitioning=1&app_version={}&app_locale=en", SOUNDCLOUD_API_DOMAIN, user_id, client_id, app_version) ) @@ -39,7 +43,40 @@ pub async fn get_likes(user_id: u64, client_id: String, app_version: String) -> Ok(Some(resp)) } -pub async fn get_playlists(user_id: u64, client_id: String, app_version: String) -> Result> { +pub async fn get_tracks( + pl: Vec, + client_id: String, + app_version: String, +) -> Result, Box> { + let ids = pl + .iter() + .map(|t| t.id.to_string()) + .collect::>() + .join(","); + + let client = reqwest::Client::new(); + + let resp = client + .get(format!( + "https://{}/tracks?ids={}&client_id={}&app_version={}&app_locale=en", + SOUNDCLOUD_API_DOMAIN, ids, client_id, app_version + )) + .header(USER_AGENT, CHROME_USER_AGENT) + .send() + .await? + .text() + .await?; + + let tracks: Vec = serde_json::from_str(&resp)?; + + Ok(tracks) +} + +pub async fn get_playlists( + user_id: u64, + client_id: String, + app_version: String, +) -> Result> { let client = reqwest::Client::new(); let resp = client.get(format!("https://{}/users/{}/playlists_without_albums?client_id={}&limit=10&offset=0&linked_partitioning=1&app_version={}&app_locale=en", SOUNDCLOUD_API_DOMAIN, user_id, client_id, app_version)) @@ -57,7 +94,8 @@ pub async fn get_playlists(user_id: u64, client_id: String, app_version: String) pub async fn get_app() -> Result, Box> { let client = reqwest::Client::new(); - let resp = client.get(format!("{}/versions.json", SOUNDCLOUD_DOMAIN)) + let resp = client + .get(format!("{}/versions.json", SOUNDCLOUD_DOMAIN)) .header(USER_AGENT, CHROME_USER_AGENT) .send() .await? @@ -65,22 +103,15 @@ pub async fn get_app() -> Result, Box> { .await?; let json: serde_json::Value = serde_json::from_str(&resp).expect("JSON was not well-formatted"); - - Ok( - Some( - json.get("app") - .unwrap() - .as_str() - .unwrap() - .to_string() - ) - ) + + Ok(Some(json.get("app").unwrap().as_str().unwrap().to_string())) } pub async fn get_client_id() -> Result, Box> { let client = reqwest::Client::new(); - let resp = client.get(format!("{}/soundcloud", SOUNDCLOUD_DOMAIN)) + let resp = client + .get(format!("{}/soundcloud", SOUNDCLOUD_DOMAIN)) .header(USER_AGENT, CHROME_USER_AGENT) .send() .await? @@ -91,10 +122,13 @@ pub async fn get_client_id() -> Result, Box> { let mut urls = Vec::new(); - for cap in Regex::new(r#"