-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
scraper
with a small custom library (#593)
* Add small custom scraper library * add control flow capabilities * fix lint
- Loading branch information
Showing
11 changed files
with
287 additions
and
259 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "schaber" | ||
authors.workspace = true | ||
edition.workspace = true | ||
version.workspace = true | ||
license = "MIT OR Apache-2.0" | ||
|
||
[dependencies] | ||
lol_html = "2.0.0" | ||
thiserror = "1.0.64" | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../LICENSE-APACHE-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../LICENSE-MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use lol_html::{ | ||
errors::{RewritingError, SelectorError}, | ||
html_content::Element, | ||
ElementContentHandlers, HandlerResult, HtmlRewriter, Selector, Settings, | ||
}; | ||
use std::{borrow::Cow, ops::ControlFlow, str::FromStr}; | ||
use thiserror::Error; | ||
|
||
type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
||
/// Ignore any content handler "errors", since we use these errors | ||
/// as our means of communicating control flow | ||
macro_rules! handle_error { | ||
($error_expr:expr) => {{ | ||
match { $error_expr } { | ||
Err(::lol_html::errors::RewritingError::ContentHandlerError(..)) => return Ok(()), | ||
other => other, | ||
} | ||
}}; | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
#[error("small sacrifice for the lol_html gods")] | ||
struct Sacrifice; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
InvalidSelector(#[from] SelectorError), | ||
|
||
#[error(transparent)] | ||
RewriteError(#[from] RewritingError), | ||
} | ||
|
||
pub struct Scraper { | ||
element_selector: Selector, | ||
} | ||
|
||
impl Scraper { | ||
pub fn new(selector: &str) -> Result<Self> { | ||
Ok(Self { | ||
element_selector: Selector::from_str(selector)?, | ||
}) | ||
} | ||
|
||
pub fn process<I, H>(&self, input: I, mut handler: H) -> Result<()> | ||
where | ||
I: AsRef<[u8]>, | ||
H: FnMut(&Element<'_, '_>) -> ControlFlow<()>, | ||
{ | ||
#[inline] | ||
fn handler_assert<F>(uwu: F) -> F | ||
where | ||
F: FnMut(&mut Element<'_, '_>) -> HandlerResult, | ||
{ | ||
uwu | ||
} | ||
|
||
#[inline] | ||
fn sink_assert<F>(uwu: F) -> F | ||
where | ||
F: FnMut(&[u8]), | ||
{ | ||
uwu | ||
} | ||
|
||
let mut rewriter = HtmlRewriter::new( | ||
Settings { | ||
element_content_handlers: vec![( | ||
Cow::Borrowed(&self.element_selector), | ||
ElementContentHandlers::default().element(handler_assert(|el| { | ||
if handler(el).is_continue() { | ||
Ok(()) | ||
} else { | ||
Err(Box::new(Sacrifice)) | ||
} | ||
})), | ||
)], | ||
..Settings::new() | ||
}, | ||
sink_assert(|_| {}), | ||
); | ||
|
||
handle_error!(rewriter.write(input.as_ref()))?; | ||
handle_error!(rewriter.end())?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use schaber::Scraper; | ||
use std::ops::ControlFlow; | ||
|
||
#[test] | ||
fn select_link() { | ||
let html = r#" | ||
<div id="hello"> | ||
<a href="http://druckbrudi.lab"> | ||
PRINT MORE BLÅHAJ CATEARS! | ||
</a> | ||
</div> | ||
"#; | ||
|
||
let mut link_url = None; | ||
let scraper = Scraper::new("a").unwrap(); | ||
|
||
scraper | ||
.process(html, |element| { | ||
link_url = element.get_attribute("href"); | ||
ControlFlow::Break(()) | ||
}) | ||
.unwrap(); | ||
|
||
assert_eq!(link_url.as_deref(), Some("http://druckbrudi.lab")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use schaber::Scraper; | ||
use std::ops::ControlFlow; | ||
|
||
#[test] | ||
fn ends_after_break() { | ||
let html = r#" | ||
<div id="hello"> | ||
<a href="http://druckbrudi.lab"> | ||
PRINT MORE BLÅHAJ CATEARS! | ||
</a> | ||
<a href="http://evil.com"> | ||
This link shall not be seen! | ||
</a> | ||
</div> | ||
"#; | ||
|
||
let mut link_url = None; | ||
let scraper = Scraper::new("a").unwrap(); | ||
|
||
scraper | ||
.process(html, |element| { | ||
link_url = element.get_attribute("href"); | ||
ControlFlow::Break(()) | ||
}) | ||
.unwrap(); | ||
|
||
assert_eq!(link_url.as_deref(), Some("http://druckbrudi.lab")); | ||
} | ||
|
||
#[test] | ||
fn continues_after_continue() { | ||
let html = r#" | ||
<div id="hello"> | ||
<a href="http://druckbrudi.lab"> | ||
PRINT MORE BLÅHAJ CATEARS! | ||
</a> | ||
<a href="https://good.org"> | ||
This link shall be seen! | ||
</a> | ||
</div> | ||
"#; | ||
|
||
let mut link_url = None; | ||
let scraper = Scraper::new("a").unwrap(); | ||
|
||
scraper | ||
.process(html, |element| { | ||
link_url = element.get_attribute("href"); | ||
ControlFlow::Continue(()) | ||
}) | ||
.unwrap(); | ||
|
||
assert_eq!(link_url.as_deref(), Some("https://good.org")); | ||
} |