diff --git a/src/main/java/DiscordDownloadBot.java b/src/main/java/DiscordDownloadBot.java index 402e377..35a13a5 100644 --- a/src/main/java/DiscordDownloadBot.java +++ b/src/main/java/DiscordDownloadBot.java @@ -24,6 +24,7 @@ import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.security.MessageDigest; import java.util.ArrayList; import java.util.HashMap; @@ -148,9 +149,12 @@ public class DiscordDownloadBot extends ListenerAdapter { .put("progress", percent).put("speed", speed == null ? "" : speed)); }; Result result = download(url, saveDir, progress); + String caption = readCaption(saveDir); System.out.println("🌐 [HTTP] " + folder + " ← " + url + " : " + result); if (!callbackUrl.isEmpty()) { - sendCallback(callbackUrl, DataObject.empty().put("folder", folder).put("result", result.name())); + DataObject done = DataObject.empty().put("folder", folder).put("result", result.name()); + if (caption != null) done.put("description", caption); + sendCallback(callbackUrl, done); } }); send(ex, 202, DataObject.empty().put("status", "accepted").put("folder", folder)); @@ -303,7 +307,9 @@ public class DiscordDownloadBot extends ListenerAdapter { try { progressMessage = channel.sendMessage("⏳ **λ‹€μš΄λ‘œλ“œ μ‹œλ„ 쀑...**").complete(); String folder = new File(saveDir).getName(); - switch (download(url, saveDir, null)) { + Result result = download(url, saveDir, null); + readCaption(saveDir); // λ””μŠ€μ½”λ“œλŠ” μ„€λͺ…글을 μ•ˆ μ“°μ§€λ§Œ .json νŒŒμΌμ€ 정리 + switch (result) { case VIDEO: progressMessage.editMessage("βœ… **λ™μ˜μƒ λ‹€μš΄λ‘œλ“œ μ™„λ£Œ!**\nπŸ“‚ 폴더: `" + folder + "`").queue(); break; @@ -389,6 +395,7 @@ public class DiscordDownloadBot extends ListenerAdapter { cmdList.add(COOKIES_PATH); cmdList.add("-D"); // ν•˜μœ„ 폴더 없이 saveDir에 λ°”λ‘œ μ €μž₯ cmdList.add(saveDir); + cmdList.add("--write-metadata"); // νŒŒμΌλ§ˆλ‹€ "파일λͺ….json" (κ²Œμ‹œλ¬Ό μ„€λͺ…글이 λ“€μ–΄ 있음) β†’ readCaption이 읽고 지움 // 파일λͺ… "이름_μ½”λ“œ_Video1.mp4" / "이름_μ½”λ“œ_Image2.jpg" (λ°±μ—”λ“œκ°€ 이 κ·œμΉ™μœΌλ‘œ 제λͺ©μ„ λ½‘μŒ) // num = κ²Œμ‹œλ¬Ό μ•ˆ μˆœμ„œ. mp4λ©΄ Video, μ•„λ‹ˆλ©΄ Image (μΈμŠ€νƒ€ μ˜μƒμ€ 항상 mp4) cmdList.add("-o"); @@ -420,6 +427,25 @@ public class DiscordDownloadBot extends ListenerAdapter { } } + // μΈμŠ€νƒ€ κ²Œμ‹œλ¬Ό μ„€λͺ…κΈ€: gallery-dl이 남긴 .jsonμ—μ„œ κΊΌλ‚΄κ³ , .json은 지움 (κ²Œμ‹œνŒμ— 파일둜 보이지 μ•Šκ²Œ) + // μ„€λͺ…글이 μ—†μœΌλ©΄(유튜브 λ“±) null + private static String readCaption(String saveDir) { + File[] jsons = new File(saveDir).listFiles((d, n) -> n.endsWith(".json")); + if (jsons == null) return null; + String caption = null; + for (File f : jsons) { + if (caption == null) { + try { + String text = DataObject.fromJson(new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8)) + .getString("description", "").trim(); + if (!text.isEmpty()) caption = text; + } catch (Exception e) { /* 읽기 μ‹€νŒ¨λ©΄ μ„€λͺ… 없이 μ§„ν–‰ */ } + } + f.delete(); + } + return caption; + } + // 파일 생성 확인 ν•¨μˆ˜ private boolean checkFileExists(String saveDir, String keyword) { File dir = new File(saveDir);