Skip to content

Commit

Permalink
GH-73: Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
SetZero committed Jul 18, 2023
1 parent 5d4c715 commit f89f703
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
22 changes: 11 additions & 11 deletions src-tauri/src/commands/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ pub struct OpenGraphCrawler {
}

impl OpenGraphCrawler {
pub fn new() -> Self {
pub fn try_new() -> Option<Self> {
let client = reqwest::Client::builder()
.redirect(Policy::limited(10))
.user_agent("FancyMumbleClient/0.1.0")
//.cookie_store(true)
.build()
.unwrap();
OpenGraphCrawler { client }
.ok()?;
Some(Self { client })
}

pub async fn crawl(&self, url: &str) -> Option<OpenGraphMetadata> {
let response = self.client.get(url).send().await.ok()?;
let body = response.text().await.ok()?;
let document = Html::parse_document(&body);

let mut title = self.extract_metadata(&document, "og:title");
let description = self.extract_metadata(&document, "og:description");
let image = self.extract_metadata(&document, "og:image");
let mut title = Self::extract_metadata(&document, "og:title");
let description = Self::extract_metadata(&document, "og:description");
let image = Self::extract_metadata(&document, "og:image");

if title.is_none() {
title = self.extract_property(&document, "title");
title = Self::extract_property(&document, "title");
}

Some(OpenGraphMetadata {
Expand All @@ -44,14 +44,14 @@ impl OpenGraphCrawler {
})
}

fn extract_metadata(&self, document: &Html, property: &str) -> Option<String> {
let selector = Selector::parse(&format!("meta[property='{}']", property)).unwrap();
fn extract_metadata(document: &Html, property: &str) -> Option<String> {
let selector = Selector::parse(&format!("meta[property='{property}']")).ok()?;
let element = document.select(&selector).next()?;
element.value().attr("content").map(String::from)
}

fn extract_property(&self, document: &Html, property: &str) -> Option<String> {
let selector = Selector::parse(property).unwrap();
fn extract_property(document: &Html, property: &str) -> Option<String> {
let selector = Selector::parse(property).ok()?;
let element = document.select(&selector).next()?;
Some(element.children().next()?.value().as_text()?.to_string())
}
Expand Down
20 changes: 11 additions & 9 deletions src-tauri/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,18 @@ pub async fn get_open_graph_data_from_website(
url: &str,
) -> Result<String, String> {
// setup crawler if not already done
let mut client = state.crawler.lock().await;
if client.is_none() {
*client = Some(OpenGraphCrawler::new());
}

let client = client
.as_ref()
.ok_or("Failed to read website body".to_string())?;
let result = {
let mut client = state.crawler.lock().await;
if client.is_none() {
*client = OpenGraphCrawler::try_new();
}

let result = client.crawl(url).await;
client
.as_ref()
.ok_or_else(|| "Failed to read website body".to_string())?
.crawl(url)
.await
};

let result = json!(result);

Expand Down

0 comments on commit f89f703

Please sign in to comment.