构建媒体下载请求头,优先复用当前打码浏览器指纹。
(
self,
media_type: str,
fingerprint: Optional[Dict[str, Any]] = None,
)
| 120 | return suffix |
| 121 | |
| 122 | def _build_download_headers( |
| 123 | self, |
| 124 | media_type: str, |
| 125 | fingerprint: Optional[Dict[str, Any]] = None, |
| 126 | ) -> Dict[str, str]: |
| 127 | """构建媒体下载请求头,优先复用当前打码浏览器指纹。""" |
| 128 | headers = { |
| 129 | "Accept": ( |
| 130 | "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8" |
| 131 | if media_type == "image" |
| 132 | else "*/*" |
| 133 | ), |
| 134 | "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", |
| 135 | "Accept-Encoding": "gzip, deflate, br", |
| 136 | "Connection": "keep-alive", |
| 137 | "Referer": "https://labs.google/", |
| 138 | "Sec-Fetch-Site": "same-origin", |
| 139 | "Sec-Fetch-Mode": "cors", |
| 140 | } |
| 141 | |
| 142 | if media_type == "image": |
| 143 | headers["Sec-Fetch-Dest"] = "image" |
| 144 | else: |
| 145 | headers["Sec-Fetch-Dest"] = "video" |
| 146 | |
| 147 | if isinstance(fingerprint, dict): |
| 148 | if fingerprint.get("user_agent"): |
| 149 | headers["User-Agent"] = str(fingerprint["user_agent"]) |
| 150 | if fingerprint.get("accept_language"): |
| 151 | headers["Accept-Language"] = str(fingerprint["accept_language"]) |
| 152 | if fingerprint.get("sec_ch_ua"): |
| 153 | headers["sec-ch-ua"] = str(fingerprint["sec_ch_ua"]) |
| 154 | if fingerprint.get("sec_ch_ua_mobile"): |
| 155 | headers["sec-ch-ua-mobile"] = str(fingerprint["sec_ch_ua_mobile"]) |
| 156 | if fingerprint.get("sec_ch_ua_platform"): |
| 157 | headers["sec-ch-ua-platform"] = str(fingerprint["sec_ch_ua_platform"]) |
| 158 | |
| 159 | headers.setdefault( |
| 160 | "User-Agent", |
| 161 | "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " |
| 162 | "(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" |
| 163 | ) |
| 164 | return headers |
| 165 | |
| 166 | def _write_cached_content(self, file_path: Path, content: bytes): |
| 167 | """先写临时文件,再原子替换,避免并发读到半截文件。""" |