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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Image extraction for halldyll-media
//!
//! Extracts images from HTML with support for:
//! - Regular img tags
//! - Lazy loading (data-src, data-lazy-src, etc.)
//! - Responsive images (srcset, sizes)
//! - Picture elements with source sets
//! - Background images in inline styles
//! - Figure/figcaption associations

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

use crate::types::{
    ImageMedia, ImageFormat, ImageLoading, SrcsetEntry, MediaResult,
};

lazy_static! {
    /// Pattern for extracting srcset entries
    static ref SRCSET_ENTRY: Regex = Regex::new(
        r"([^\s,]+)\s*(\d+w|\d+(?:\.\d+)?x)?"
    ).unwrap();
    
    /// Pattern for data URLs
    static ref DATA_URL: Regex = Regex::new(r"^data:image/").unwrap();
    
    /// Pattern for placeholder images
    static ref PLACEHOLDER_PATTERN: Regex = Regex::new(
        r"(?i)(placeholder|blank|spacer|pixel|1x1|loading|lazy)"
    ).unwrap();
    
    /// Pattern for background-image in style
    static ref BG_IMAGE_PATTERN: Regex = Regex::new(
        r#"background(?:-image)?\s*:\s*url\(['"]?([^'")]+)['"]?\)"#
    ).unwrap();
    
    /// Common lazy loading data attributes
    static ref LAZY_ATTRS: Vec<&'static str> = vec![
        "data-src",
        "data-lazy-src",
        "data-original",
        "data-srcset",
        "data-lazy-srcset",
        "data-bg",
        "data-background",
        "data-image",
        "data-url",
    ];
}

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

/// Extract all images from HTML document
pub fn extract_images(document: &Html, base_url: Option<&Url>) -> Vec<ImageMedia> {
    let mut images = Vec::new();
    let mut seen_urls: HashSet<String> = HashSet::new();
    
    // Extract from <img> tags
    if let Ok(sel) = Selector::parse("img") {
        for el in document.select(&sel) {
            if let Some(img) = extract_image_element(&el, base_url) {
                let key = img.absolute_url.as_ref().unwrap_or(&img.src).clone();
                if seen_urls.insert(key) {
                    images.push(img);
                }
            }
        }
    }
    
    // Extract from <picture> elements
    if let Ok(sel) = Selector::parse("picture") {
        for el in document.select(&sel) {
            for img in extract_picture_element(&el, base_url) {
                let key = img.absolute_url.as_ref().unwrap_or(&img.src).clone();
                if seen_urls.insert(key) {
                    images.push(img);
                }
            }
        }
    }
    
    // Extract from <figure> elements (may have additional context)
    if let Ok(sel) = Selector::parse("figure img") {
        for el in document.select(&sel) {
            // Already extracted via img selector, but check for figcaption
            if let Some(src) = get_image_src(&el) {
                let abs_url = resolve_url(&src, base_url);
                let _key = abs_url.as_ref().unwrap_or(&src).clone();
                
                // Find and update with figcaption if exists
                if let Some(img) = images.iter_mut().find(|i| {
                    i.absolute_url.as_ref() == abs_url.as_ref() || i.src == src
                }) {
                    if img.alt.is_none() || img.alt.as_ref().map(|s| s.is_empty()).unwrap_or(true) {
                        img.alt = get_figcaption(&el);
                    }
                }
            }
        }
    }
    
    images
}

