halldyll-media 0.1.0

Media extraction (images, videos, links) for halldyll scraper
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Video extraction for halldyll-media
//!
//! Extracts videos from HTML with support for:
//! - HTML5 video elements
//! - Embedded players (YouTube, Vimeo, Dailymotion, etc.)
//! - Video sources and tracks (subtitles)
//! - Schema.org VideoObject

use lazy_static::lazy_static;
use regex::Regex;
use scraper::{Html, Selector, ElementRef};
use std::collections::HashSet;
use url::Url;

use crate::types::{
    VideoMedia, VideoPlatform, VideoSource, VideoTrack, TrackKind, MediaResult,
};

lazy_static! {
    /// YouTube video ID pattern
    static ref YOUTUBE_ID: Regex = Regex::new(
        r"(?:youtube\.com/(?:watch\?v=|embed/|v/)|youtu\.be/)([a-zA-Z0-9_-]{11})"
    ).unwrap();
    
    /// Vimeo video ID pattern
    static ref VIMEO_ID: Regex = Regex::new(
        r"vimeo\.com/(?:video/)?(\d+)"
    ).unwrap();
    
    /// Dailymotion video ID pattern
    static ref DAILYMOTION_ID: Regex = Regex::new(
        r"dailymotion\.com/(?:video|embed/video)/([a-zA-Z0-9]+)"
    ).unwrap();
    
    /// Twitch channel/video pattern
    static ref TWITCH_ID: Regex = Regex::new(
        r"twitch\.tv/(?:videos/(\d+)|([a-zA-Z0-9_]+))"
    ).unwrap();
    
    /// TikTok video ID pattern
    static ref TIKTOK_ID: Regex = Regex::new(
        r"tiktok\.com/@[^/]+/video/(\d+)"
    ).unwrap();
}

// ============================================================================
// EXTRACTION FUNCTIONS
// ============================================================================

/// Extract all videos from HTML document
pub fn extract_videos(document: &Html, base_url: Option<&Url>) -> Vec<VideoMedia> {
    let mut videos = Vec::new();
    let mut seen_urls: HashSet<String> = HashSet::new();
    
    // Extract HTML5 video elements
    if let Ok(sel) = Selector::parse("video") {
        for el in document.select(&sel) {
            if let Some(video) = extract_video_element(&el, base_url) {
                let key = video.absolute_url.as_ref().unwrap_or(&video.src).clone();
                if seen_urls.insert(key) {
                    videos.push(video);
                }
            }
        }
    }
    
    // Extract embedded videos from iframes
    if let Ok(sel) = Selector::parse("iframe[src]") {
        for el in document.select(&sel) {
            if let Some(src) = el.value().attr("src") {
                if is_video_embed(src) {
                    if let Some(video) = extract_embedded_video(&el, base_url) {
                        let key = video.absolute_url.as_ref().unwrap_or(&video.src).clone();
                        if seen_urls.insert(key) {
                            videos.push(video);
                        }
                    }
                }
            }
        }
    }
    
    // Extract from object/embed tags
    if let Ok(sel) = Selector::parse("object[data], embed[src]") {
        for el in document.select(&sel) {
            let src = el.value().attr("data").or_else(|| el.value().attr("src"));
            if let Some(src) = src {
                if is_video_url(src) {
                    if let Some(video) = create_video_from_url(src, &el, base_url) {
                        let key = video.absolute_url.as_ref().unwrap_or(&video.src).clone();
                        if seen_urls.insert(key) {
                            videos.push(video);
                        }
                    }
                }
            }
        }
    }
    
    videos
}

/// Extract HTML5 video element
fn extract_video_element(el: &ElementRef, base_url: Option<&Url>) -> Option<VideoMedia> {
    let src = el.value().attr("src")
        .or_else(|| {
            // Try to get from first source element
            if let Ok(sel) = Selector::parse("source") {
                el.select(&sel).next()
                    .and_then(|s| s.value().attr("src"))
            } else {
                None
            }
        })?;
    
    let absolute_url = resolve_url(src, base_url);
    
    let mut video = VideoMedia {
        src: src.to_string(),
        absolute_url,
        platform: VideoPlatform::Html5,
        ..Default::default()
    };
    
    // Parse attributes
    video.poster = el.value().attr("poster")
        .and_then(|p| resolve_url(p, base_url));
    video.width = el.value().attr("width")
        .and_then(|w| w.trim_end_matches("px").parse().ok());
    video.height = el.value().attr("height")
        .and_then(|h| h.trim_end_matches("px").parse().ok());
    video.autoplay = el.value().attr("autoplay").is_some();
    video.loop_video = el.value().attr("loop").is_some();
    video.muted = el.value().attr("muted").is_some();
    video.controls = el.value().attr("controls").is_some();
    video.playsinline = el.value().attr("playsinline").is_some();
    
    // Get MIME type
    video.mime_type = el.value().attr("type").map(|s| s.to_string())
        .or_else(|| guess_video_mime(&video.src));
    
    // Extract sources
    video.sources = extract_video_sources(el, base_url);
    
    // Extract tracks
    video.tracks = extract_video_tracks(el, base_url);
    
    // Get title from various sources
    video.title = el.value().attr("title").map(|s| s.to_string())
        .or_else(|| el.value().attr("aria-label").map(|s| s.to_string()));
    
    Some(video)
}

