parent
2f7bb13dc6
commit
c8810ca271
@ -0,0 +1,99 @@ |
|||||||
|
/* |
||||||
|
* To change this license header, choose License Headers in Project Properties. |
||||||
|
* To change this template file, choose Tools | Templates |
||||||
|
* and open the template in the editor. |
||||||
|
*/ |
||||||
|
package moe.nekojimi.chords; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.nio.charset.Charset; |
||||||
|
import java.util.concurrent.LinkedBlockingDeque; |
||||||
|
import java.util.concurrent.ThreadPoolExecutor; |
||||||
|
import java.util.concurrent.TimeUnit; |
||||||
|
import java.util.function.BiConsumer; |
||||||
|
import java.util.function.Consumer; |
||||||
|
import java.util.logging.Level; |
||||||
|
import java.util.logging.Logger; |
||||||
|
import java.util.regex.Matcher; |
||||||
|
import java.util.regex.Pattern; |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* @author jimj316 |
||||||
|
*/ |
||||||
|
public class Downloader implements Consumer<Song> |
||||||
|
{ |
||||||
|
// private final Queue<Song> downloadQueue = new LinkedBlockingDeque<>();
|
||||||
|
private final LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>(); |
||||||
|
private final ThreadPoolExecutor exec = new ThreadPoolExecutor(1, 4, 30, TimeUnit.SECONDS, workQueue); |
||||||
|
private Consumer<Song> next; |
||||||
|
private BiConsumer<Song, Exception> messageHandler; |
||||||
|
|
||||||
|
public Downloader() |
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void accept(Song song) |
||||||
|
{ |
||||||
|
exec.submit(() -> |
||||||
|
{ |
||||||
|
download(song); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
public void setNext(Consumer<Song> next) |
||||||
|
{ |
||||||
|
this.next = next; |
||||||
|
} |
||||||
|
|
||||||
|
private void download(Song song) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
messageHandler.accept(song, null); |
||||||
|
String cmd = "/usr/bin/youtube-dl -x --audio-format=wav --no-playlist --write-info-json " + song.getUrl().toString(); |
||||||
|
System.out.println("Running command: " + cmd); |
||||||
|
// Process exec = Runtime.getRuntime().exec().split(" "));
|
||||||
|
Process exec = new ProcessBuilder(cmd.split(" ")).redirectOutput(ProcessBuilder.Redirect.PIPE).start(); |
||||||
|
boolean done = exec.waitFor(30, TimeUnit.SECONDS); |
||||||
|
if (!done) |
||||||
|
{ |
||||||
|
exec.destroyForcibly(); |
||||||
|
throw new RuntimeException("Took too long to download, giving up."); |
||||||
|
} |
||||||
|
InputStream in = exec.getInputStream(); |
||||||
|
String output = new String(in.readAllBytes(), Charset.defaultCharset()); |
||||||
|
System.out.println(output); |
||||||
|
Matcher matcher = Pattern.compile("Destination: (.*\\.wav)").matcher(output); |
||||||
|
if (matcher.find()) |
||||||
|
song.setLocation(new File(matcher.group(1))); |
||||||
|
else if (exec.exitValue() != 0) |
||||||
|
throw new RuntimeException("youtube-dl failed with error " + exec.exitValue()); |
||||||
|
// return true;
|
||||||
|
|
||||||
|
if (next != null) |
||||||
|
next.accept(song); |
||||||
|
messageHandler.accept(song, null); |
||||||
|
} catch (Exception ex) |
||||||
|
{ |
||||||
|
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex); |
||||||
|
if (messageHandler != null) |
||||||
|
messageHandler.accept(song, ex); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public BiConsumer<Song, Exception> getMessageHandler() |
||||||
|
{ |
||||||
|
return messageHandler; |
||||||
|
} |
||||||
|
|
||||||
|
public void setMessageHandler(BiConsumer<Song, Exception> messageHandler) |
||||||
|
{ |
||||||
|
this.messageHandler = messageHandler; |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue