-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Azorlogh/feature/search
Track search
- Loading branch information
Showing
23 changed files
with
900 additions
and
156 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::rc::Rc; | ||
|
||
use anyhow::Result; | ||
|
||
mod soundcloud; | ||
use druid::{Data, ImageBuf, Lens}; | ||
pub use soundcloud::Soundcloud; | ||
use url::Url; | ||
|
||
pub trait Plugin { | ||
fn search(&self, query: &str) -> Result<Vec<SearchResult>>; | ||
} | ||
|
||
#[derive(Debug, Clone, Data, Lens)] | ||
pub struct SearchResult { | ||
pub url: Rc<Url>, | ||
pub artist: String, | ||
pub title: String, | ||
pub artwork: Option<ImageBuf>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use url::Url; | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub struct SearchResponse { | ||
pub collection: Vec<SearchResult>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
#[serde(rename_all = "snake_case")] | ||
#[serde(tag = "kind")] | ||
pub enum SearchResult { | ||
Track { | ||
permalink_url: Url, | ||
user: User, | ||
title: String, | ||
artwork_url: Option<Url>, | ||
}, | ||
User, | ||
Playlist, | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub struct User { | ||
pub username: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::rc::Rc; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use druid::piet::ImageFormat; | ||
use percent_encoding::{AsciiSet, CONTROLS}; | ||
use tracing::debug; | ||
|
||
use super::Plugin; | ||
|
||
mod api; | ||
|
||
pub struct Soundcloud { | ||
client_id: String, | ||
} | ||
|
||
impl Soundcloud { | ||
pub fn new() -> Result<Self> { | ||
// grap soundcloud home page | ||
let body = ureq::get("https://soundcloud.com").call()?.into_string()?; | ||
|
||
// grab location of a specific script | ||
let re = regex::Regex::new(r#"<script crossorigin src="([^\n]*)">"#).unwrap(); | ||
let magic_script = &re | ||
.captures_iter(&body) | ||
.last() | ||
.ok_or(anyhow!("could not find url to the magic script"))?[1]; | ||
|
||
// grab that script | ||
let body = ureq::get(magic_script).call()?.into_string()?; | ||
let re = regex::Regex::new(r#"client_id:"([^"]*)""#).unwrap(); | ||
|
||
let client_id = re | ||
.captures(&body) | ||
.ok_or(anyhow!("missing client id in script"))?[1] | ||
.to_owned(); | ||
|
||
debug!("soundcloud client id: {:?}", client_id); | ||
|
||
Ok(Self { client_id }) | ||
} | ||
} | ||
|
||
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`'); | ||
|
||
impl Plugin for Soundcloud { | ||
fn search(&self, query: &str) -> Result<Vec<super::SearchResult>> { | ||
let query_enc = percent_encoding::utf8_percent_encode(query, FRAGMENT); | ||
let response_json = ureq::get(&format!( | ||
"https://api-v2.soundcloud.com/search?client_id={}&q={}&limit=10&offset=0&linked_partitioning=1&app_version=1665395834&app_locale=en", | ||
self.client_id, query_enc | ||
)) | ||
.call()? | ||
.into_string()?; | ||
let response: api::SearchResponse = serde_json::from_str(&response_json)?; | ||
|
||
Ok(response | ||
.collection | ||
.into_iter() | ||
.filter_map(|res| match res { | ||
api::SearchResult::Track { | ||
permalink_url, | ||
user, | ||
title, | ||
artwork_url, | ||
} => { | ||
let artwork = artwork_url.and_then(|url| { | ||
let mut artwork_buf = vec![]; | ||
ureq::get(url.as_str()) | ||
.call() | ||
.ok()? | ||
.into_reader() | ||
.read_to_end(&mut artwork_buf) | ||
.ok()?; | ||
let artwork_image = | ||
image::io::Reader::new(std::io::Cursor::new(artwork_buf)) | ||
.with_guessed_format() | ||
.ok()? | ||
.decode() | ||
.ok()? | ||
.to_rgb8(); | ||
Some(druid::ImageBuf::from_raw( | ||
artwork_image.as_raw().as_slice(), | ||
ImageFormat::Rgb, | ||
artwork_image.width() as usize, | ||
artwork_image.height() as usize, | ||
)) | ||
}); | ||
Some(super::SearchResult { | ||
url: Rc::new(permalink_url), | ||
artist: user.username, | ||
title, | ||
artwork, | ||
}) | ||
} | ||
_ => None, | ||
}) | ||
.collect()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// TODO |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.