/// Extract a single image element
fn extract_image_element(el: &ElementRef, base_url: Option<&Url>) -> Option<ImageMedia> {
    let src = get_image_src(el)?;
    
    // Skip data URLs unless configured otherwise
    if DATA_URL.is_match(&src) {
        return None;
    }
    
    let absolute_url = resolve_url(&src, base_url);
    
    // Detect format from URL (try absolute first, then src)
    let format = absolute_url.as_ref()
        .and_then(|u| extract_extension(u))
        .or_else(|| extract_extension(&src))
        .map(|ext| ImageFormat::from_extension(&ext))
        .unwrap_or(ImageFormat::Unknown);
    
    // Parse dimensions
    let width = el.value().attr("width")
        .and_then(|w| w.trim_end_matches("px").parse().ok());
    let height = el.value().attr("height")
        .and_then(|h| h.trim_end_matches("px").parse().ok());
    
    // Get alt text
    let alt = el.value().attr("alt").map(|s| s.to_string());
    let is_decorative = alt.as_ref().map(|a| a.is_empty()).unwrap_or(false);
    
    // Detect loading strategy
    let loading = match el.value().attr("loading") {
        Some("lazy") => ImageLoading::Lazy,
        _ => ImageLoading::Eager,
    };
    
    // Parse srcset
    let srcset = el.value().attr("srcset")
        .map(|s| parse_srcset(s, base_url))
        .unwrap_or_default();
    
    // Check for lazy loading data attributes
    let data_src = LAZY_ATTRS.iter()
        .find_map(|attr| el.value().attr(attr))
        .map(|s| s.to_string());
    
    // Detect placeholder
    let is_placeholder = is_placeholder_image(&src, width, height);
    
    // Get classes and id
    let classes: Vec<String> = el.value().classes().map(|s| s.to_string()).collect();
    let id = el.value().attr("id").map(|s| s.to_string());
    
    // Get MIME type
    let mime_type = format.mime_type();
    
    Some(ImageMedia {
        src,
        absolute_url,
        alt,
        title: el.value().attr("title").map(|s| s.to_string()),
        width,
        height,
        format,
        mime_type: Some(mime_type.to_string()),
        loading,
        is_decorative,
        srcset,
        sizes: el.value().attr("sizes").map(|s| s.to_string()),
        data_src,
        is_placeholder,
        size_bytes: None,
        content_hash: None,
        classes,
        id,
    })
}

/// Extract images from picture element
fn extract_picture_element(picture: &ElementRef, base_url: Option<&Url>) -> Vec<ImageMedia> {
    let mut images = Vec::new();
    
    // Get the fallback img inside picture
    if let Ok(img_sel) = Selector::parse("img") {
        if let Some(img_el) = picture.select(&img_sel).next() {
            if let Some(mut img) = extract_image_element(&img_el, base_url) {
                // Add source alternatives
                if let Ok(source_sel) = Selector::parse("source") {
                    for source in picture.select(&source_sel) {
                        if let Some(srcset_str) = source.value().attr("srcset") {
                            let source_entries = parse_srcset(srcset_str, base_url);
                            img.srcset.extend(source_entries);
                        }
                    }
                }
                images.push(img);
            }
        }
    }
    
    images
}

/// Get image src from various attributes
fn get_image_src(el: &ElementRef) -> Option<String> {
    // Priority: src > data-src variants > currentSrc
    el.value().attr("src")
        .filter(|s| !s.is_empty() && !s.starts_with("data:image/svg+xml") || s.len() > 100)
        .or_else(|| {
            LAZY_ATTRS.iter()
                .find_map(|attr| el.value().attr(attr))
        })
        .map(|s| s.to_string())
}

/// Parse srcset attribute into entries
fn parse_srcset(srcset: &str, base_url: Option<&Url>) -> Vec<SrcsetEntry> {
    let mut entries = Vec::new();
    
    for part in srcset.split(',') {
        let part = part.trim();
        let parts: Vec<&str> = part.split_whitespace().collect();
        
        if parts.is_empty() {
            continue;
        }
        
        let url = parts[0].to_string();
        let resolved_url = resolve_url(&url, base_url).unwrap_or_else(|| url.clone());
        
        let mut width = None;
        let mut density = None;
        
        if parts.len() > 1 {
            let descriptor = parts[1];
            if descriptor.ends_with('w') {
                width = descriptor.trim_end_matches('w').parse().ok();
            } else if descriptor.ends_with('x') {
                density = descriptor.trim_end_matches('x').parse().ok();
            }
        }
        
        entries.push(SrcsetEntry {
            url: resolved_url,
            width,
            density,
        });
    }
    
    entries
}

/// Get figcaption text from parent figure
fn get_figcaption(img: &ElementRef) -> Option<String> {
    // Navigate up to find figure, then find figcaption
    if let Ok(_sel) = Selector::parse("figcaption") {
        // Check if there's a figure parent with figcaption
        let html = img.html();
        if html.contains("figure") {
            // Simple approach: parse the parent context
            // In practice, we'd need to traverse up the DOM
        }
    }
    None
}

/// Check if image is a placeholder
fn is_placeholder_image(src: &str, width: Option<u32>, height: Option<u32>) -> bool {
    // Check URL pattern
    if PLACEHOLDER_PATTERN.is_match(src) {
        return true;
    }
    
    // Check for tiny dimensions
    if let (Some(w), Some(h)) = (width, height) {
        if w <= 10 && h <= 10 {
            return true;
        }
    }
    
    // Check for known placeholder services
    let placeholders = [
        "placehold.it",
        "placeholder.com",
        "placekitten.com",
        "picsum.photos",
        "via.placeholder.com",
    ];
    
    placeholders.iter().any(|p| src.contains(p))
}

