From e4ffec9ba4421f873e27d0d7f269917731727547 Mon Sep 17 00:00:00 2001 From: x1ao4 Date: Sat, 10 Jan 2026 14:32:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B1=86=E7=93=A3=E6=B5=B7?= =?UTF-8?q?=E6=8A=A5=E6=97=A0=E6=B3=95=E6=98=BE=E7=A4=BA=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增后端图片代理路由 /api/proxy/douban-image,用于代理豆瓣图片请求 - 后端代理会自动设置正确的 Referer 头以绕过豆瓣防盗链限制 - 修改前端 getProxiedImageUrl 函数,自动检测豆瓣图片URL并使用代理 - 支持流式传输和缓存控制,提升加载性能 解决豆瓣新增防盗链限制导致海报无法显示的问题 --- app/run.py | 54 ++++++++++++++++++++++++++++++++++++++++ app/templates/index.html | 14 ++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/app/run.py b/app/run.py index 3f3326d..d17d13d 100644 --- a/app/run.py +++ b/app/run.py @@ -8805,6 +8805,60 @@ def get_tv_list(tv_type, sub_category): 'data': {'items': []} }) + +@app.route("/api/proxy/douban-image") +def proxy_douban_image(): + """代理豆瓣图片请求,设置正确的 Referer 头以绕过防盗链限制""" + try: + image_url = request.args.get('url') + if not image_url: + return Response('缺少图片URL参数', status=400, mimetype='text/plain') + + # 验证URL是否为豆瓣图片地址 + if not (image_url.startswith('http://') or image_url.startswith('https://')): + return Response('无效的图片URL', status=400, mimetype='text/plain') + + # 检查是否为豆瓣图片域名(douban.com, doubanio.com等) + douban_domains = ['douban.com', 'doubanio.com'] + is_douban_image = any(domain in image_url for domain in douban_domains) + + if not is_douban_image: + # 如果不是豆瓣图片,可以选择直接重定向或拒绝 + # 这里我们选择直接代理,但不设置Referer + headers = { + 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1' + } + else: + # 豆瓣图片需要设置Referer为豆瓣域名 + headers = { + 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1', + 'Referer': 'https://movie.douban.com/' + } + + # 请求图片 + response = requests.get(image_url, headers=headers, timeout=10, stream=True) + + if response.status_code != 200: + return Response(f'图片加载失败: {response.status_code}', + status=response.status_code, + mimetype='text/plain') + + # 获取图片的Content-Type + content_type = response.headers.get('Content-Type', 'image/jpeg') + + # 设置响应头,允许跨域(如果需要) + resp = Response( + stream_with_context(response.iter_content(chunk_size=8192)), + content_type=content_type + ) + resp.headers['Cache-Control'] = 'public, max-age=3600' + + return resp + + except Exception as e: + logging.error(f"代理豆瓣图片失败: {str(e)}") + return Response(f'代理图片失败: {str(e)}', status=500, mimetype='text/plain') + @app.route("/api/calendar/update_content_type", methods=["POST"]) def update_show_content_type(): """更新节目的内容类型""" diff --git a/app/templates/index.html b/app/templates/index.html index 4f57769..668f239 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -14300,8 +14300,20 @@ event.target.src = '/static/images/no-poster.svg'; }, getProxiedImageUrl(originalUrl) { - // 保持直连,发现页加载更快,且不需要取色跨域处理 + // 检查是否为豆瓣图片URL,如果是则使用后端代理绕过防盗链限制 if (!originalUrl) return '/static/images/no-poster.svg'; + + // 检查是否为豆瓣图片地址 + const doubanDomains = ['douban.com', 'doubanio.com']; + const isDoubanImage = doubanDomains.some(domain => originalUrl.includes(domain)); + + if (isDoubanImage) { + // 使用后端代理接口,通过URL编码传递图片地址 + const encodedUrl = encodeURIComponent(originalUrl); + return `/api/proxy/douban-image?url=${encodedUrl}`; + } + + // 非豆瓣图片直接返回原URL return originalUrl; }, createTaskFromDiscovery(item) {