meta_oxide 0.1.1

Universal metadata extraction library supporting 13 formats (HTML Meta, Open Graph, Twitter Cards, JSON-LD, Microdata, Microformats, RDFa, Dublin Core, Web App Manifest, oEmbed, rel-links, Images, SEO) with 7 language bindings
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! Types for social media tags (Phase 2)
//!
//! This module defines types for Open Graph Protocol and Twitter Cards,
//! which control how links appear when shared on social media platforms.
//!
//! - **Open Graph**: Used by Facebook, LinkedIn, WhatsApp, Slack, Discord (60%+ adoption)
//! - **Twitter Cards**: Used by Twitter/X for link previews (45% adoption)

use pyo3::prelude::*;
use pyo3::types::PyDict;
use serde::{Deserialize, Serialize};

/// Open Graph Protocol data (Facebook, LinkedIn, WhatsApp, Slack, Discord)
///
/// 60%+ of websites use Open Graph to control link preview appearance.
/// Specification: https://ogp.me/
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct OpenGraph {
    // Basic metadata (required by spec)
    /// The title of the object as it should appear in the graph
    pub title: Option<String>,
    /// The type of object (article, website, video.movie, music.song, etc.)
    pub r#type: Option<String>,
    /// The canonical URL of the object
    pub url: Option<String>,
    /// Primary image URL that should represent the object
    pub image: Option<String>,

    // Optional metadata
    /// A one to two sentence description of the object
    pub description: Option<String>,
    /// The name of the overall site (e.g., "IMDb", "Wikipedia")
    pub site_name: Option<String>,
    /// The locale tag (e.g., "en_US", "es_ES")
    pub locale: Option<String>,
    /// Array of alternate locales this page is available in
    pub locale_alternate: Vec<String>,

    // Image metadata
    /// All images with full metadata (width, height, type, alt text)
    pub images: Vec<OgImage>,

    // Video metadata
    /// Video objects associated with this content
    pub videos: Vec<OgVideo>,

    // Audio metadata
    /// Audio objects associated with this content
    pub audios: Vec<OgAudio>,

    // Type-specific metadata
    /// Article-specific metadata (when type="article")
    pub article: Option<OgArticle>,
    /// Book-specific metadata (when type="book")
    pub book: Option<OgBook>,
    /// Profile-specific metadata (when type="profile")
    pub profile: Option<OgProfile>,

    // Platform integration (Phase 6)
    /// Facebook App ID for platform integration
    pub fb_app_id: Option<String>,
    /// Facebook Admin user IDs (comma-separated)
    pub fb_admins: Option<String>,
}

/// Open Graph Image with full metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct OgImage {
    /// URL of the image
    pub url: String,
    /// HTTPS version of the image URL
    pub secure_url: Option<String>,
    /// MIME type of the image (e.g., "image/jpeg", "image/png")
    pub r#type: Option<String>,
    /// Width in pixels
    pub width: Option<u32>,
    /// Height in pixels
    pub height: Option<u32>,
    /// Alt text for accessibility
    pub alt: Option<String>,
}

/// Open Graph Video metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct OgVideo {
    /// URL of the video
    pub url: String,
    /// HTTPS version of the video URL
    pub secure_url: Option<String>,
    /// MIME type of the video (e.g., "video/mp4", "application/x-shockwave-flash")
    pub r#type: Option<String>,
    /// Width in pixels
    pub width: Option<u32>,
    /// Height in pixels
    pub height: Option<u32>,
}

/// Open Graph Audio metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct OgAudio {
    /// URL of the audio file
    pub url: String,
    /// HTTPS version of the audio URL
    pub secure_url: Option<String>,
    /// MIME type of the audio (e.g., "audio/mpeg", "audio/vorbis")
    pub r#type: Option<String>,
}

