Compare commits
No commits in common. "bad8d6cd154d461886e76c5ebdcf688aefd02270" and "76d8726ee50716139163d2e023b1601a1f6e966b" have entirely different histories.
bad8d6cd15
...
76d8726ee5
7
api.py
7
api.py
@ -7,6 +7,7 @@ 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')
|
||||||
@ -99,7 +100,7 @@ def check_url(url):
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
'msg': '成功',
|
'msg': '成功',
|
||||||
'code': 200,
|
'code': 200,
|
||||||
'data': result
|
'dat': result
|
||||||
}), 200
|
}), 200
|
||||||
except:
|
except:
|
||||||
return jsonify({
|
return jsonify({
|
||||||
@ -129,12 +130,12 @@ def download():
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
'msg': '成功',
|
'msg': '成功',
|
||||||
'code': 200,
|
'code': 200,
|
||||||
'data': task_id
|
'dat': 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 ubuntu:24.04
|
FROM python:3.12-slim
|
||||||
|
|
||||||
# 安装系统依赖
|
# 安装系统依赖
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
@ -26,9 +26,8 @@ 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,6 +269,10 @@
|
|||||||
<div>正在加载任务数据...</div>
|
<div>正在加载任务数据...</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="last-update" id="lastUpdate">
|
||||||
|
最后更新: --
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -301,10 +305,10 @@
|
|||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
location.href='/';
|
throw new Error('认证失败');
|
||||||
}
|
}
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
location.href='/';
|
throw new Error('网络响应不正常');
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
@ -348,9 +352,9 @@
|
|||||||
var progressPercent = Math.round(task.progress * 100) + '%';
|
var progressPercent = Math.round(task.progress * 100) + '%';
|
||||||
|
|
||||||
taskHtml += `
|
taskHtml += `
|
||||||
<div class="task-item" data-id="${task.task_id}">
|
<div class="task-item" data-id="${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">
|
||||||
@ -371,12 +375,12 @@
|
|||||||
// 更新任务进度(不重新渲染整个列表)
|
// 更新任务进度(不重新渲染整个列表)
|
||||||
function updateTaskProgress(tasks) {
|
function updateTaskProgress(tasks) {
|
||||||
tasks = tasks.data;
|
tasks = tasks.data;
|
||||||
tasks.forEach(function (item) {
|
tasks.forEach(function () {
|
||||||
var taskItem = $(`.task-item[data-id="${item.task_id}"]`);
|
var taskItem = $(`.task-item[data-id="${task.id}"]`);
|
||||||
if (taskItem.length > 0) {
|
if (taskItem.length > 0) {
|
||||||
var progressPercent = Math.round(item.progress * 100) + '%';
|
var progressPercent = Math.round(task.progress * 100) + '%';
|
||||||
var statusText = item.status === 'completed' ? '已完成' : '进行中';
|
var statusText = task.status === 'completed' ? '已完成' : '进行中';
|
||||||
var statusClass = item.status === 'completed' ? 'status-completed' : 'status-processing';
|
var statusClass = task.status === 'completed' ? 'status-completed' : 'status-processing';
|
||||||
|
|
||||||
// 更新进度条
|
// 更新进度条
|
||||||
var progressBar = taskItem.find('.layui-progress-bar');
|
var progressBar = taskItem.find('.layui-progress-bar');
|
||||||
@ -407,6 +411,7 @@
|
|||||||
fetchTasks().then(tasks => {
|
fetchTasks().then(tasks => {
|
||||||
currentTasks = tasks;
|
currentTasks = tasks;
|
||||||
renderTaskList(tasks);
|
renderTaskList(tasks);
|
||||||
|
|
||||||
// 启动定时更新
|
// 启动定时更新
|
||||||
startAutoUpdate();
|
startAutoUpdate();
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
@ -427,7 +432,7 @@
|
|||||||
currentTasks = tasks;
|
currentTasks = tasks;
|
||||||
updateTaskProgress(tasks);
|
updateTaskProgress(tasks);
|
||||||
});
|
});
|
||||||
}, 1000);
|
}, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加新任务
|
// 添加新任务
|
||||||
@ -437,12 +442,10 @@
|
|||||||
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: {
|
||||||
@ -451,187 +454,25 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.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 => {
|
||||||
layer.close(loadingIndex);
|
console.log('接口返回数据:', data);
|
||||||
|
|
||||||
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();
|
// initTaskList();
|
||||||
} else {
|
|
||||||
layer.msg('下载任务提交失败: ' + data.msg, {icon: 2});
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
layer.close(downloadLoading);
|
console.log('错误信息:', error);
|
||||||
layer.msg('下载任务提交失败: ' + error.message, {icon: 2});
|
layer.msg('添加任务失败: ' + error.message);
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
yes: function(index) {
|
|
||||||
layer.close(index);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
layer.close(loadingIndex);
|
|
||||||
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 80;
|
listen 4560;
|
||||||
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(f'Authorization')
|
token = request.headers.get('Authorization')
|
||||||
|
|
||||||
if not token:
|
if not token:
|
||||||
return jsonify({
|
return jsonify({
|
||||||
@ -93,16 +93,17 @@ 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):
|
||||||
try:
|
status = is_from_missav(url)
|
||||||
|
if (status):
|
||||||
result = asyncio.run(crawl_missav(
|
result = asyncio.run(crawl_missav(
|
||||||
url
|
url
|
||||||
))
|
))
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'msg': '成功',
|
'msg': '成功',
|
||||||
'code': 200,
|
'code': 200,
|
||||||
'data': result
|
'dat': result
|
||||||
}), 200
|
}), 200
|
||||||
except:
|
else:
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'msg': '不是来自missav的链接',
|
'msg': '不是来自missav的链接',
|
||||||
'code': 500
|
'code': 500
|
||||||
@ -130,7 +131,7 @@ def download():
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
'msg': '成功',
|
'msg': '成功',
|
||||||
'code': 200,
|
'code': 200,
|
||||||
'data': task_id
|
'dat': task_id
|
||||||
}), 200
|
}), 200
|
||||||
|
|
||||||
|
|
||||||
@ -162,6 +163,15 @@ 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,41 +7,19 @@ 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="/app/downloads", cache_dir="cache"):
|
def __init__(self, max_workers=5, output_dir="downloads"):
|
||||||
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:
|
||||||
@ -84,8 +62,7 @@ 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',
|
'start_time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(task_info.get('start_time', time.time())))
|
||||||
time.localtime(task_info.get('start_time', time.time())))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
# 按开始时间倒序排列,最新的任务在前面
|
# 按开始时间倒序排列,最新的任务在前面
|
||||||
@ -134,9 +111,6 @@ 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)
|
||||||
|
|
||||||
@ -227,17 +201,15 @@ 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.cache_dir / f"temp_{task_id}"
|
temp_dir = self.output_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:
|
||||||
@ -264,30 +236,23 @@ 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()
|
||||||
|
|
||||||
# 清理缓存目录
|
# 清理临时目录
|
||||||
if temp_dir.exists():
|
temp_dir.rmdir()
|
||||||
shutil.rmtree(temp_dir)
|
|
||||||
|
|
||||||
task_info['status'] = 'completed'
|
task_info['status'] = 'completed'
|
||||||
task_info['progress'] = 1.0
|
task_info['progress'] = 1.0
|
||||||
@ -299,12 +264,6 @@ 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="/app/downloads", cache_dir="cache"):
|
def __init__(self, max_workers=5, output_dir="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