Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
🎨 Improving IDM downloading process using new POC theory, update yout…
Browse files Browse the repository at this point in the history
…ube-dl.exe, update log4j2.xml
  • Loading branch information
CXwudi committed May 8, 2020
1 parent 6cd5a8f commit f120dd6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
3 changes: 2 additions & 1 deletion data/system_config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
data.dir=data
lib.dir=lib

#other exe location
#tells progream where to find these exe location
#for IDM users, you might want to change the idm path to your IDM location
youtube-dl=${lib.dir}/youtube-dl.exe
mp4box=${lib.dir}/mp4box.exe
idm=C:/Program Files (x86)/Internet Download Manager/IDMan.exe
Expand Down
Binary file modified lib/youtube-dl.exe
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public static AbstractVideoDownloader getDefaultDownloader(){
*/
public DownloadStatus downloadVocaloidPV(VsongDownloadTask task) {
Objects.requireNonNull(task);
if (task.getSong().getId().equals("")) return FAIL_INITIAL;
if (task.getSong().getId().equals("")) {
return FAIL_INITIAL;
}
var song = task.getSong();
var file = task.getOutputDir();
var songFileName = task.getFileName();
Expand All @@ -59,7 +61,7 @@ public DownloadStatus downloadVocaloidPV(VsongDownloadTask task) {
// }

} catch (IOException e) {
logger.error("IOException: ", e);
logger.error("IOException in AbstractVideoDownloader.downloadVocaloidPV: ", e);
return FAIL_INITIAL;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class IDMwithYoutubeDLDownloader extends AbstractVideoDownloader {

private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private static final String idmProcessName = "IDMan.exe";

/**
* The second honor function that first use youtube-dl to get a url of the video,
* then use IDM to download the video from the url
Expand All @@ -41,8 +43,7 @@ protected DownloadStatus downloadImpl(Vsong song, File dir, String fileName) thr
var urlStrBuilder = new StringBuilder();
if (getUrl(song.getId(), urlStrBuilder)){
// Call IDM to download url
var status = downloadUsingIDM(urlStrBuilder.toString(), dir, fileName);
return status;
return downloadUsingIdm(urlStrBuilder.toString(), dir, fileName);
} else {
return DownloadStatus.FAIL_INITIAL;
}
Expand All @@ -57,16 +58,16 @@ protected DownloadStatus downloadImpl(Vsong song, File dir, String fileName) thr
*/
private boolean getUrl(String smId, StringBuilder urlStrBuilder) throws IOException {
logger.info("start getting video URL for {}", smId);
var youtubeDL_getUrlPB = new ProcessBuilder(
var youtubeDlGetUrlPb = new ProcessBuilder(
Config.getYoutubeDlFile().getAbsolutePath(),
"-v",
"--username", '"' + Config.getEmail() + '"',
"--password", '"' + Config.getPassword() + '"',
"https://www.nicovideo.jp/watch/" + smId,
"--get-url",
"-f", "\"best[height<=1080]\""); //for IDM, we trust it to download 1080p video for real fast
youtubeDL_getUrlPB.redirectError(ProcessBuilder.Redirect.INHERIT);
var getUrlProcess = youtubeDL_getUrlPB.start();
youtubeDlGetUrlPb.redirectError(ProcessBuilder.Redirect.INHERIT);
var getUrlProcess = youtubeDlGetUrlPb.start();

try (var output = new BufferedReader(new InputStreamReader(getUrlProcess.getInputStream()))){
var url = output.readLine();
Expand All @@ -87,30 +88,62 @@ private boolean getUrl(String smId, StringBuilder urlStrBuilder) throws IOExcept
* @return {@link DownloadStatus#SUCCESS} if download success
* @throws IOException if IDM process throw exception, or fail to delete existing file
*/
private DownloadStatus downloadUsingIDM(String url, File dir, String fileName) throws IOException {
private DownloadStatus downloadUsingIdm(String url, File dir, String fileName) throws IOException {
var targetFile = new File(dir, fileName);
if (targetFile.exists()){
logger.warn("{} already exists, now deleted and re-download", targetFile);
Files.delete(targetFile.toPath());
}
var idmPath = Config.getIdmFile().toString();

if (!terminateExistingIdmProcess(idmPath)) {
return DownloadStatus.FAIL_INITIAL;
}

logger.info("CXwudi and Miku are invoking IDM to download it 😂");
var idmPB = new ProcessBuilder(
Config.getIdmFile().toString(),
idmPath,
"/d", url,
"/p", dir.getAbsolutePath(),
"/f", fileName,
"/n");
idmPB.start(); //this process will fork a new process and return immediately

"/n", "/q"); //no question and quite after download
var idmProcess = idmPB.inheritIO().start();
try {
return waitUntilFileIsHere(targetFile);
idmProcess.waitFor();
} catch (InterruptedException e) {
logger.error("Interrupted in IDMwithYoutubeDLDownloader.waitUntilFileIsHere", e);
idmProcess.destroy();
logger.error("Interrupted in IDMwithYoutubeDLDownloader.downloadUsingIdm", e);
System.exit(-1);
}
if (Files.exists(targetFile.toPath())){
return DownloadStatus.SUCCESS;
} else {
return DownloadStatus.FAIL_DOWNLOAD;
}

}

return DownloadStatus.FAIL_DOWNLOAD;
/**
* To terminate IDM,
* used for make sure we are synchronized when calling IDM, the existing IDM process must be terminated
* @param idmPath path of IDM
* @return {@code true} if termination success
*/
private boolean terminateExistingIdmProcess(String idmPath) {
try {
var taskKillPb = new ProcessBuilder("taskkill", "/F", "/FI", String.format("\"imagename eq %s\"", idmProcessName));
logger.info("killing IDM with {}", taskKillPb.command());
var p = taskKillPb.inheritIO().start();
p.waitFor();
return true;
} catch (IOException e) {
logger.error("Fail to kill IDM process", e);
return false;
} catch (InterruptedException e) {
logger.error("Interrupted in IDMwithYoutubeDLDownloader.terminateExistingIdmProcess", e);
System.exit(-1);
return false; // let compiler happy
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

<Loggers>
<!-- global logger setup-->
<Root level="trace">
<Root level="info">
<AppenderRef ref="stdout" />
</Root>
<Logger name="com.cxwudi" level="debug" additivity="false">
<AppenderRef ref="stdout"/>
</Logger>
</Loggers>

</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.cxwudi.niconico_videodownloader.util.DownloadStatus;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

class IDMwithYoutubeDLDownloaderTest extends DownloaderTestSupport{

Expand All @@ -14,7 +14,7 @@ void testDownload(){
var downloader = new IDMwithYoutubeDLDownloader();
VsongDownloadTask task = getSampleVsongDownloadTask();
var status = downloader.downloadVocaloidPV(task);
assertTrue(status == DownloadStatus.SUCCESS);
assertEquals(status, DownloadStatus.SUCCESS);
}

}

0 comments on commit f120dd6

Please sign in to comment.