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
use scraper::{Html, Selector};
use std::fmt;
#[derive(Debug)]
pub struct OpenGraph {
title: Option<String>,
description: Option<String>,
url: Option<String>,
image: Option<String>,
object_type: Option<String>,
locale: Option<String>,
}
pub enum OpenGraphTag {
Title,
Url,
Image,
Type,
Description,
Locale,
ImageHeight,
ImageWidth,
SiteName,
}
impl fmt::Debug for OpenGraphTag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.str())
}
}
impl OpenGraphTag {
fn str(&self) -> &str {
match self {
OpenGraphTag::Title => "title",
OpenGraphTag::Url => "url",
OpenGraphTag::Image => "image",
OpenGraphTag::Type => "type",
OpenGraphTag::Description => "description",
OpenGraphTag::Locale => "locale",
OpenGraphTag::ImageHeight => "image:height",
OpenGraphTag::ImageWidth => "image:width",
OpenGraphTag::SiteName => "site_name",
}
}
}
pub fn find_og_tag(html: &Html, tag: OpenGraphTag) -> Option<String> {
let selector = Selector::parse(&format!("meta[property=\"og:{}\"]", tag.str())).unwrap();
if let Some(element) = html.select(&selector).next() {
if let Some(value) = element.value().attr("content") {
return Some(value.to_string());
}
}
None
}
#[cfg(test)]
mod tests {
use crate::html_from_bytes;
use crate::tests::OG_COMPILANT_HTML;
use super::{find_og_tag, OpenGraphTag};
#[test]
fn retrieves_title_from_og_compilant_html() {
let html = html_from_bytes(OG_COMPILANT_HTML).unwrap();
let title = find_og_tag(&html, OpenGraphTag::Title).unwrap();
assert_eq!(title, "SEO Strategies for a better web");
}
#[test]
fn retrieves_description_from_og_compilant_html() {
let html = html_from_bytes(OG_COMPILANT_HTML).unwrap();
let description = find_og_tag(&html, OpenGraphTag::Description).unwrap();
assert_eq!(description, "John Appleseed tells you his secrets on SEO for a better web experience by taking advantage of OpenGraph\'s Tags!");
}
#[test]
fn retrieves_url_from_og_compilant_html() {
let html = html_from_bytes(OG_COMPILANT_HTML).unwrap();
let url = find_og_tag(&html, OpenGraphTag::Url).unwrap();
assert_eq!(url, "https://abetterweb.com");
}
#[test]
fn retrieves_locale_from_og_compilant_html() {
let html = html_from_bytes(OG_COMPILANT_HTML).unwrap();
let locale = find_og_tag(&html, OpenGraphTag::Locale).unwrap();
assert_eq!(locale, "en_US");
}
#[test]
fn retrieves_image_from_og_compilant_html() {
let html = html_from_bytes(OG_COMPILANT_HTML).unwrap();
let image = find_og_tag(&html, OpenGraphTag::Image).unwrap();
assert_eq!(
image,
"https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201809210816"
);
}
#[test]
fn retrieves_type_from_og_compilant_html() {
let html = html_from_bytes(OG_COMPILANT_HTML).unwrap();
let r#type = find_og_tag(&html, OpenGraphTag::Type).unwrap();
assert_eq!(r#type, "website");
}
#[test]
fn retrieves_image_height_from_og_compilant_html() {
let html = html_from_bytes(OG_COMPILANT_HTML).unwrap();
let height = find_og_tag(&html, OpenGraphTag::ImageHeight).unwrap();
assert_eq!(height, "600");
}
#[test]
fn retrieves_image_width_from_og_compilant_html() {
let html = html_from_bytes(OG_COMPILANT_HTML).unwrap();
let width = find_og_tag(&html, OpenGraphTag::ImageWidth).unwrap();
assert_eq!(width, "1200");
}
#[test]
fn retrieves_site_name_from_og_compilant_html() {
let html = html_from_bytes(OG_COMPILANT_HTML).unwrap();
let site_name = find_og_tag(&html, OpenGraphTag::SiteName).unwrap();
assert_eq!(site_name, "TechPro");
}
}