mirror of
https://github.com/Cp0204/quark-auto-save.git
synced 2026-01-11 14:40:44 +08:00
修复豆瓣海报无法显示的问题
- 新增后端图片代理路由 /api/proxy/douban-image,用于代理豆瓣图片请求 - 后端代理会自动设置正确的 Referer 头以绕过豆瓣防盗链限制 - 修改前端 getProxiedImageUrl 函数,自动检测豆瓣图片URL并使用代理 - 支持流式传输和缓存控制,提升加载性能 解决豆瓣新增防盗链限制导致海报无法显示的问题
This commit is contained in:
parent
c1ef5fdda6
commit
e4ffec9ba4
54
app/run.py
54
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():
|
||||
"""更新节目的内容类型"""
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user