/// Extract video sources from video element
fn extract_video_sources(video: &ElementRef, base_url: Option<&Url>) -> Vec<VideoSource> {
    let mut sources = Vec::new();
    
    if let Ok(sel) = Selector::parse("source") {
        for source in video.select(&sel) {
            if let Some(src) = source.value().attr("src") {
                sources.push(VideoSource {
                    src: resolve_url(src, base_url).unwrap_or_else(|| src.to_string()),
                    mime_type: source.value().attr("type").map(|s| s.to_string()),
                    quality: source.value().attr("data-quality")
                        .or_else(|| source.value().attr("label"))
                        .map(|s| s.to_string()),
                });
            }
        }
    }
    
    sources
}

/// Extract video tracks (subtitles, captions)
fn extract_video_tracks(video: &ElementRef, base_url: Option<&Url>) -> Vec<VideoTrack> {
    let mut tracks = Vec::new();
    
    if let Ok(sel) = Selector::parse("track") {
        for track in video.select(&sel) {
            if let Some(src) = track.value().attr("src") {
                let kind = match track.value().attr("kind") {
                    Some("captions") => TrackKind::Captions,
                    Some("descriptions") => TrackKind::Descriptions,
                    Some("chapters") => TrackKind::Chapters,
                    Some("metadata") => TrackKind::Metadata,
                    _ => TrackKind::Subtitles,
                };
                
                tracks.push(VideoTrack {
                    src: resolve_url(src, base_url).unwrap_or_else(|| src.to_string()),
                    kind,
                    label: track.value().attr("label").map(|s| s.to_string()),
                    srclang: track.value().attr("srclang").map(|s| s.to_string()),
                    is_default: track.value().attr("default").is_some(),
                });
            }
        }
    }
    
    tracks
}

/// Extract embedded video from iframe
fn extract_embedded_video(el: &ElementRef, base_url: Option<&Url>) -> Option<VideoMedia> {
    let src = el.value().attr("src")?;
    let platform = VideoPlatform::from_url(src);
    
    let mut video = VideoMedia {
        src: src.to_string(),
        absolute_url: resolve_url(src, base_url),
        platform,
        embed_url: Some(src.to_string()),
        ..Default::default()
    };
    
    // Extract video ID based on platform
    video.video_id = extract_video_id(src, platform);
    
    // Parse dimensions
    video.width = el.value().attr("width")
        .and_then(|w| w.trim_end_matches("px").parse().ok());
    video.height = el.value().attr("height")
        .and_then(|h| h.trim_end_matches("px").parse().ok());
    
    // Get title
    video.title = el.value().attr("title").map(|s| s.to_string());
    
    // Generate thumbnail URL
    video.poster = generate_thumbnail_url(&video);
    
    Some(video)
}

/// Create video from URL
fn create_video_from_url(src: &str, el: &ElementRef, base_url: Option<&Url>) -> Option<VideoMedia> {
    let platform = if is_video_embed(src) {
        VideoPlatform::from_url(src)
    } else {
        VideoPlatform::Html5
    };
    
    let mut video = VideoMedia {
        src: src.to_string(),
        absolute_url: resolve_url(src, base_url),
        platform,
        ..Default::default()
    };
    
    video.video_id = extract_video_id(src, platform);
    video.width = el.value().attr("width")
        .and_then(|w| w.parse().ok());
    video.height = el.value().attr("height")
        .and_then(|h| h.parse().ok());
    
    Some(video)
}

/// Extract video ID based on platform
fn extract_video_id(url: &str, platform: VideoPlatform) -> Option<String> {
    match platform {
        VideoPlatform::YouTube => {
            YOUTUBE_ID.captures(url)
                .and_then(|c| c.get(1))
                .map(|m| m.as_str().to_string())
        }
        VideoPlatform::Vimeo => {
            VIMEO_ID.captures(url)
                .and_then(|c| c.get(1))
                .map(|m| m.as_str().to_string())
        }
        VideoPlatform::Dailymotion => {
            DAILYMOTION_ID.captures(url)
                .and_then(|c| c.get(1))
                .map(|m| m.as_str().to_string())
        }
        VideoPlatform::Twitch => {
            TWITCH_ID.captures(url)
                .and_then(|c| c.get(1).or_else(|| c.get(2)))
                .map(|m| m.as_str().to_string())
        }
        VideoPlatform::TikTok => {
            TIKTOK_ID.captures(url)
                .and_then(|c| c.get(1))
                .map(|m| m.as_str().to_string())
        }
        _ => None,
    }
}

