From 582dddf910116a707deb0b783d3b204ea322ef01 Mon Sep 17 00:00:00 2001 From: brqnko Date: Sat, 4 May 2024 01:33:53 +0900 Subject: [PATCH 1/3] feat: search suggestion --- examples/suggestion.rs | 12 ++++++++++++ src/search/youtube.rs | 28 ++++++++++++++++++++++++++++ tests/suggestion.rs | 10 ++++++++++ 3 files changed, 50 insertions(+) create mode 100644 examples/suggestion.rs create mode 100644 tests/suggestion.rs diff --git a/examples/suggestion.rs b/examples/suggestion.rs new file mode 100644 index 0000000..a38a6e0 --- /dev/null +++ b/examples/suggestion.rs @@ -0,0 +1,12 @@ +use rusty_ytdl::search::YouTube; + +#[tokio::main] +async fn main() { + use rusty_ytdl::search::YouTube; + + let youtube = YouTube::new().unwrap(); + + let res = youtube.suggestion("i know ").await; + + println!("{res:#?}"); +} \ No newline at end of file diff --git a/src/search/youtube.rs b/src/search/youtube.rs index b8b479e..e9015fd 100644 --- a/src/search/youtube.rs +++ b/src/search/youtube.rs @@ -201,6 +201,34 @@ impl YouTube { Ok(res.first().cloned()) } + pub async fn suggestion( + &self, + query: impl Into, + ) -> Result, VideoError> { + let query: String = query.into(); + let url = format!( + "https://suggestqueries-clients6.youtube.com/complete/search?client=android&q={query}", + query = encode(query.trim()) + ); + + let body = get_html(&self.client, url, None).await?; + + let serde_value = serde_json::from_str::(&body).unwrap(); + + let suggestion = serde_value + .as_array() + .unwrap() + .get(1) + .unwrap() + .as_array() + .unwrap() + .iter() + .map(|x| x.to_string()) + .collect(); + + return Ok(suggestion); + } + async fn innertube_key(&self) -> String { { let innertube_cache = self.innertube_cache.read().unwrap(); diff --git a/tests/suggestion.rs b/tests/suggestion.rs new file mode 100644 index 0000000..6f6a1cf --- /dev/null +++ b/tests/suggestion.rs @@ -0,0 +1,10 @@ +#[tokio::test] +async fn suggestion() { + use rusty_ytdl::search::YouTube; + + let youtube = YouTube::new().unwrap(); + + let res = youtube.suggestion("i know ").await; + + println!("{res:#?}"); +} \ No newline at end of file From 39121290b212b93959e5ebfe2f9b95e49b2d791f Mon Sep 17 00:00:00 2001 From: brqnko Date: Sat, 4 May 2024 01:37:12 +0900 Subject: [PATCH 2/3] add a description for suggestion --- src/search/youtube.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/search/youtube.rs b/src/search/youtube.rs index e9015fd..189c913 100644 --- a/src/search/youtube.rs +++ b/src/search/youtube.rs @@ -201,6 +201,15 @@ impl YouTube { Ok(res.first().cloned()) } + /// Fetch search suggestion with specific `query` + /// # Example + /// ```ignore + /// let youtube = YouTube::new().unwrap(); + /// + /// let res = youtube.suggestion("i know ").await; + /// + /// println!("{res:#?}"); + /// ``` pub async fn suggestion( &self, query: impl Into, From 3900c3dea7d65cee4801bad6bc5b0ed53982f11c Mon Sep 17 00:00:00 2001 From: brqnko Date: Sat, 4 May 2024 09:19:20 +0900 Subject: [PATCH 3/3] removed undeeded return statement --- src/search/youtube.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/search/youtube.rs b/src/search/youtube.rs index 189c913..4073e2f 100644 --- a/src/search/youtube.rs +++ b/src/search/youtube.rs @@ -235,7 +235,7 @@ impl YouTube { .map(|x| x.to_string()) .collect(); - return Ok(suggestion); + Ok(suggestion) } async fn innertube_key(&self) -> String {