Compare commits
2 Commits
76d8726ee5
...
bad8d6cd15
| Author | SHA1 | Date | |
|---|---|---|---|
| bad8d6cd15 | |||
| c863d3b2e0 |
7
api.py
7
api.py
@ -7,7 +7,6 @@ from functools import wraps
|
|||||||
|
|
||||||
from download import M3U8Downloader
|
from download import M3U8Downloader
|
||||||
from function import crawl_missav
|
from function import crawl_missav
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'your-secret-key-here')
|
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'your-secret-key-here')
|
||||||
@ -100,7 +99,7 @@ def check_url(url):
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
'msg': '成功',
|
'msg': '成功',
|
||||||
'code': 200,
|
'code': 200,
|
||||||
'dat': result
|
'data': result
|
||||||
}), 200
|
}), 200
|
||||||
except:
|
except:
|
||||||
return jsonify({
|
return jsonify({
|
||||||
@ -130,12 +129,12 @@ def download():
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
'msg': '成功',
|
'msg': '成功',
|
||||||
'code': 200,
|
'code': 200,
|
||||||
'dat': task_id
|
'data': task_id
|
||||||
}), 200
|
}), 200
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/all-task', methods=['GET'])
|
@app.route('/api/all-task', methods=['GET'])
|
||||||
# @token_required
|
@token_required
|
||||||
def all_task():
|
def all_task():
|
||||||
all_tasks = downloader.get_all_tasks()
|
all_tasks = downloader.get_all_tasks()
|
||||||
return jsonify({
|
return jsonify({
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
FROM python:3.12-slim
|
FROM ubuntu:24.04
|
||||||
|
|
||||||
# 安装系统依赖
|
# 安装系统依赖
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
@ -26,8 +26,9 @@ RUN apt-get update && apt-get install -y \
|
|||||||
fonts-liberation \
|
fonts-liberation \
|
||||||
libnss3-tools \
|
libnss3-tools \
|
||||||
xvfb \
|
xvfb \
|
||||||
|
python3 \
|
||||||
|
python3-pip \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# 设置工作目录
|
# 设置工作目录
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
|||||||
@ -269,10 +269,6 @@
|
|||||||
<div>正在加载任务数据...</div>
|
<div>正在加载任务数据...</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="last-update" id="lastUpdate">
|
|
||||||
最后更新: --
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -305,10 +301,10 @@
|
|||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
throw new Error('认证失败');
|
location.href='/';
|
||||||
}
|
}
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('网络响应不正常');
|
location.href='/';
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
@ -352,9 +348,9 @@
|
|||||||
var progressPercent = Math.round(task.progress * 100) + '%';
|
var progressPercent = Math.round(task.progress * 100) + '%';
|
||||||
|
|
||||||
taskHtml += `
|
taskHtml += `
|
||||||
<div class="task-item" data-id="${task.id}">
|
<div class="task-item" data-id="${task.task_id}">
|
||||||
<div class="file-info">
|
<div class="file-info">
|
||||||
<div class="file-name">${task.fileName}</div>
|
<div class="file-name">${task.filename}</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="progress-container">
|
<div class="progress-container">
|
||||||
<div class="layui-progress">
|
<div class="layui-progress">
|
||||||
@ -375,12 +371,12 @@
|
|||||||
// 更新任务进度(不重新渲染整个列表)
|
// 更新任务进度(不重新渲染整个列表)
|
||||||
function updateTaskProgress(tasks) {
|
function updateTaskProgress(tasks) {
|
||||||
tasks = tasks.data;
|
tasks = tasks.data;
|
||||||
tasks.forEach(function () {
|
tasks.forEach(function (item) {
|
||||||
var taskItem = $(`.task-item[data-id="${task.id}"]`);
|
var taskItem = $(`.task-item[data-id="${item.task_id}"]`);
|
||||||
if (taskItem.length > 0) {
|
if (taskItem.length > 0) {
|
||||||
var progressPercent = Math.round(task.progress * 100) + '%';
|
var progressPercent = Math.round(item.progress * 100) + '%';
|
||||||
var statusText = task.status === 'completed' ? '已完成' : '进行中';
|
var statusText = item.status === 'completed' ? '已完成' : '进行中';
|
||||||
var statusClass = task.status === 'completed' ? 'status-completed' : 'status-processing';
|
var statusClass = item.status === 'completed' ? 'status-completed' : 'status-processing';
|
||||||
|
|
||||||
// 更新进度条
|
// 更新进度条
|
||||||
var progressBar = taskItem.find('.layui-progress-bar');
|
var progressBar = taskItem.find('.layui-progress-bar');
|
||||||
@ -411,7 +407,6 @@
|
|||||||
fetchTasks().then(tasks => {
|
fetchTasks().then(tasks => {
|
||||||
currentTasks = tasks;
|
currentTasks = tasks;
|
||||||
renderTaskList(tasks);
|
renderTaskList(tasks);
|
||||||
|
|
||||||
// 启动定时更新
|
// 启动定时更新
|
||||||
startAutoUpdate();
|
startAutoUpdate();
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
@ -432,7 +427,7 @@
|
|||||||
currentTasks = tasks;
|
currentTasks = tasks;
|
||||||
updateTaskProgress(tasks);
|
updateTaskProgress(tasks);
|
||||||
});
|
});
|
||||||
}, 5000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加新任务
|
// 添加新任务
|
||||||
@ -442,10 +437,12 @@
|
|||||||
formType: 0,
|
formType: 0,
|
||||||
}, function (url, index) {
|
}, function (url, index) {
|
||||||
layer.close(index);
|
layer.close(index);
|
||||||
// 获取token
|
|
||||||
const token = localStorage.getItem('Authorization');
|
|
||||||
|
|
||||||
// 调用检查接口
|
const token = localStorage.getItem('Authorization');
|
||||||
|
const loadingIndex = layer.load(2, {
|
||||||
|
shade: [0.1, '#000']
|
||||||
|
});
|
||||||
|
|
||||||
fetch(`/api/check/${encodeURIComponent(url)}`, {
|
fetch(`/api/check/${encodeURIComponent(url)}`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
@ -454,25 +451,187 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
console.log('响应状态:', response.status);
|
|
||||||
console.log('响应头:', response.headers);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('检查URL失败');
|
throw new Error('检查URL失败');
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
console.log('接口返回数据:', data);
|
layer.close(loadingIndex);
|
||||||
// 重新加载任务列表
|
|
||||||
// initTaskList();
|
if (data.code !== 200 || !data.data) {
|
||||||
|
layer.msg('接口返回异常', { icon: 2 });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { serial_number, title, url: urlList } = data.data;
|
||||||
|
|
||||||
|
// 检测设备类型
|
||||||
|
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
||||||
|
|
||||||
|
// 动态生成URL列表HTML - 响应式布局
|
||||||
|
const urlHtml = urlList.map((u, index) => {
|
||||||
|
if (isMobile) {
|
||||||
|
// 手机端布局 - 垂直排列
|
||||||
|
return `
|
||||||
|
<li class="layui-col-xs12" style="padding: 10px 0; border-bottom: 1px solid #f6f6f6;">
|
||||||
|
<div style="word-break: break-all; font-size: 13px; color: #666; margin-bottom: 8px;">
|
||||||
|
${u}
|
||||||
|
</div>
|
||||||
|
<button class="layui-btn layui-btn-sm layui-btn-normal select-url"
|
||||||
|
data-serial="${serial_number}"
|
||||||
|
data-url="${u}"
|
||||||
|
style="width: 100%;">
|
||||||
|
选择
|
||||||
|
</button>
|
||||||
|
</li>`;
|
||||||
|
} else {
|
||||||
|
// 电脑端布局 - 水平排列
|
||||||
|
return `
|
||||||
|
<li class="layui-row" style="padding: 8px 0; border-bottom: 1px solid #f6f6f6; margin: 0;">
|
||||||
|
<div class="layui-col-xs10" style="word-break: break-all; font-size: 13px; color: #666; line-height: 32px;">
|
||||||
|
${u}
|
||||||
|
</div>
|
||||||
|
<div class="layui-col-xs2" style="text-align: right;">
|
||||||
|
<button class="layui-btn layui-btn-sm layui-btn-normal select-url"
|
||||||
|
data-serial="${serial_number}"
|
||||||
|
data-url="${u}">
|
||||||
|
选择
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</li>`;
|
||||||
|
}
|
||||||
|
}).join('');
|
||||||
|
|
||||||
|
// 响应式弹窗设置
|
||||||
|
const popupSettings = isMobile ? {
|
||||||
|
area: ['90vw', '70vh'],
|
||||||
|
content: `
|
||||||
|
<div style="padding: 15px;">
|
||||||
|
<div class="layui-form-item" style="margin-bottom: 15px;">
|
||||||
|
<label class="layui-form-label" style="width: 80px; padding: 9px 5px;">番号:</label>
|
||||||
|
<div class="layui-input-block">
|
||||||
|
<input type="text" class="layui-input" value="${serial_number || ''}" readonly
|
||||||
|
style="background-color: #f8f8f8; color: #666; font-size: 14px;">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="layui-form-item" style="margin-bottom: 15px;">
|
||||||
|
<label class="layui-form-label" style="width: 80px; padding: 9px 5px;">标题:</label>
|
||||||
|
<div class="layui-input-block">
|
||||||
|
<input type="text" class="layui-input" value="${title || ''}" readonly
|
||||||
|
style="background-color: #f8f8f8; color: #666; font-size: 14px;">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="layui-form-item">
|
||||||
|
<label class="layui-form-label" style="width: 80px; padding: 9px 5px;">URL列表:</label>
|
||||||
|
<div class="layui-input-block">
|
||||||
|
<div style="max-height: 40vh; overflow-y: auto; border: 1px solid #e6e6e6; border-radius: 4px; padding: 10px;">
|
||||||
|
<ul style="list-style: none; padding: 0; margin: 0;">${urlHtml || '<li style="padding: 10px; text-align: center; color: #999;">无URL</li>'}</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
} : {
|
||||||
|
area: ['580px', '420px'],
|
||||||
|
content: `
|
||||||
|
<div style="padding: 15px;">
|
||||||
|
<div class="layui-form-item">
|
||||||
|
<label class="layui-form-label" style="width: 100px;">番号:</label>
|
||||||
|
<div class="layui-input-block">
|
||||||
|
<input type="text" class="layui-input" value="${serial_number || ''}" readonly
|
||||||
|
style="background-color: #f8f8f8; color: #666;">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="layui-form-item">
|
||||||
|
<label class="layui-form-label" style="width: 100px;">标题:</label>
|
||||||
|
<div class="layui-input-block">
|
||||||
|
<input type="text" class="layui-input" value="${title || ''}" readonly
|
||||||
|
style="background-color: #f8f8f8; color: #666;">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="layui-form-item">
|
||||||
|
<label class="layui-form-label" style="width: 100px;">URL列表:</label>
|
||||||
|
<div class="layui-input-block">
|
||||||
|
<div style="max-height: 200px; overflow-y: auto; border: 1px solid #e6e6e6; border-radius: 2px; padding: 0 10px;">
|
||||||
|
<ul style="list-style: none; padding: 0; margin: 0;">${urlHtml || '<li style="padding: 10px; text-align: center; color: #999;">无URL</li>'}</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
};
|
||||||
|
|
||||||
|
// 打开弹窗
|
||||||
|
const popupIndex = layer.open({
|
||||||
|
type: 1,
|
||||||
|
title: '链接详情',
|
||||||
|
...popupSettings,
|
||||||
|
btn: ['关闭'],
|
||||||
|
success: function (layero) {
|
||||||
|
// 为选择按钮绑定点击事件
|
||||||
|
$(layero).find('.select-url').on('click', function () {
|
||||||
|
const serial = $(this).data('serial');
|
||||||
|
const selectedUrl = $(this).data('url');
|
||||||
|
|
||||||
|
|
||||||
|
// 响应式提示框
|
||||||
|
const msgSettings = isMobile ? {
|
||||||
|
area: ['80vw', 'auto'],
|
||||||
|
time: 4000
|
||||||
|
} : {
|
||||||
|
area: ['400px', 'auto'],
|
||||||
|
time: 3000
|
||||||
|
};
|
||||||
|
const downloadLoading = layer.msg('正在提交下载任务...', {icon: 16, time: 0});
|
||||||
|
fetch('/api/download', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Authorization': token,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
name: serial,
|
||||||
|
url: selectedUrl
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('下载任务提交失败');
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
layer.close(downloadLoading);
|
||||||
|
if (data.code === 200) {
|
||||||
|
layer.msg('下载任务提交成功', {icon: 1});
|
||||||
|
// 关闭所有弹出层
|
||||||
|
layer.closeAll();
|
||||||
|
// 重新加载任务列表
|
||||||
|
initTaskList();
|
||||||
|
} else {
|
||||||
|
layer.msg('下载任务提交失败: ' + data.msg, {icon: 2});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
layer.close(downloadLoading);
|
||||||
|
layer.msg('下载任务提交失败: ' + error.message, {icon: 2});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
yes: function(index) {
|
||||||
|
layer.close(index);
|
||||||
|
}
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.log('错误信息:', error);
|
layer.close(loadingIndex);
|
||||||
layer.msg('添加任务失败: ' + error.message);
|
layer.msg('添加任务失败: ' + error.message, { icon: 2, time: 2000 });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
initTaskList();
|
initTaskList();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@ -13,7 +13,7 @@ http {
|
|||||||
default $remote_addr;
|
default $remote_addr;
|
||||||
}
|
}
|
||||||
server {
|
server {
|
||||||
listen 4560;
|
listen 80;
|
||||||
server_name localhost;
|
server_name localhost;
|
||||||
client_header_buffer_size 64k;
|
client_header_buffer_size 64k;
|
||||||
large_client_header_buffers 8 128k;
|
large_client_header_buffers 8 128k;
|
||||||
|
|||||||
@ -22,7 +22,7 @@ PASSWORD = os.getenv('PASSWORD')
|
|||||||
def token_required(f):
|
def token_required(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated(*args, **kwargs):
|
def decorated(*args, **kwargs):
|
||||||
token = request.headers.get('Authorization')
|
token = request.headers.get(f'Authorization')
|
||||||
|
|
||||||
if not token:
|
if not token:
|
||||||
return jsonify({
|
return jsonify({
|
||||||
@ -93,17 +93,16 @@ def login():
|
|||||||
@app.route('/api/check/<path:url>')
|
@app.route('/api/check/<path:url>')
|
||||||
@token_required
|
@token_required
|
||||||
def check_url(url):
|
def check_url(url):
|
||||||
status = is_from_missav(url)
|
try:
|
||||||
if (status):
|
|
||||||
result = asyncio.run(crawl_missav(
|
result = asyncio.run(crawl_missav(
|
||||||
url
|
url
|
||||||
))
|
))
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'msg': '成功',
|
'msg': '成功',
|
||||||
'code': 200,
|
'code': 200,
|
||||||
'dat': result
|
'data': result
|
||||||
}), 200
|
}), 200
|
||||||
else:
|
except:
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'msg': '不是来自missav的链接',
|
'msg': '不是来自missav的链接',
|
||||||
'code': 500
|
'code': 500
|
||||||
@ -131,7 +130,7 @@ def download():
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
'msg': '成功',
|
'msg': '成功',
|
||||||
'code': 200,
|
'code': 200,
|
||||||
'dat': task_id
|
'data': task_id
|
||||||
}), 200
|
}), 200
|
||||||
|
|
||||||
|
|
||||||
@ -163,15 +162,6 @@ def progress(task_id):
|
|||||||
}), 200
|
}), 200
|
||||||
|
|
||||||
|
|
||||||
def is_from_missav(url):
|
|
||||||
try:
|
|
||||||
parsed = urlparse(url)
|
|
||||||
hostname = parsed.netloc.lower()
|
|
||||||
return hostname == 'missav.ws' or hostname.endswith('.missav.ws')
|
|
||||||
except:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# 检查环境变量是否设置
|
# 检查环境变量是否设置
|
||||||
if not USERNAME or not PASSWORD:
|
if not USERNAME or not PASSWORD:
|
||||||
|
|||||||
@ -7,19 +7,41 @@ import m3u8
|
|||||||
from Crypto.Cipher import AES
|
from Crypto.Cipher import AES
|
||||||
import concurrent.futures
|
import concurrent.futures
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
|
||||||
class M3U8Downloader:
|
class M3U8Downloader:
|
||||||
def __init__(self, max_workers=5, output_dir="downloads"):
|
def __init__(self, max_workers=5, output_dir="/app/downloads", cache_dir="cache"):
|
||||||
self.max_workers = max_workers
|
self.max_workers = max_workers
|
||||||
self.output_dir = Path(output_dir)
|
self.output_dir = Path(output_dir)
|
||||||
|
self.cache_dir = Path(cache_dir)
|
||||||
|
|
||||||
|
# 创建目录
|
||||||
self.output_dir.mkdir(exist_ok=True)
|
self.output_dir.mkdir(exist_ok=True)
|
||||||
|
self.cache_dir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
# 清空缓存目录
|
||||||
|
self.clear_cache()
|
||||||
|
|
||||||
# 存储下载任务状态
|
# 存储下载任务状态
|
||||||
self.tasks = {}
|
self.tasks = {}
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
self.task_counter = 0
|
self.task_counter = 0
|
||||||
|
|
||||||
|
def clear_cache(self):
|
||||||
|
"""清空缓存目录"""
|
||||||
|
try:
|
||||||
|
if self.cache_dir.exists():
|
||||||
|
# 删除缓存目录中的所有内容
|
||||||
|
for item in self.cache_dir.iterdir():
|
||||||
|
if item.is_file():
|
||||||
|
item.unlink()
|
||||||
|
elif item.is_dir():
|
||||||
|
shutil.rmtree(item)
|
||||||
|
print(f"缓存目录已清空: {self.cache_dir}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"清空缓存目录失败: {e}")
|
||||||
|
|
||||||
def get_task_info(self, task_id):
|
def get_task_info(self, task_id):
|
||||||
"""获取任务信息"""
|
"""获取任务信息"""
|
||||||
with self.lock:
|
with self.lock:
|
||||||
@ -62,7 +84,8 @@ class M3U8Downloader:
|
|||||||
'filename': task_info['output_filename'],
|
'filename': task_info['output_filename'],
|
||||||
'status': task_info['status'],
|
'status': task_info['status'],
|
||||||
'progress': round(progress, 4),
|
'progress': round(progress, 4),
|
||||||
'start_time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(task_info.get('start_time', time.time())))
|
'start_time': time.strftime('%Y-%m-%d %H:%M:%S',
|
||||||
|
time.localtime(task_info.get('start_time', time.time())))
|
||||||
})
|
})
|
||||||
|
|
||||||
# 按开始时间倒序排列,最新的任务在前面
|
# 按开始时间倒序排列,最新的任务在前面
|
||||||
@ -111,6 +134,9 @@ class M3U8Downloader:
|
|||||||
cipher = AES.new(task_info['key'], AES.MODE_CBC, task_info['iv'])
|
cipher = AES.new(task_info['key'], AES.MODE_CBC, task_info['iv'])
|
||||||
ts_data = cipher.decrypt(ts_data)
|
ts_data = cipher.decrypt(ts_data)
|
||||||
|
|
||||||
|
# 确保缓存目录存在
|
||||||
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
with open(output_path, 'wb') as f:
|
with open(output_path, 'wb') as f:
|
||||||
f.write(ts_data)
|
f.write(ts_data)
|
||||||
|
|
||||||
@ -201,15 +227,17 @@ class M3U8Downloader:
|
|||||||
task_info['total_segments'] = len(ts_segments)
|
task_info['total_segments'] = len(ts_segments)
|
||||||
task_info['status'] = 'downloading'
|
task_info['status'] = 'downloading'
|
||||||
|
|
||||||
# 设置输出文件路径
|
# 设置输出文件路径(在下载目录中)
|
||||||
output_path = self.output_dir / output_filename
|
output_path = self.output_dir / output_filename
|
||||||
task_info['output_file'] = str(output_path)
|
task_info['output_file'] = str(output_path)
|
||||||
|
|
||||||
# 创建临时目录存储TS片段
|
# 创建临时目录存储TS片段(在缓存目录中)
|
||||||
temp_dir = self.output_dir / f"temp_{task_id}"
|
temp_dir = self.cache_dir / f"temp_{task_id}"
|
||||||
temp_dir.mkdir(exist_ok=True)
|
temp_dir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
print(f"开始下载任务 {task_id}: {len(ts_segments)} 个片段")
|
print(f"开始下载任务 {task_id}: {len(ts_segments)} 个片段")
|
||||||
|
print(f"缓存目录: {temp_dir}")
|
||||||
|
print(f"输出文件: {output_path}")
|
||||||
|
|
||||||
# 使用线程池下载所有TS片段
|
# 使用线程池下载所有TS片段
|
||||||
with concurrent.futures.ThreadPoolExecutor(max_workers=self.max_workers) as executor:
|
with concurrent.futures.ThreadPoolExecutor(max_workers=self.max_workers) as executor:
|
||||||
@ -236,23 +264,30 @@ class M3U8Downloader:
|
|||||||
task_info['error'] = '部分片段下载失败'
|
task_info['error'] = '部分片段下载失败'
|
||||||
task_info['progress'] = 0.0
|
task_info['progress'] = 0.0
|
||||||
print(f"任务 {task_id} 下载失败,部分片段下载失败")
|
print(f"任务 {task_id} 下载失败,部分片段下载失败")
|
||||||
|
|
||||||
|
# 清理缓存
|
||||||
|
if temp_dir.exists():
|
||||||
|
shutil.rmtree(temp_dir)
|
||||||
return
|
return
|
||||||
|
|
||||||
# 合并TS文件
|
# 合并TS文件到下载目录
|
||||||
print(f"开始合并TS文件...")
|
print(f"开始合并TS文件...")
|
||||||
task_info['status'] = 'merging'
|
task_info['status'] = 'merging'
|
||||||
task_info['progress'] = 1.0
|
task_info['progress'] = 1.0
|
||||||
|
|
||||||
|
# 确保输出目录存在
|
||||||
|
self.output_dir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
with open(output_path, 'wb') as outfile:
|
with open(output_path, 'wb') as outfile:
|
||||||
for i in range(len(ts_segments)):
|
for i in range(len(ts_segments)):
|
||||||
ts_path = temp_dir / f"segment_{i:05d}.ts"
|
ts_path = temp_dir / f"segment_{i:05d}.ts"
|
||||||
if ts_path.exists():
|
if ts_path.exists():
|
||||||
with open(ts_path, 'rb') as infile:
|
with open(ts_path, 'rb') as infile:
|
||||||
outfile.write(infile.read())
|
outfile.write(infile.read())
|
||||||
ts_path.unlink()
|
|
||||||
|
|
||||||
# 清理临时目录
|
# 清理缓存目录
|
||||||
temp_dir.rmdir()
|
if temp_dir.exists():
|
||||||
|
shutil.rmtree(temp_dir)
|
||||||
|
|
||||||
task_info['status'] = 'completed'
|
task_info['status'] = 'completed'
|
||||||
task_info['progress'] = 1.0
|
task_info['progress'] = 1.0
|
||||||
@ -264,6 +299,12 @@ class M3U8Downloader:
|
|||||||
task_info['status'] = 'failed'
|
task_info['status'] = 'failed'
|
||||||
task_info['error'] = str(e)
|
task_info['error'] = str(e)
|
||||||
task_info['progress'] = 0.0
|
task_info['progress'] = 0.0
|
||||||
|
|
||||||
|
# 清理缓存
|
||||||
|
temp_dir = self.cache_dir / f"temp_{task_id}"
|
||||||
|
if temp_dir.exists():
|
||||||
|
shutil.rmtree(temp_dir)
|
||||||
|
|
||||||
print(f"任务 {task_id} 失败: {e}")
|
print(f"任务 {task_id} 失败: {e}")
|
||||||
|
|
||||||
def download(self, output_filename, m3u8_url):
|
def download(self, output_filename, m3u8_url):
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import shutil
|
|||||||
|
|
||||||
|
|
||||||
class M3U8Downloader:
|
class M3U8Downloader:
|
||||||
def __init__(self, max_workers=5, output_dir="downloads", cache_dir="cache"):
|
def __init__(self, max_workers=5, output_dir="/app/downloads", cache_dir="cache"):
|
||||||
self.max_workers = max_workers
|
self.max_workers = max_workers
|
||||||
self.output_dir = Path(output_dir)
|
self.output_dir = Path(output_dir)
|
||||||
self.cache_dir = Path(cache_dir)
|
self.cache_dir = Path(cache_dir)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user