/// Generate thumbnail URL for embedded videos
fn generate_thumbnail_url(video: &VideoMedia) -> Option<String> {
    match video.platform {
        VideoPlatform::YouTube => {
            video.video_id.as_ref().map(|id| {
                format!("https://img.youtube.com/vi/{}/maxresdefault.jpg", id)
            })
        }
        VideoPlatform::Vimeo => {
            // Vimeo requires API call for thumbnail, return None
            None
        }
        VideoPlatform::Dailymotion => {
            video.video_id.as_ref().map(|id| {
                format!("https://www.dailymotion.com/thumbnail/video/{}", id)
            })
        }
        _ => None,
    }
}

/// Check if URL is a video embed
fn is_video_embed(url: &str) -> bool {
    let url_lower = url.to_lowercase();
    let video_hosts = [
        "youtube.com", "youtube-nocookie.com", "youtu.be",
        "vimeo.com", "player.vimeo.com",
        "dailymotion.com", "dai.ly",
        "twitch.tv", "player.twitch.tv",
        "facebook.com/plugins/video",
        "tiktok.com",
        "wistia.com", "wistia.net",
        "brightcove",
        "jwplayer", "jwplatform",
    ];
    
    video_hosts.iter().any(|host| url_lower.contains(host))
}

/// Check if URL is a direct video file
fn is_video_url(url: &str) -> bool {
    let url_lower = url.to_lowercase();
    let video_extensions = [".mp4", ".webm", ".ogg", ".ogv", ".avi", ".mov", ".mkv", ".m4v"];
    
    video_extensions.iter().any(|ext| url_lower.contains(ext))
        || is_video_embed(url)
}

/// Guess video MIME type from URL
fn guess_video_mime(url: &str) -> Option<String> {
    let url_lower = url.to_lowercase();
    
    if url_lower.contains(".mp4") {
        Some("video/mp4".to_string())
    } else if url_lower.contains(".webm") {
        Some("video/webm".to_string())
    } else if url_lower.contains(".ogg") || url_lower.contains(".ogv") {
        Some("video/ogg".to_string())
    } else if url_lower.contains(".mov") {
        Some("video/quicktime".to_string())
    } else if url_lower.contains(".avi") {
        Some("video/x-msvideo".to_string())
    } else {
        None
    }
}

/// Resolve relative URL
fn resolve_url(href: &str, base_url: Option<&Url>) -> Option<String> {
    if href.starts_with("http://") || href.starts_with("https://") {
        return Some(href.to_string());
    }
    
    if href.starts_with("//") {
        return Some(format!("https:{}", href));
    }
    
    base_url.and_then(|base| base.join(href).ok().map(|u| u.to_string()))
}

// ============================================================================
// CONVENIENCE FUNCTIONS
// ============================================================================

/// Extract videos from HTML string
pub fn extract_videos_from_html(html: &str, base_url: Option<&str>) -> MediaResult<Vec<VideoMedia>> {
    let document = Html::parse_document(html);
    let base = base_url.and_then(|u| Url::parse(u).ok());
    Ok(extract_videos(&document, base.as_ref()))
}

/// Get all video URLs from HTML
pub fn get_video_urls(html: &str, base_url: Option<&str>) -> Vec<String> {
    extract_videos_from_html(html, base_url)
        .unwrap_or_default()
        .into_iter()
        .filter_map(|v| v.absolute_url)
        .collect()
}

/// Check if HTML has videos
pub fn has_videos(document: &Html) -> bool {
    if let Ok(sel) = Selector::parse("video, iframe[src*='youtube'], iframe[src*='vimeo']") {
        document.select(&sel).next().is_some()
    } else {
        false
    }
}

/// Get YouTube embed URL from video ID
pub fn youtube_embed_url(video_id: &str) -> String {
    format!("https://www.youtube.com/embed/{}", video_id)
}

/// Get Vimeo embed URL from video ID
pub fn vimeo_embed_url(video_id: &str) -> String {
    format!("https://player.vimeo.com/video/{}", video_id)
}

/// Get YouTube thumbnail URL
pub fn youtube_thumbnail(video_id: &str, quality: &str) -> String {
    // quality: default, mqdefault, hqdefault, sddefault, maxresdefault
    format!("https://img.youtube.com/vi/{}/{}.jpg", video_id, quality)
}

