aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2018-03-22 21:23:58 +0100
committerJokler <jokler.contact@gmail.com>2018-03-22 21:23:58 +0100
commitabc0b5bccb0029c2e14d3cf1d304945480bc27d3 (patch)
treea6e87f5f707c1ac98a9fe7179c7d3e723dba2de6 /src
parentb636c6fb4f9f6fce1e567624cab5d975fd7aabd0 (diff)
downloadfrippy-abc0b5bccb0029c2e14d3cf1d304945480bc27d3.tar.gz
frippy-abc0b5bccb0029c2e14d3cf1d304945480bc27d3.zip
Let the Url plugin check for og:titles
Diffstat (limited to 'src')
-rw-r--r--src/plugins/url.rs31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/plugins/url.rs b/src/plugins/url.rs
index df3329a..ec98900 100644
--- a/src/plugins/url.rs
+++ b/src/plugins/url.rs
@@ -35,6 +35,28 @@ impl Url {
Some(captures.get(2)?.as_str().to_owned())
}
+
+ fn get_ogtitle<'a>(&self, body: &str) -> Result<String, UrlError> {
+ let title = body.find("property=\"og:title\"")
+ .map(|tag| {
+ body[tag..]
+ .find("content=\"")
+ .map(|offset| tag + offset + 9)
+ .map(|start| {
+ body[start..]
+ .find("\"")
+ .map(|offset| start + offset)
+ .map(|end| &body[start..end])
+ })
+ })
+ .and_then(|s| s.and_then(|s| s))
+ .ok_or(ErrorKind::MissingTitle)?;
+
+ debug!("Title: {:?}", title);
+
+ htmlescape::decode_html(title).map_err(|_| ErrorKind::HtmlDecoding.into())
+ }
+
fn get_title<'a>(&self, body: &str) -> Result<String, UrlError> {
let title = body.find("<title")
.map(|tag| {
@@ -60,7 +82,14 @@ impl Url {
let url = self.grep_url(text).ok_or(ErrorKind::MissingUrl)?;
let body = utils::download(&url, Some(self.max_kib)).context(ErrorKind::Download)?;
- let title = self.get_title(&body)?;
+ let title = match self.get_ogtitle(&body) {
+ Ok(t) => t,
+ Err(e) => if e.kind() == ErrorKind::MissingTitle {
+ self.get_title(&body)?
+ } else {
+ Err(e)?
+ }
+ };
Ok(title.trim().replace('\n', "|").replace('\r', "|"))
}