SPB Git

Repository Public

test_extract_media.py 5.5 KB 108 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project          : SPIDER — Universal Web Intelligence Engine7# File             : tests/unit/extract/test_extract_media.py8# Module           : Tests9# Repository       : https://git.spboucher.ai/spider10# Project Website  : Not specified11# Created          : 2026-12-0412# Last Modified    : 2026-12-0413#14# Author           : Simon-Pierre Boucher15# Position         : Professor of Finance16# Institution      : Université du Québec en Outaouais (UQO)17# Organization     : SP Boucher18# Website          : https://spboucher.ai19# Git              : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development      : dev@spboucher.ai23# Support          : support@spboucher.ai24# Security/Admin   : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License          : ISC33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""Media inventory."""42from spider.extract.media import extract_media, guess_mime, parse_srcset4344BASE = "https://ex.com/a/page"454647def test_img_srcset_picks_largest_and_keeps_all():48    html = '<img src="s.jpg" srcset="s-400.jpg 400w, s-1200.jpg 1200w, s-800.jpg 800w" alt=" Le  salon " width="800" height="600px">'49    (item,) = extract_media(html, BASE)50    assert item.kind == "image" and item.url == "https://ex.com/a/s-1200.jpg"51    assert [c.descriptor for c in item.candidates] == ["400w", "1200w", "800w", ""]52    assert item.alt == "Le salon" and (item.width, item.height) == (800, 600)53    assert item.mime_guess == "image/jpeg" and not item.lazy54    assert item.selector == "html > body > img"555657def test_density_descriptors_and_plain_src():58    (item,) = extract_media('<img src="/a.png" srcset="/a.png 1x, /a@2x.png 2x">', BASE)59    assert item.url == "https://ex.com/a@2x.png" and item.mime_guess == "image/png"60    (item,) = extract_media('<img src="/only.webp">', BASE)61    assert item.url == "https://ex.com/only.webp" and len(item.candidates) == 1626364def test_lazy_patterns():65    html = """<img src="data:image/gif;base64,R0lGOD" data-src="/real.jpg" loading="lazy" alt="Lazy">66    <img src="/placeholder.gif" data-lazy-src="/real2.jpg">67    <img data-srcset="/l-300.jpg 300w, /l-900.jpg 900w" class="lazyload">68    <noscript><img src="/real.jpg" alt="fallback"></noscript>"""69    items = extract_media(html, BASE)70    assert [i.url for i in items] == ["https://ex.com/real.jpg", "https://ex.com/real2.jpg", "https://ex.com/l-900.jpg"]71    assert all(i.lazy for i in items)72    assert items[0].loading == "lazy" and items[0].candidates[0].origin == "data-src"73    assert not any(c.url.startswith("data:") for i in items for c in i.candidates)747576def test_picture_sources_and_figure_caption():77    html = """<figure><picture><source type="image/avif" srcset="/h-800.avif 800w, /h-1600.avif 1600w" media="(min-width: 600px)">78    <source type="image/webp" srcset="/h-800.webp 800w"><img src="/h.jpg" alt="Héro"></picture>79    <figcaption>Vue  de la façade</figcaption></figure>"""80    (item,) = extract_media(html, BASE)81    assert item.tag == "picture" and item.url == "https://ex.com/h-1600.avif"82    assert item.mime_guess == "image/avif" and item.caption == "Vue de la façade" and item.alt == "Héro"83    assert {c.type for c in item.candidates} == {"", "image/avif", "image/webp"}84    assert item.candidates[1].media == "(min-width: 600px)"858687def test_video_and_audio():88    html = """<video poster="/poster.jpg" width="640" height="360" preload="none">89    <source src="/v.webm" type="video/webm"><source src="/v.mp4" type="video/mp4"></video>90    <audio src="/son.mp3" title="Balado"></audio><video></video>"""91    video, audio = extract_media(html, BASE)92    assert video.kind == "video" and video.url == "https://ex.com/v.webm" and video.mime_guess == "video/webm"93    assert video.poster == "https://ex.com/poster.jpg" and (video.width, video.height) == (640, 360) and video.lazy94    assert [c.url for c in video.candidates] == ["https://ex.com/v.webm", "https://ex.com/v.mp4"]95    assert audio.kind == "audio" and audio.mime_guess == "audio/mpeg" and audio.title == "Balado"969798def test_helpers_and_degenerate_inputs():99    assert guess_mime("https://ex.com/a/b.JPG?x=1") == "image/jpeg"100    assert guess_mime("https://ex.com/a/b", "image/webp; q=1") == "image/webp"101    assert guess_mime("https://ex.com/a.b/noext") == ""102    cands = parse_srcset("a.jpg 1x,https://cdn.ex.com/b,c.jpg 2x, data:image/png;base64,AA 3x", BASE)103    assert [(c.url, c.descriptor) for c in cands] == [("https://ex.com/a/a.jpg", "1x"),104                                                     ("https://cdn.ex.com/b,c.jpg", "2x")]105    assert extract_media("", BASE) == []106    assert extract_media('<img><img src=""><img src="javascript:x"><picture></picture>', BASE) == []107    assert extract_media('<base href="https://cdn.ex.com/"><img src="i.svg">', BASE)[0].url == "https://cdn.ex.com/i.svg"108
Files main
Menu

Theme