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
//! Open Graph Protocol extractor
//!
//! Extracts Open Graph metadata used by Facebook, LinkedIn, WhatsApp, Slack, Discord.
//! Specification: https://ogp.me/
use crate::errors::Result;
use crate::extractors::common::{html_utils, url_utils};
use crate::types::social::{OgArticle, OgAudio, OgBook, OgImage, OgProfile, OgVideo, OpenGraph};
/// Extract Open Graph metadata from HTML
///
/// # Arguments
/// * `html` - HTML content to parse
/// * `base_url` - Optional base URL for resolving relative URLs
///
/// # Returns
/// * `Result<OpenGraph>` - Extracted Open Graph data
pub fn extract(html: &str, base_url: Option<&str>) -> Result<OpenGraph> {
let document = html_utils::parse_html(html);
let mut og = OpenGraph::default();
// Track current image/video/audio for structured properties
let mut current_image: Option<OgImage> = None;
let mut current_video: Option<OgVideo> = None;
let mut current_audio: Option<OgAudio> = None;
// Article metadata accumulator
let mut article_data = OgArticle::default();
let mut has_article_data = false;
// Book metadata accumulator
let mut book_data = OgBook::default();
let mut has_book_data = false;
// Profile metadata accumulator
let mut profile_data = OgProfile::default();
let mut has_profile_data = false;
// Extract meta tags with property="og:*" or property="article:*" etc.
if let Ok(selector) = html_utils::create_selector("meta[property]") {
for element in document.select(&selector) {
if let (Some(property), Some(content)) = (
html_utils::get_attr(&element, "property"),
html_utils::get_attr(&element, "content"),
) {
let content = content.trim().to_string();
if content.is_empty() {
continue;
}
// Parse property name
if let Some(prop) = property.strip_prefix("og:") {
match prop {
"title" => og.title = Some(content),
"type" => og.r#type = Some(content),
"url" => {
og.url =
Some(url_utils::resolve_url(base_url, &content).unwrap_or(content))
}
"image" => {
// Save previous image if exists
if let Some(img) = current_image.take() {
og.images.push(img);
}
let resolved_url = url_utils::resolve_url(base_url, &content)
.unwrap_or(content.clone());
// First image becomes the primary image
if og.image.is_none() {
og.image = Some(resolved_url.clone());
}
// Start new image
current_image =
Some(OgImage { url: resolved_url, ..Default::default() });
}
"description" => og.description = Some(content),
"site_name" => og.site_name = Some(content),
"locale" => og.locale = Some(content),
// Handle nested properties
_ if prop.starts_with("image:") => {
if let Some(ref mut img) = current_image {
match &prop[6..] {
"secure_url" => img.secure_url = Some(content),
"type" => img.r#type = Some(content),
"width" => img.width = content.parse().ok(),
"height" => img.height = content.parse().ok(),
"alt" => img.alt = Some(content),
_ => {}
}
}
}
_ if prop.starts_with("video:") => match &prop[6..] {
"secure_url" => {
if let Some(ref mut video) = current_video {
video.secure_url = Some(content);
}
}
"type" => {
if let Some(ref mut video) = current_video {
video.r#type = Some(content);
}
}
"width" => {
if let Some(ref mut video) = current_video {
video.width = content.parse().ok();
}
}
"height" => {
if let Some(ref mut video) = current_video {
video.height = content.parse().ok();
}
}
_ => {}
},
_ if prop.starts_with("audio:") => match &prop[6..] {
"secure_url" => {
if let Some(ref mut audio) = current_audio {
audio.secure_url = Some(content);
}
}
"type" => {
if let Some(ref mut audio) = current_audio {
audio.r#type = Some(content);
}
}
_ => {}
},
_ if prop.starts_with("locale:") => {
if &prop[7..] == "alternate" {
og.locale_alternate.push(content);
}
}
"video" => {
// Save previous video if exists
if let Some(video) = current_video.take() {
og.videos.push(video);
}
let resolved_url =
url_utils::resolve_url(base_url, &content).unwrap_or(content);
// Start new video
current_video =
Some(OgVideo { url: resolved_url, ..Default::default() });
}
"audio" => {
// Save previous audio if exists
if let Some(audio) = current_audio.take() {
og.audios.push(audio);
}
let resolved_url =
url_utils::resolve_url(base_url, &content).unwrap_or(content);
// Start new audio
current_audio =
Some(OgAudio { url: resolved_url, ..Default::default() });
}
_ => {}
}
} else if let Some(prop) = property.strip_prefix("article:") {
has_article_data = true;
match prop {
"published_time" => article_data.published_time = Some(content),
"modified_time" => article_data.modified_time = Some(content),
"expiration_time" => article_data.expiration_time = Some(content),
"author" => article_data.author.push(content),
"section" => article_data.section = Some(content),
"tag" => article_data.tag.push(content),
_ => {}
}
} else if let Some(prop) = property.strip_prefix("book:") {
has_book_data = true;
match prop {
"author" => book_data.author.push(content),
"isbn" => book_data.isbn = Some(content),
"release_date" => book_data.release_date = Some(content),
"tag" => book_data.tag.push(content),
_ => {}
}
} else if let Some(prop) = property.strip_prefix("profile:") {
has_profile_data = true;
match prop {
"first_name" => profile_data.first_name = Some(content),
"last_name" => profile_data.last_name = Some(content),
"username" => profile_data.username = Some(content),
"gender" => profile_data.gender = Some(content),
_ => {}
}
} else if let Some(prop) = property.strip_prefix("fb:") {
// Phase 6: Facebook platform integration
match prop {
"app_id" => og.fb_app_id = Some(content),
"admins" => og.fb_admins = Some(content),
_ => {}
}
}
}
}
}
// Save final image/video/audio if exists
if let Some(img) = current_image {
og.images.push(img);
}
if let Some(video) = current_video {
og.videos.push(video);
}
if let Some(audio) = current_audio {
og.audios.push(audio);
}
// Set type-specific metadata if any was found
if has_article_data {
og.article = Some(article_data);
}
if has_book_data {
og.book = Some(book_data);
}
if has_profile_data {
og.profile = Some(profile_data);
}
Ok(og)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_basic() {
let html = r#"<meta property="og:title" content="Test">"#;
let og = extract(html, None).unwrap();
assert_eq!(og.title, Some("Test".to_string()));
}
#[test]
fn test_extract_empty() {
let html = "";
let og = extract(html, None).unwrap();
assert_eq!(og.title, None);
}
// Phase 6: Facebook Platform Tests
#[test]
fn test_facebook_app_id() {
let html = r#"<meta property="fb:app_id" content="123456789">"#;
let og = extract(html, None).unwrap();
assert_eq!(og.fb_app_id, Some("123456789".to_string()));
}
#[test]
fn test_facebook_admins() {
let html = r#"<meta property="fb:admins" content="user1,user2,user3">"#;
let og = extract(html, None).unwrap();
assert_eq!(og.fb_admins, Some("user1,user2,user3".to_string()));
}
#[test]
fn test_facebook_platform_with_og() {
let html = r#"
<meta property="og:title" content="Test Page">
<meta property="fb:app_id" content="987654321">
<meta property="fb:admins" content="admin1,admin2">
"#;
let og = extract(html, None).unwrap();
assert_eq!(og.title, Some("Test Page".to_string()));
assert_eq!(og.fb_app_id, Some("987654321".to_string()));
assert_eq!(og.fb_admins, Some("admin1,admin2".to_string()));
}
}