Remove blacklisting to make SiteIcons Send
This commit is contained in:
parent
9e41e65c45
commit
1925d4748e
2 changed files with 15 additions and 45 deletions
|
@ -13,7 +13,6 @@ use url::Url;
|
||||||
pub async fn parse_site_logo(
|
pub async fn parse_site_logo(
|
||||||
url: &Url,
|
url: &Url,
|
||||||
mut body: impl Stream<Item = Result<Vec<u8>, String>> + Unpin,
|
mut body: impl Stream<Item = Result<Vec<u8>, String>> + Unpin,
|
||||||
is_blacklisted: impl Fn(&Url) -> bool,
|
|
||||||
) -> Result<Icon, Box<dyn Error>> {
|
) -> Result<Icon, Box<dyn Error>> {
|
||||||
let mut parser = driver::parse_document(Html::new_document(), Default::default());
|
let mut parser = driver::parse_document(Html::new_document(), Default::default());
|
||||||
while let Some(data) = body.next().await {
|
while let Some(data) = body.next().await {
|
||||||
|
@ -123,12 +122,6 @@ pub async fn parse_site_logo(
|
||||||
elem.attr("src").and_then(|href| url.join(&href).ok())
|
elem.attr("src").and_then(|href| url.join(&href).ok())
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(href) = &href {
|
|
||||||
if is_blacklisted(href) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
href.map(|href| (href, elem_ref, weight))
|
href.map(|href| (href, elem_ref, weight))
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
29
src/icons.rs
29
src/icons.rs
|
@ -10,9 +10,7 @@ use std::error::Error;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use vec1::Vec1;
|
use vec1::Vec1;
|
||||||
|
|
||||||
pub struct SiteIcons {
|
pub struct SiteIcons;
|
||||||
blacklist: Option<Box<dyn Fn(&Url) -> bool>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum LoadedKind {
|
enum LoadedKind {
|
||||||
|
@ -24,21 +22,7 @@ enum LoadedKind {
|
||||||
|
|
||||||
impl SiteIcons {
|
impl SiteIcons {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
SiteIcons { blacklist: None }
|
SiteIcons
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_with_blacklist(blacklist: impl Fn(&Url) -> bool + 'static) -> Self {
|
|
||||||
SiteIcons {
|
|
||||||
blacklist: Some(Box::new(blacklist)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_blacklisted(&self, url: &Url) -> bool {
|
|
||||||
if let Some(is_blacklisted) = &self.blacklist {
|
|
||||||
is_blacklisted(url)
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn load_website<U: IntoUrl>(
|
pub async fn load_website<U: IntoUrl>(
|
||||||
|
@ -78,9 +62,6 @@ impl SiteIcons {
|
||||||
|
|
||||||
let url = res.url().clone();
|
let url = res.url().clone();
|
||||||
|
|
||||||
if self.is_blacklisted(&url) {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
let body = res.bytes_stream().map(|res| {
|
let body = res.bytes_stream().map(|res| {
|
||||||
res
|
res
|
||||||
.map(|bytes| bytes.to_vec())
|
.map(|bytes| bytes.to_vec())
|
||||||
|
@ -96,7 +77,6 @@ impl SiteIcons {
|
||||||
subscriber,
|
subscriber,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
.shared();
|
.shared();
|
||||||
|
|
||||||
let mut futures = vec![
|
let mut futures = vec![
|
||||||
|
@ -117,10 +97,7 @@ impl SiteIcons {
|
||||||
|
|
||||||
LoadedKind::SiteLogo(match html_response {
|
LoadedKind::SiteLogo(match html_response {
|
||||||
Some((url, complete, body)) => {
|
Some((url, complete, body)) => {
|
||||||
let (icons, _) = join!(
|
let (icons, _) = join!(html_parser::parse_site_logo(&url, body), complete);
|
||||||
html_parser::parse_site_logo(&url, body, |url| self.is_blacklisted(url)),
|
|
||||||
complete
|
|
||||||
);
|
|
||||||
|
|
||||||
icons.ok()
|
icons.ok()
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue