Files
dc-docs/skills/dingtalk-feishu-collector/scripts/step4_download_files.py
T
2026-07-11 12:30:54 +08:00

68 lines
3.2 KiB
Python

"""Step 4: 下载钉钉群文件附件"""
import argparse, json, os, subprocess, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from paths import LINKS_DIR, DOWNLOAD_DIR, find_dws
HTML_MD = {".html",".htm",".md",".markdown",".txt"}
def main():
p = argparse.ArgumentParser(); p.add_argument("--incremental",action="store_true"); args = p.parse_args()
path = os.path.join(LINKS_DIR,"all_file_attachments.json")
if not os.path.isfile(path): print("ERROR: 先运行 step2"); sys.exit(1)
with open(path,"r",encoding="utf-8") as f: files = json.load(f)
print(f"附件: {len(files)} 个")
dws = find_dws()
new = 0
for fi in files:
fid, name = fi.get("fileId",""), fi.get("name","unknown")
if not fid: continue
safe_name = os.path.basename(name)
ext = os.path.splitext(name)[1].lower()
subdir = "html-md" if ext in HTML_MD else "other"
out = os.path.join(DOWNLOAD_DIR, subdir)
os.makedirs(out, exist_ok=True)
if args.incremental and os.path.isfile(os.path.join(out, safe_name)):
print(f" SKIP {safe_name}")
continue
print(f" {name} -> {subdir}/")
try:
r = subprocess.run([dws,"drive","download","--node",fid,"--output",out],capture_output=True,timeout=120)
if r.returncode==0: new+=1
else: print(f" FAIL")
except Exception as e: print(f" ERR: {e}")
media_path = os.path.join(LINKS_DIR, "all_chat_media.json")
media = []
if os.path.isfile(media_path):
with open(media_path, "r", encoding="utf-8") as f: media = json.load(f)
media_dir = os.path.join(DOWNLOAD_DIR, "chat-media")
os.makedirs(media_dir, exist_ok=True)
media_new = 0
for item in media:
resource_id = item.get("resourceId", "")
message_id = item.get("messageId", "")
conversation_id = item.get("conversationId", "")
if not resource_id or not message_id or not conversation_id:
print(f" MEDIA FAIL 缺少下载标识: {message_id or resource_id}")
continue
ext = ".mp4" if item.get("mediaType") == "video" else ".jpg"
stamp = item.get("time", "unknown").replace(":", "").replace(" ", "_").replace("-", "")
safe_id = "".join(ch for ch in message_id if ch.isalnum())[-12:] or "media"
output = os.path.join(media_dir, f"{stamp}_{safe_id}{ext}")
if args.incremental and os.path.isfile(output) and os.path.getsize(output) > 0:
print(f" SKIP {os.path.basename(output)}")
continue
cmd = [dws, "chat", "message", "download-media", "--type", "mediaId",
"--resource-id", resource_id, "--message-id", message_id,
"--open-conversation-id", conversation_id, "--output", output, "-y"]
try:
r = subprocess.run(cmd, capture_output=True, timeout=120)
if r.returncode == 0 and os.path.isfile(output) and os.path.getsize(output) > 0:
media_new += 1
else:
print(f" MEDIA FAIL {message_id}")
except Exception as e: print(f" MEDIA ERR {message_id}: {e}")
print(f"\n下载附件 {new} 个、聊天媒体 {media_new} 个 -> {DOWNLOAD_DIR}")
if __name__ == "__main__": main()