// ============================================================================
// TESTS
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn parse_html(html: &str) -> Html {
        Html::parse_document(html)
    }

    #[test]
    fn test_extract_html5_video() {
        let html = r#"
            <video src="/videos/test.mp4" width="640" height="360" controls>
                <source src="/videos/test.webm" type="video/webm">
                <track src="/subtitles.vtt" kind="subtitles" srclang="en" label="English">
            </video>
        "#;
        let doc = parse_html(html);
        let base = Url::parse("https://example.com").unwrap();
        let videos = extract_videos(&doc, Some(&base));
        
        assert_eq!(videos.len(), 1);
        assert_eq!(videos[0].platform, VideoPlatform::Html5);
        assert!(videos[0].controls);
        assert_eq!(videos[0].width, Some(640));
        assert!(!videos[0].sources.is_empty());
        assert!(!videos[0].tracks.is_empty());
    }

    #[test]
    fn test_extract_youtube_embed() {
        let html = r#"
            <iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" 
                    width="560" height="315" title="Test Video">
            </iframe>
        "#;
        let doc = parse_html(html);
        let videos = extract_videos(&doc, None);
        
        assert_eq!(videos.len(), 1);
        assert_eq!(videos[0].platform, VideoPlatform::YouTube);
        assert_eq!(videos[0].video_id, Some("dQw4w9WgXcQ".to_string()));
        assert_eq!(videos[0].title, Some("Test Video".to_string()));
        assert!(videos[0].poster.is_some()); // YouTube thumbnail
    }

    #[test]
    fn test_extract_vimeo_embed() {
        let html = r#"
            <iframe src="https://player.vimeo.com/video/123456789" 
                    width="640" height="360">
            </iframe>
        "#;
        let doc = parse_html(html);
        let videos = extract_videos(&doc, None);
        
        assert_eq!(videos.len(), 1);
        assert_eq!(videos[0].platform, VideoPlatform::Vimeo);
        assert_eq!(videos[0].video_id, Some("123456789".to_string()));
    }

    #[test]
    fn test_youtube_video_id_extraction() {
        let urls = [
            ("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "dQw4w9WgXcQ"),
            ("https://youtu.be/dQw4w9WgXcQ", "dQw4w9WgXcQ"),
            ("https://www.youtube.com/embed/dQw4w9WgXcQ", "dQw4w9WgXcQ"),
        ];
        
        for (url, expected_id) in urls {
            let id = extract_video_id(url, VideoPlatform::YouTube);
            assert_eq!(id, Some(expected_id.to_string()), "Failed for URL: {}", url);
        }
    }

    #[test]
    fn test_video_sources() {
        let html = r#"
            <video>
                <source src="video.mp4" type="video/mp4">
                <source src="video.webm" type="video/webm">
                <source src="video.ogg" type="video/ogg">
            </video>
        "#;
        let doc = parse_html(html);
        let videos = extract_videos(&doc, None);
        
        assert_eq!(videos[0].sources.len(), 3);
    }

    #[test]
    fn test_video_tracks() {
        let html = r#"
            <video src="test.mp4">
                <track kind="subtitles" src="en.vtt" srclang="en" label="English" default>
                <track kind="subtitles" src="fr.vtt" srclang="fr" label="French">
                <track kind="captions" src="cc.vtt" srclang="en" label="CC">
            </video>
        "#;
        let doc = parse_html(html);
        let videos = extract_videos(&doc, None);
        
        assert_eq!(videos[0].tracks.len(), 3);
        assert!(videos[0].tracks[0].is_default);
        assert_eq!(videos[0].tracks[2].kind, TrackKind::Captions);
    }

    #[test]
    fn test_video_attributes() {
        let html = r#"<video src="test.mp4" autoplay loop muted playsinline></video>"#;
        let doc = parse_html(html);
        let videos = extract_videos(&doc, None);
        
        assert!(videos[0].autoplay);
        assert!(videos[0].loop_video);
        assert!(videos[0].muted);
        assert!(videos[0].playsinline);
    }

    #[test]
    fn test_has_videos() {
        let with_video = "<video src='test.mp4'></video>";
        let with_youtube = "<iframe src='https://youtube.com/embed/abc'></iframe>";
        let without = "<p>No video</p>";
        
        assert!(has_videos(&parse_html(with_video)));
        assert!(has_videos(&parse_html(with_youtube)));
        assert!(!has_videos(&parse_html(without)));
    }

    #[test]
    fn test_youtube_thumbnail_url() {
        let thumb = youtube_thumbnail("dQw4w9WgXcQ", "maxresdefault");
        assert_eq!(thumb, "https://img.youtube.com/vi/dQw4w9WgXcQ/maxresdefault.jpg");
    }

    #[test]
    fn test_is_video_embed() {
        assert!(is_video_embed("https://www.youtube.com/embed/abc"));
        assert!(is_video_embed("https://player.vimeo.com/video/123"));
        assert!(is_video_embed("https://www.dailymotion.com/embed/video/abc"));
        assert!(!is_video_embed("https://example.com/page"));
    }
}