Add Douyin video downloader with auto-cookie feature
This commit is contained in:
105
test_auto_download.py
Normal file
105
test_auto_download.py
Normal file
@@ -0,0 +1,105 @@
|
||||
import sys
|
||||
import os
|
||||
import time
|
||||
import subprocess
|
||||
|
||||
# 设置UTF-8编码
|
||||
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
||||
|
||||
# 添加项目路径
|
||||
sys.path.insert(0, r"C:\Users\Administrator\Desktop\TikTokDownload-main")
|
||||
|
||||
from auto_cookie import get_douyin_cookie_auto
|
||||
|
||||
def test_auto_download():
|
||||
"""测试完整的自动下载流程"""
|
||||
print("="*60)
|
||||
print("自动下载测试")
|
||||
print("="*60)
|
||||
|
||||
# 步骤1: 自动获取Cookie
|
||||
print("\n[步骤1] 自动获取Cookie")
|
||||
cookie = get_douyin_cookie_auto(force_refresh=True)
|
||||
|
||||
if not cookie:
|
||||
print("错误: Cookie获取失败")
|
||||
return False
|
||||
|
||||
print(f"成功: Cookie长度 {len(cookie)}")
|
||||
print(f"Cookie内容: {cookie}")
|
||||
|
||||
# 步骤2: 使用Cookie下载视频
|
||||
print("\n[步骤2] 使用Cookie下载视频")
|
||||
|
||||
test_url = "https://v.douyin.com/R5doyi5_cTk/"
|
||||
save_dir = r"C:\Users\Administrator\Desktop\TestDownload"
|
||||
|
||||
cmd = [
|
||||
r"C:\Program Files\Python311\python.exe",
|
||||
"-m", "f2", "dy",
|
||||
"-M", "one",
|
||||
"-u", test_url,
|
||||
"-p", save_dir,
|
||||
"-k", cookie,
|
||||
]
|
||||
|
||||
print(f"下载链接: {test_url}")
|
||||
print(f"保存目录: {save_dir}")
|
||||
print("开始下载...\n")
|
||||
|
||||
try:
|
||||
env = os.environ.copy()
|
||||
env['PYTHONIOENCODING'] = 'utf-8'
|
||||
|
||||
process = subprocess.Popen(
|
||||
cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
env=env,
|
||||
text=True,
|
||||
encoding='utf-8',
|
||||
errors='replace'
|
||||
)
|
||||
|
||||
# 实时输出
|
||||
for line in process.stdout:
|
||||
print(line.rstrip())
|
||||
|
||||
process.wait()
|
||||
|
||||
if process.returncode == 0:
|
||||
print("\n成功: 下载完成!")
|
||||
|
||||
# 检查文件是否存在
|
||||
if os.path.exists(save_dir):
|
||||
files = [f for f in os.listdir(save_dir) if f.endswith('.mp4')]
|
||||
if files:
|
||||
print(f"下载的文件: {files}")
|
||||
for f in files:
|
||||
filepath = os.path.join(save_dir, f)
|
||||
size = os.path.getsize(filepath) / 1024 / 1024
|
||||
print(f" - {f} ({size:.2f} MB)")
|
||||
|
||||
return True
|
||||
else:
|
||||
print(f"\n错误: 下载失败,返回码 {process.returncode}")
|
||||
return False
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
print("错误: 下载超时")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"错误: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_auto_download()
|
||||
|
||||
print("\n" + "="*60)
|
||||
if success:
|
||||
print("测试结果: 成功 - 自动Cookie功能正常工作")
|
||||
else:
|
||||
print("测试结果: 失败")
|
||||
print("="*60)
|
||||
Reference in New Issue
Block a user