feat(scraper): added search option

This commit is contained in:
Maieul BOYER 2026-02-09 00:01:07 +01:00
parent 0b17c760cb
commit ac98af1428
Signed by: maix
SSH key fingerprint: SHA256:iqCzqFFF5KjRixmDExqbAltCIj9ndlBWIGJf3t9Ln9g
5 changed files with 77 additions and 3 deletions

View file

@ -135,7 +135,7 @@ impl State {
pub async fn get_user(
&self,
name: &str,
) -> Result<Option<crate::types::Profile>, GetUserError> {
) -> Result<Option<crate::types::Profile>, FetchJsonError> {
let req = self.get(format!("/getuser/{name}"), None).await?;
let text = req.text().await?;
let json = serde_json::from_str::<crate::types::ProfileRaw>(&text)?;
@ -204,10 +204,31 @@ impl State {
self.get("/friends/", None).await?.text().await?,
))
}
pub async fn search(
&self,
keyword: impl AsRef<str>,
relation: i32,
) -> Result<Option<Vec<crate::types::Search>>, FetchJsonError> {
//search/<keyword>/<int:friends_only>
let text = self
.get(format!("/search/{}/{relation}", keyword.as_ref()), None)
.await?
.text()
.await?;
log::debug!("Search result => {text:?}");
if text.is_empty() {
return Ok(None);
};
let ret = serde_json::from_str(&text)?;
Ok(ret)
}
}
#[derive(Debug, thiserror::Error)]
pub enum GetUserError {
pub enum FetchJsonError {
#[error("RequestError: {0}")]
RequestError(#[from] reqwest::Error),
#[error("JsonError: {0}")]

View file

@ -115,3 +115,13 @@ pub struct Profile {
pub discord: Option<String>,
pub github: Option<String>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq, Hash)]
#[serde(tag = "type")]
pub enum Search {
#[serde(rename = "project")]
Project { v: SmolStr, s: SmolStr },
#[serde(rename = "user")]
User { v: SmolStr, s: SmolStr },
}