"""Shared media download helpers for acquisition and decode adapters."""
from __future__ import annotations

import httpx


REFERER = {
    "douyin": "https://www.douyin.com/",
    "kuaishou": "https://www.kuaishou.com/",
    "bilibili": "https://www.bilibili.com/",
    "shipinhao": "https://channels.weixin.qq.com/",
    "xiaohongshu": "https://www.xiaohongshu.com/",
    "weixin": "https://mp.weixin.qq.com/",
}
IOS_UA = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15"


def download_media_bytes(url: str, platform: str, timeout: float = 180.0) -> bytes:
    headers = {"User-Agent": IOS_UA, "Referer": REFERER.get(platform, "")}
    with httpx.stream("GET", url, headers=headers, timeout=timeout, follow_redirects=True) as response:
        response.raise_for_status()
        return b"".join(response.iter_bytes())
