1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use scraper::Html;
use std::str::FromStr;
use std::string::FromUtf8Error;
use thiserror::Error;
use url::Url;

use crate::html::{find_link, find_meta_tag, first_inner_html};
use crate::og::{find_og_tag, OpenGraphTag};
use crate::schema::{find_schema_tag, SchemaMetaTag};
use crate::twitter::{find_twitter_tag, TwitterMetaTag};

#[derive(Error, Debug)]
pub enum Error {
    #[error("The provided byte slice contains invalid UTF-8 characters")]
    InvalidUtf8(FromUtf8Error),
}

#[derive(Clone, Debug)]
pub struct LinkPreview {
    pub title: Option<String>,
    pub description: Option<String>,
    pub domain: Option<String>,
    pub image_url: Option<Url>,
}

impl LinkPreview {
    /// Retrieves the `String` representation of `image_url` `Url` instance
    pub fn image_url_str(&self) -> Option<String> {
        if let Some(image_url) = self.image_url.clone() {
            return Some(image_url.to_string());
        }

        None
    }

    /// Attempts to find the description of the page in the following order:
    ///
    /// - Document's `<link rel="canonical" /> element's `href` attribute
    /// - OpenGraphTag's image meta tag (`og:image`)
    pub fn find_first_domain(html: &Html) -> Option<String> {
        if let Some(domain) = find_link(html, "canonical") {
            return LinkPreview::domain_from_string(domain);
        }

        if let Some(domain) = find_og_tag(html, OpenGraphTag::Url) {
            return LinkPreview::domain_from_string(domain);
        }

        None
    }

    /// Attempts to parse a `Url` from a `String` and then attempts to retrieve
    /// the `domain` fragment from such `Url` instance.
    ///
    /// If either the `Url` is invalid or theres no a domain fragment available
    /// (the provided `Url` points to an IP instead of a domain), `None` is
    /// returned.
    fn domain_from_string(value: String) -> Option<String> {
        let url = Url::parse(&value).ok()?;

        url.domain().map(|domain| domain.to_string())
    }

    /// Attempts to find the description of the page in the following order:
    ///
    /// - OpenGraphTag's image meta tag (`og:image`)
    /// - Document's `<link rel="image_url" /> element's `href` attribute
    /// - Twitter Card's image meta tag (`twitter:image`)
    /// - Schema.org image meta tag (`image`)
    pub fn find_first_image_url(html: &Html) -> Option<Url> {
        if let Some(image_url) = find_og_tag(html, OpenGraphTag::Image) {
            return Url::parse(&image_url).ok();
        }

        if let Some(image_url) = find_link(html, "image_src") {
            return Url::parse(&image_url).ok();
        }

        if let Some(image_url) = find_schema_tag(html, SchemaMetaTag::Image) {
            return Url::parse(&image_url).ok();
        }

        if let Some(image_url) = find_twitter_tag(html, TwitterMetaTag::Image) {
            return Url::parse(&image_url).ok();
        }

        None
    }

    /// Attempts to find the description of the page in the following order:
    ///
    /// - OpenGraphTag's description meta tag (`og:description`)
    /// - Twitter Card's description meta tag (`twitter:description`)
    /// - Schema.org description meta tag (`description`)
    /// - Description meta tag (`description`)
    /// - The first `p` element from the document
    pub fn find_first_description(html: &Html) -> Option<String> {
        if let Some(description) = find_og_tag(html, OpenGraphTag::Description) {
            return Some(description);
        }

        if let Some(description) = find_twitter_tag(html, TwitterMetaTag::Description) {
            return Some(description);
        }

        if let Some(description) = find_schema_tag(html, SchemaMetaTag::Description) {
            return Some(description);
        }

        if let Some(description) = find_meta_tag(html, "description") {
            return Some(description);
        }

        if let Some(description) = first_inner_html(html, "p") {
            return Some(description);
        }

        None
    }

    /// Attempts to find the title of the page in the following order:
    ///
    /// - OpenGraphTag's title meta tag (`og:title`)
    /// - Twitter Card's title meta tag (`twitter:title`)
    /// - Schema.org title meta tag (`title`)
    /// - The HTML's document title
    /// - The first `<h1>` tag in the document
    /// - The first `<h2>` tag in the document
    pub fn find_first_title(html: &Html) -> Option<String> {
        if let Some(title) = find_og_tag(html, OpenGraphTag::Title) {
            return Some(title);
        }

        if let Some(title) = find_twitter_tag(html, TwitterMetaTag::Title) {
            return Some(title);
        }

        if let Some(title) = find_schema_tag(html, SchemaMetaTag::Name) {
            return Some(title);
        }

        if let Some(title) = first_inner_html(html, "title") {
            return Some(title);
        }

        if let Some(title) = first_inner_html(html, "h1") {
            return Some(title);
        }

        if let Some(title) = first_inner_html(html, "h2") {
            return Some(title);
        }

        None
    }
}