/// Article-specific Open Graph metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct OgArticle {
    /// ISO 8601 datetime when the article was published
    pub published_time: Option<String>,
    /// ISO 8601 datetime when the article was last modified
    pub modified_time: Option<String>,
    /// ISO 8601 datetime when the article will expire
    pub expiration_time: Option<String>,
    /// URLs to author profile pages
    pub author: Vec<String>,
    /// High-level section name (e.g., "Technology", "Sports")
    pub section: Option<String>,
    /// Keywords/tags associated with the article
    pub tag: Vec<String>,
}

/// Book-specific Open Graph metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct OgBook {
    /// URLs to author profile pages
    pub author: Vec<String>,
    /// ISBN number
    pub isbn: Option<String>,
    /// Date the book was released
    pub release_date: Option<String>,
    /// Keywords/tags associated with the book
    pub tag: Vec<String>,
}

/// Profile-specific Open Graph metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct OgProfile {
    /// First name
    pub first_name: Option<String>,
    /// Last name
    pub last_name: Option<String>,
    /// Username
    pub username: Option<String>,
    /// Gender
    pub gender: Option<String>,
}

/// Twitter Card data (45% adoption)
///
/// Twitter Cards control how links appear on Twitter/X.
/// Falls back to Open Graph when Twitter-specific tags are missing.
/// Specification: https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/abouts-cards
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct TwitterCard {
    /// Card type: "summary", "summary_large_image", "app", or "player"
    pub card: Option<String>,

    // Content metadata
    /// Title of the content (max 70 characters)
    pub title: Option<String>,
    /// Description of the content (max 200 characters)
    pub description: Option<String>,
    /// URL of the image to display
    pub image: Option<String>,
    /// Alt text for the image
    pub image_alt: Option<String>,

    // Site and creator
    /// @username of the website (e.g., "@nytimes")
    pub site: Option<String>,
    /// Twitter user ID of the website
    pub site_id: Option<String>,
    /// @username of the content creator
    pub creator: Option<String>,
    /// Twitter user ID of the content creator
    pub creator_id: Option<String>,

    // Type-specific metadata
    /// App card specific metadata
    pub app: Option<TwitterApp>,
    /// Player card specific metadata (video/audio)
    pub player: Option<TwitterPlayer>,
}

/// Twitter App card metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct TwitterApp {
    // iPhone app
    /// Name of the iPhone app
    pub name_iphone: Option<String>,
    /// App ID in the App Store
    pub id_iphone: Option<String>,
    /// Custom URL scheme for iPhone app
    pub url_iphone: Option<String>,

    // iPad app
    /// Name of the iPad app
    pub name_ipad: Option<String>,
    /// App ID in the App Store
    pub id_ipad: Option<String>,
    /// Custom URL scheme for iPad app
    pub url_ipad: Option<String>,

    // Google Play app
    /// Name of the Android app
    pub name_googleplay: Option<String>,
    /// App ID in Google Play
    pub id_googleplay: Option<String>,
    /// Custom URL scheme for Android app
    pub url_googleplay: Option<String>,

    /// Country code if app is only available in specific countries
    pub country: Option<String>,
}

/// Twitter Player card metadata (video/audio)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct TwitterPlayer {
    /// HTTPS URL to iframe player
    pub url: String,
    /// Width of iframe in pixels
    pub width: Option<u32>,
    /// Height of iframe in pixels
    pub height: Option<u32>,
    /// URL to raw video/audio stream (MP4, etc.)
    pub stream: Option<String>,
}

// Python conversion implementations

impl OpenGraph {
    /// Convert OpenGraph to Python dictionary
    pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
        let dict = PyDict::new_bound(py);

