Files
eeclass-video-downloader/download.py
2026-04-23 02:44:00 +08:00

48 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import requests
import os
# ==========================================
# 配置區域 (請將 GetEnv.js 的輸出貼在下方)
# ==========================================
# url = "https://eeclass.yourschool.edu.tw/sysdata/doc/X/XXXXXXXXXXXXXXXXXX/video/video_hd.mp4"
# headers = {
# "Referer": "https://eeclass.yourschool.edu.tw/media/doc/*",
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0",
# "Cookie": "PHPSESSID=; accesstoken=; account="
# }
# filename = "filename.mp4"
# ==========================================
def download_video(video_url, req_headers, output_name):
print(f"🚀 準備下載: {output_name}")
try:
response = requests.get(video_url, headers=req_headers, stream=True, timeout=30)
# 檢查是否 404 或其他錯誤
if response.status_code == 404:
print("❌ 錯誤: 伺服器回傳 404。可能是該畫質檔案不存在請試著更換網址。")
return
response.raise_for_status()
total_size = int(response.headers.get('content-length', 0))
downloaded = 0
with open(output_name, "wb") as f:
for chunk in response.iter_content(chunk_size=1024*1024):
if chunk:
f.write(chunk)
downloaded += len(chunk)
if total_size > 0:
percent = (downloaded / total_size) * 100
print(f"\r📥 進度: {percent:.2f}% ({downloaded/(1024*1024):.1f}MB / {total_size/(1024*1024):.1f}MB)", end="")
print(f"\n✅ 下載完成!檔案已儲存於: {os.path.abspath(output_name)}")
except Exception as e:
print(f"\n❌ 發生錯誤: {e}")
if __name__ == "__main__":
download_video(url, headers, filename)