impl From<Html> for LinkPreview {
    fn from(html: Html) -> Self {
        let image_url: Option<Url> = LinkPreview::find_first_image_url(&html);
        let domain = LinkPreview::find_first_domain(&html);

        LinkPreview {
            title: LinkPreview::find_first_title(&html),
            description: LinkPreview::find_first_description(&html),
            domain,
            image_url,
        }
    }
}

impl From<&Html> for LinkPreview {
    fn from(html: &Html) -> Self {
        let image_url: Option<Url> = LinkPreview::find_first_image_url(html);
        let domain: Option<String> = LinkPreview::find_first_domain(html);

        LinkPreview {
            title: LinkPreview::find_first_title(html),
            description: LinkPreview::find_first_description(html),
            domain,
            image_url,
        }
    }
}

impl FromStr for LinkPreview {
    type Err = Error;

    fn from_str(html: &str) -> Result<Self, Self::Err> {
        let html = Html::parse_document(html);
        let image_url: Option<Url> = LinkPreview::find_first_image_url(&html);
        let domain: Option<String> = LinkPreview::find_first_domain(&html);

        Ok(LinkPreview {
            title: LinkPreview::find_first_title(&html),
            description: LinkPreview::find_first_description(&html),
            domain,
            image_url,
        })
    }
}

/// Attempts to convert a HTML document byte slice into a HTML string instance
/// and then parses the document into a `Html` instance
pub fn html_from_bytes(value: &[u8]) -> Result<Html, Error> {
    let utf8 = String::from_utf8(value.to_vec()).map_err(Error::InvalidUtf8)?;

    Ok(Html::parse_document(utf8.as_str()))
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use crate::html_from_bytes;
    use crate::tests::FULL_FEATURED_HTML;

    use super::LinkPreview;

    #[test]
    fn creates_instance_of_link_preview_from_html_instance() {
        let html = html_from_bytes(FULL_FEATURED_HTML).unwrap();
        let link_preview = LinkPreview::from(&html);

        assert_eq!(
            link_preview.title.unwrap(),
            "SEO Strategies for a better web"
        );
        assert_eq!(link_preview.description.unwrap(), "John Appleseed tells you his secrets on SEO for a better web experience by taking advantage of OpenGraph\'s Tags!");
        assert_eq!(
            link_preview.image_url.unwrap().to_string(),
            "https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201809210816"
        );
        assert_eq!(link_preview.domain.unwrap().to_string(), "en.wikipedia.com");
    }

    #[test]
    fn creates_instance_of_link_preview_from_str_instance() {
        let html = String::from_utf8(FULL_FEATURED_HTML.to_vec()).unwrap();
        let link_preview = LinkPreview::from_str(&html).unwrap();

        assert_eq!(
            link_preview.title.unwrap(),
            "SEO Strategies for a better web"
        );
        assert_eq!(link_preview.description.unwrap(), "John Appleseed tells you his secrets on SEO for a better web experience by taking advantage of OpenGraph\'s Tags!");
        assert_eq!(
            link_preview.image_url.unwrap().to_string(),
            "https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201809210816"
        );
        assert_eq!(link_preview.domain.unwrap().to_string(), "en.wikipedia.com");
    }

    #[test]
    fn finds_first_title() {
        let html = html_from_bytes(FULL_FEATURED_HTML).unwrap();
        let title = LinkPreview::find_first_title(&html);

        assert_eq!(title.unwrap(), "SEO Strategies for a better web");
    }

    #[test]
    fn finds_first_description() {
        let html = html_from_bytes(FULL_FEATURED_HTML).unwrap();
        let title = LinkPreview::find_first_description(&html);

        assert_eq!(title.unwrap(), "John Appleseed tells you his secrets on SEO for a better web experience by taking advantage of OpenGraph\'s Tags!");
    }

    #[test]
    fn finds_first_image_url() {
        let html = html_from_bytes(FULL_FEATURED_HTML).unwrap();
        let image_url: Option<String> =
            LinkPreview::find_first_image_url(&html).and_then(|url| Some(url.to_string()));

        assert_eq!(
            image_url.unwrap(),
            "https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201809210816"
        );
    }

    #[test]
    fn finds_first_domain() {
        let html = html_from_bytes(FULL_FEATURED_HTML).unwrap();
        let domain = LinkPreview::find_first_domain(&html).and_then(|url| Some(url.to_string()));

        assert_eq!(domain.unwrap(), "en.wikipedia.com");
    }
}