/// Extract file extension from URL
fn extract_extension(url: &str) -> Option<String> {
    // Remove query string and fragment
    let path = url.split('?').next()?.split('#').next()?;
    
    // Get last path segment
    let filename = path.rsplit('/').next()?;
    
    // Check if there's a dot in the filename
    if !filename.contains('.') {
        return None;
    }
    
    // Get extension
    let ext = filename.rsplit('.').next()?;
    
    // Validate it looks like an extension (not the whole filename)
    if ext != filename && ext.len() <= 5 && ext.chars().all(|c| c.is_alphanumeric()) {
        Some(ext.to_lowercase())
    } 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));
    }
    
    if href.starts_with("data:") {
        return Some(href.to_string());
    }
    
    base_url.and_then(|base| base.join(href).ok().map(|u| u.to_string()))
}

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

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

/// Get all image URLs from HTML
pub fn get_image_urls(html: &str, base_url: Option<&str>) -> Vec<String> {
    extract_images_from_html(html, base_url)
        .unwrap_or_default()
        .into_iter()
        .filter_map(|img| img.absolute_url)
        .collect()
}

/// Check if HTML has images
pub fn has_images(document: &Html) -> bool {
    if let Ok(sel) = Selector::parse("img, picture") {
        document.select(&sel).next().is_some()
    } else {
        false
    }
}

/// Count images in document
pub fn count_images(document: &Html) -> usize {
    if let Ok(sel) = Selector::parse("img") {
        document.select(&sel).count()
    } else {
        0
    }
}

/// Get responsive image with best resolution
pub fn get_best_image_url(img: &ImageMedia) -> &str {
    // If srcset exists, get the highest resolution
    if !img.srcset.is_empty() {
        // Find by width (prefer highest)
        if let Some(entry) = img.srcset.iter()
            .filter(|e| e.width.is_some())
            .max_by_key(|e| e.width)
        {
            return &entry.url;
        }
        
        // Find by density (prefer highest)
        if let Some(entry) = img.srcset.iter()
            .filter(|e| e.density.is_some())
            .max_by(|a, b| a.density.partial_cmp(&b.density).unwrap_or(std::cmp::Ordering::Equal))
        {
            return &entry.url;
        }
    }
    
    // Fallback to absolute_url or src
    img.absolute_url.as_deref().unwrap_or(&img.src)
}

/// Filter out placeholder images
pub fn filter_placeholders(images: Vec<ImageMedia>) -> Vec<ImageMedia> {
    images.into_iter()
        .filter(|img| !img.is_placeholder)
        .collect()
}