        // Basic metadata
        if let Some(ref v) = self.title {
            let _ = dict.set_item("title", v);
        }
        if let Some(ref v) = self.r#type {
            let _ = dict.set_item("type", v);
        }
        if let Some(ref v) = self.url {
            let _ = dict.set_item("url", v);
        }
        if let Some(ref v) = self.image {
            let _ = dict.set_item("image", v);
        }
        if let Some(ref v) = self.description {
            let _ = dict.set_item("description", v);
        }
        if let Some(ref v) = self.site_name {
            let _ = dict.set_item("site_name", v);
        }
        if let Some(ref v) = self.locale {
            let _ = dict.set_item("locale", v);
        }

        // Lists and complex types
        if !self.locale_alternate.is_empty() {
            let _ = dict.set_item("locale_alternate", self.locale_alternate.clone());
        }
        if !self.images.is_empty() {
            let images: Vec<_> = self.images.iter().map(|img| img.to_py_dict(py)).collect();
            let _ = dict.set_item("images", images);
        }
        if !self.videos.is_empty() {
            let videos: Vec<_> = self.videos.iter().map(|v| v.to_py_dict(py)).collect();
            let _ = dict.set_item("videos", videos);
        }
        if !self.audios.is_empty() {
            let audios: Vec<_> = self.audios.iter().map(|a| a.to_py_dict(py)).collect();
            let _ = dict.set_item("audios", audios);
        }
        if let Some(ref article) = self.article {
            let _ = dict.set_item("article", article.to_py_dict(py));
        }
        if let Some(ref book) = self.book {
            let _ = dict.set_item("book", book.to_py_dict(py));
        }
        if let Some(ref profile) = self.profile {
            let _ = dict.set_item("profile", profile.to_py_dict(py));
        }

        // Platform integration (Phase 6)
        if let Some(ref v) = self.fb_app_id {
            let _ = dict.set_item("fb_app_id", v);
        }
        if let Some(ref v) = self.fb_admins {
            let _ = dict.set_item("fb_admins", v);
        }

        dict.unbind()
    }
}

impl OgImage {
    /// Convert OgImage to Python dictionary
    pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
        let dict = PyDict::new_bound(py);
        let _ = dict.set_item("url", &self.url);
        if let Some(ref v) = self.secure_url {
            let _ = dict.set_item("secure_url", v);
        }
        if let Some(ref v) = self.r#type {
            let _ = dict.set_item("type", v);
        }
        if let Some(v) = self.width {
            let _ = dict.set_item("width", v);
        }
        if let Some(v) = self.height {
            let _ = dict.set_item("height", v);
        }
        if let Some(ref v) = self.alt {
            let _ = dict.set_item("alt", v);
        }
        dict.unbind()
    }
}

impl OgVideo {
    /// Convert OgVideo to Python dictionary
    pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
        let dict = PyDict::new_bound(py);
        let _ = dict.set_item("url", &self.url);
        if let Some(ref v) = self.secure_url {
            let _ = dict.set_item("secure_url", v);
        }
        if let Some(ref v) = self.r#type {
            let _ = dict.set_item("type", v);
        }
        if let Some(v) = self.width {
            let _ = dict.set_item("width", v);
        }
        if let Some(v) = self.height {
            let _ = dict.set_item("height", v);
        }
        dict.unbind()
    }
}

impl OgAudio {
    /// Convert OgAudio to Python dictionary
    pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
        let dict = PyDict::new_bound(py);
        let _ = dict.set_item("url", &self.url);
        if let Some(ref v) = self.secure_url {
            let _ = dict.set_item("secure_url", v);
        }
        if let Some(ref v) = self.r#type {
            let _ = dict.set_item("type", v);
        }
        dict.unbind()
    }
}

impl OgArticle {
    /// Convert OgArticle to Python dictionary
    pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
        let dict = PyDict::new_bound(py);
        if let Some(ref v) = self.published_time {
            let _ = dict.set_item("published_time", v);
        }
        if let Some(ref v) = self.modified_time {
            let _ = dict.set_item("modified_time", v);
        }
        if let Some(ref v) = self.expiration_time {
            let _ = dict.set_item("expiration_time", v);
        }
        if !self.author.is_empty() {
            let _ = dict.set_item("author", self.author.clone());
        }
        if let Some(ref v) = self.section {
            let _ = dict.set_item("section", v);
        }
        if !self.tag.is_empty() {
            let _ = dict.set_item("tag", self.tag.clone());
        }
        dict.unbind()
    }
}

