Skip to content

Commit

Permalink
Merge pull request #25 from brqnko/main
Browse files Browse the repository at this point in the history
feat: search suggestion
  • Loading branch information
Mithronn authored May 4, 2024
2 parents ccf2a18 + 3900c3d commit 048c979
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/suggestion.rs
Original file line number Diff line number Diff line change
@@ -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:#?}");
}
37 changes: 37 additions & 0 deletions src/search/youtube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,43 @@ 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<String>,
) -> Result<Vec<String>, 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::<serde_json::Value>(&body).unwrap();

let suggestion = serde_value
.as_array()
.unwrap()
.get(1)
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|x| x.to_string())
.collect();

Ok(suggestion)
}

async fn innertube_key(&self) -> String {
{
let innertube_cache = self.innertube_cache.read().unwrap();
Expand Down
10 changes: 10 additions & 0 deletions tests/suggestion.rs
Original file line number Diff line number Diff line change
@@ -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:#?}");
}

0 comments on commit 048c979

Please sign in to comment.