/// Filter images by minimum dimensions
pub fn filter_by_dimensions(images: Vec<ImageMedia>, min_width: u32, min_height: u32) -> Vec<ImageMedia> {
    images.into_iter()
        .filter(|img| {
            let w = img.width.unwrap_or(u32::MAX);
            let h = img.height.unwrap_or(u32::MAX);
            w >= min_width && h >= min_height
        })
        .collect()
}

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

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

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

    #[test]
    fn test_extract_basic_image() {
        let html = r#"<html><body><img src="/images/test.jpg" alt="Test image"></body></html>"#;
        let doc = parse_html(html);
        let base = Url::parse("https://example.com").unwrap();
        let images = extract_images(&doc, Some(&base));
        
        assert_eq!(images.len(), 1);
        assert_eq!(images[0].src, "/images/test.jpg");
        assert_eq!(images[0].absolute_url, Some("https://example.com/images/test.jpg".to_string()));
        assert_eq!(images[0].alt, Some("Test image".to_string()));
    }

    #[test]
    fn test_extract_lazy_loaded_image() {
        let html = r#"<img src="placeholder.gif" data-src="/real-image.jpg" loading="lazy">"#;
        let doc = parse_html(html);
        let base = Url::parse("https://example.com").unwrap();
        let images = extract_images(&doc, Some(&base));
        
        assert_eq!(images.len(), 1);
        assert_eq!(images[0].loading, ImageLoading::Lazy);
        assert!(images[0].data_src.is_some());
    }

    #[test]
    fn test_extract_srcset() {
        let html = r#"
            <img src="small.jpg" 
                 srcset="small.jpg 300w, medium.jpg 600w, large.jpg 1200w"
                 sizes="(max-width: 600px) 300px, 600px">
        "#;
        let doc = parse_html(html);
        let images = extract_images(&doc, None);
        
        assert_eq!(images.len(), 1);
        assert_eq!(images[0].srcset.len(), 3);
        assert_eq!(images[0].srcset[0].width, Some(300));
        assert_eq!(images[0].srcset[2].width, Some(1200));
    }

    #[test]
    fn test_decorative_image() {
        let html = r#"<img src="spacer.gif" alt="">"#;
        let doc = parse_html(html);
        let images = extract_images(&doc, None);
        
        assert_eq!(images.len(), 1);
        assert!(images[0].is_decorative);
    }

    #[test]
    fn test_image_dimensions() {
        let html = r#"<img src="test.jpg" width="800" height="600">"#;
        let doc = parse_html(html);
        let images = extract_images(&doc, None);
        
        assert_eq!(images[0].width, Some(800));
        assert_eq!(images[0].height, Some(600));
    }

    #[test]
    fn test_picture_element() {
        let html = r#"
            <picture>
                <source srcset="image.webp" type="image/webp">
                <source srcset="image.jpg" type="image/jpeg">
                <img src="image.jpg" alt="Test">
            </picture>
        "#;
        let doc = parse_html(html);
        let images = extract_images(&doc, None);
        
        // Should extract the img and include sources in srcset
        assert!(!images.is_empty());
    }

    #[test]
    fn test_placeholder_detection() {
        assert!(is_placeholder_image("https://example.com/placeholder.png", None, None));
        assert!(is_placeholder_image("https://placehold.it/100x100", None, None));
        assert!(is_placeholder_image("/spacer.gif", Some(1), Some(1)));
        assert!(!is_placeholder_image("/real-image.jpg", Some(800), Some(600)));
    }

    #[test]
    fn test_parse_srcset() {
        let srcset = "small.jpg 300w, medium.jpg 600w, large.jpg 1200w";
        let entries = parse_srcset(srcset, None);
        
        assert_eq!(entries.len(), 3);
        assert_eq!(entries[0].url, "small.jpg");
        assert_eq!(entries[0].width, Some(300));
        assert_eq!(entries[2].width, Some(1200));
    }

    #[test]
    fn test_parse_srcset_density() {
        let srcset = "image.jpg 1x, image@2x.jpg 2x, image@3x.jpg 3x";
        let entries = parse_srcset(srcset, None);
        
        assert_eq!(entries.len(), 3);
        assert_eq!(entries[0].density, Some(1.0));
        assert_eq!(entries[1].density, Some(2.0));
    }

    #[test]
    fn test_extract_extension() {
        assert_eq!(extract_extension("https://example.com/image.jpg"), Some("jpg".to_string()));
        assert_eq!(extract_extension("https://example.com/image.PNG"), Some("png".to_string()));
        assert_eq!(extract_extension("https://example.com/image.jpg?w=100"), Some("jpg".to_string()));
        assert_eq!(extract_extension("https://example.com/image"), None);
    }

    #[test]
    fn test_get_best_image_url() {
        let img = ImageMedia {
            src: "small.jpg".to_string(),
            srcset: vec![
                SrcsetEntry { url: "small.jpg".to_string(), width: Some(300), density: None },
                SrcsetEntry { url: "large.jpg".to_string(), width: Some(1200), density: None },
            ],
            ..Default::default()
        };
        
        assert_eq!(get_best_image_url(&img), "large.jpg");
    }

    #[test]
    fn test_has_images() {
        let html_with = "<html><body><img src='test.jpg'></body></html>";
        let html_without = "<html><body><p>No images</p></body></html>";
        
        assert!(has_images(&parse_html(html_with)));
        assert!(!has_images(&parse_html(html_without)));
    }

    #[test]
    fn test_filter_placeholders() {
        let images = vec![
            ImageMedia { src: "real.jpg".to_string(), is_placeholder: false, ..Default::default() },
            ImageMedia { src: "placeholder.png".to_string(), is_placeholder: true, ..Default::default() },
        ];
        
        let filtered = filter_placeholders(images);
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].src, "real.jpg");
    }

    #[test]
    fn test_image_format_detection() {
        let html = r#"<img src="test.webp">"#;
        let doc = parse_html(html);
        let images = extract_images(&doc, None);
        
        assert_eq!(images[0].format, ImageFormat::WebP);
    }
}