impl OgBook {
    /// Convert OgBook to Python dictionary
    pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
        let dict = PyDict::new_bound(py);
        if !self.author.is_empty() {
            let _ = dict.set_item("author", self.author.clone());
        }
        if let Some(ref v) = self.isbn {
            let _ = dict.set_item("isbn", v);
        }
        if let Some(ref v) = self.release_date {
            let _ = dict.set_item("release_date", v);
        }
        if !self.tag.is_empty() {
            let _ = dict.set_item("tag", self.tag.clone());
        }
        dict.unbind()
    }
}

impl OgProfile {
    /// Convert OgProfile to Python dictionary
    pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
        let dict = PyDict::new_bound(py);
        if let Some(ref v) = self.first_name {
            let _ = dict.set_item("first_name", v);
        }
        if let Some(ref v) = self.last_name {
            let _ = dict.set_item("last_name", v);
        }
        if let Some(ref v) = self.username {
            let _ = dict.set_item("username", v);
        }
        if let Some(ref v) = self.gender {
            let _ = dict.set_item("gender", v);
        }
        dict.unbind()
    }
}

impl TwitterCard {
    /// Convert TwitterCard to Python dictionary
    pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
        let dict = PyDict::new_bound(py);

        if let Some(ref v) = self.card {
            let _ = dict.set_item("card", v);
        }
        if let Some(ref v) = self.title {
            let _ = dict.set_item("title", v);
        }
        if let Some(ref v) = self.description {
            let _ = dict.set_item("description", v);
        }
        if let Some(ref v) = self.image {
            let _ = dict.set_item("image", v);
        }
        if let Some(ref v) = self.image_alt {
            let _ = dict.set_item("image_alt", v);
        }
        if let Some(ref v) = self.site {
            let _ = dict.set_item("site", v);
        }
        if let Some(ref v) = self.site_id {
            let _ = dict.set_item("site_id", v);
        }
        if let Some(ref v) = self.creator {
            let _ = dict.set_item("creator", v);
        }
        if let Some(ref v) = self.creator_id {
            let _ = dict.set_item("creator_id", v);
        }

        // Complex types
        if let Some(ref app) = self.app {
            let _ = dict.set_item("app", app.to_py_dict(py));
        }
        if let Some(ref player) = self.player {
            let _ = dict.set_item("player", player.to_py_dict(py));
        }

        dict.unbind()
    }
}

impl TwitterApp {
    /// Convert TwitterApp to Python dictionary
    pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
        let dict = PyDict::new_bound(py);
        if let Some(ref v) = self.name_iphone {
            let _ = dict.set_item("name_iphone", v);
        }
        if let Some(ref v) = self.id_iphone {
            let _ = dict.set_item("id_iphone", v);
        }
        if let Some(ref v) = self.url_iphone {
            let _ = dict.set_item("url_iphone", v);
        }
        if let Some(ref v) = self.name_ipad {
            let _ = dict.set_item("name_ipad", v);
        }
        if let Some(ref v) = self.id_ipad {
            let _ = dict.set_item("id_ipad", v);
        }
        if let Some(ref v) = self.url_ipad {
            let _ = dict.set_item("url_ipad", v);
        }
        if let Some(ref v) = self.name_googleplay {
            let _ = dict.set_item("name_googleplay", v);
        }
        if let Some(ref v) = self.id_googleplay {
            let _ = dict.set_item("id_googleplay", v);
        }
        if let Some(ref v) = self.url_googleplay {
            let _ = dict.set_item("url_googleplay", v);
        }
        if let Some(ref v) = self.country {
            let _ = dict.set_item("country", v);
        }
        dict.unbind()
    }
}

impl TwitterPlayer {
    /// Convert TwitterPlayer to Python dictionary
    pub fn to_py_dict(&self, py: Python) -> Py<PyDict> {
        let dict = PyDict::new_bound(py);
        let _ = dict.set_item("url", &self.url);
        if let Some(v) = self.width {
            let _ = dict.set_item("width", v);
        }
        if let Some(v) = self.height {
            let _ = dict.set_item("height", v);
        }
        if let Some(ref v) = self.stream {
            let _ = dict.set_item("stream", v);
        }
        dict.unbind()
    }
}