feat(scraper): added search option
This commit is contained in:
parent
0b17c760cb
commit
ac98af1428
5 changed files with 77 additions and 3 deletions
|
|
@ -59,6 +59,14 @@ impl GlobalState {
|
|||
) -> color_eyre::Result<Option<froxy_scraper::types::Profile>> {
|
||||
proxy::getuser(self, login).await
|
||||
}
|
||||
|
||||
pub async fn search(
|
||||
&self,
|
||||
keyword: String,
|
||||
friends_only: i32,
|
||||
) -> color_eyre::Result<Option<Vec<froxy_scraper::types::Search>>> {
|
||||
proxy::search(self, keyword, friends_only).await
|
||||
}
|
||||
}
|
||||
|
||||
impl GlobalState {}
|
||||
|
|
|
|||
|
|
@ -23,3 +23,18 @@ pub async fn getuser(
|
|||
) -> color_eyre::Result<Option<froxy_scraper::types::Profile>> {
|
||||
Ok(self_.scraper.get_user(login.as_str()).await?)
|
||||
}
|
||||
|
||||
#[cached(
|
||||
time = 3,
|
||||
size = 100,
|
||||
result = true,
|
||||
key = "(String, i32)",
|
||||
convert = "{ (keyword.clone(), friends_only) }"
|
||||
)]
|
||||
pub async fn search(
|
||||
self_: &GlobalState,
|
||||
keyword: String,
|
||||
friends_only: i32,
|
||||
) -> color_eyre::Result<Option<Vec<froxy_scraper::types::Search>>> {
|
||||
Ok(self_.scraper.search(keyword, friends_only).await?)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ use axum::{
|
|||
};
|
||||
|
||||
pub fn router() -> Router<crate::GlobalState> {
|
||||
Router::new().route("/getuser/{login}", get(getuser_handler))
|
||||
Router::new()
|
||||
.route("/getuser/{login}", get(getuser_handler))
|
||||
.route("/search/{keyword}/{friends_only}", get(search_handler))
|
||||
}
|
||||
|
||||
#[cfg_attr(debug_assertions, axum::debug_handler)]
|
||||
|
|
@ -22,3 +24,21 @@ async fn getuser_handler(
|
|||
.map_err(|()| StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.and_then(|o| o.ok_or(StatusCode::NOT_FOUND).map(Json))
|
||||
}
|
||||
|
||||
// /search/<keyword>/<int:friends_only>
|
||||
async fn search_handler(
|
||||
State(state): State<crate::GlobalState>,
|
||||
Path((keyword, friends_only)): Path<(String, i32)>,
|
||||
) -> Result<Json<Vec<froxy_scraper::types::Search>>, StatusCode> {
|
||||
log::info!(
|
||||
"Searching `{keyword}` (friends_only: {})",
|
||||
friends_only == 1,
|
||||
);
|
||||
state
|
||||
.search(keyword, friends_only)
|
||||
.await
|
||||
.map_err(crate::report_error)
|
||||
.map_err(|()| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.ok_or(StatusCode::BAD_REQUEST)
|
||||
.map(Json)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue