Compare commits
5 Commits
7c1000ba55
...
617d1ebacf
Author | SHA1 | Date |
---|---|---|
|
617d1ebacf | |
|
c31e32c96e | |
|
3437887551 | |
|
03ffc9d994 | |
|
3ee4f8fde5 |
|
@ -54,3 +54,6 @@ nbbuild/
|
||||||
dist/
|
dist/
|
||||||
nbdist/
|
nbdist/
|
||||||
.nb-gradle/
|
.nb-gradle/
|
||||||
|
|
||||||
|
# Secrets!
|
||||||
|
secrets.yml
|
5
pom.xml
5
pom.xml
|
@ -37,11 +37,6 @@
|
||||||
<artifactId>JDA</artifactId>
|
<artifactId>JDA</artifactId>
|
||||||
<version>4.3.0_277</version>
|
<version>4.3.0_277</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>io.humble</groupId>
|
|
||||||
<artifactId>humble-video-all</artifactId>
|
|
||||||
<version>0.3.0</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
|
|
|
@ -6,8 +6,12 @@
|
||||||
package moe.nekojimi.chords;
|
package moe.nekojimi.chords;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.attribute.FileAttribute;
|
||||||
|
import java.nio.file.attribute.PosixFilePermission;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
|
@ -20,6 +24,9 @@ import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
import javax.json.Json;
|
||||||
|
import javax.json.JsonObject;
|
||||||
|
import javax.json.JsonReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -33,6 +40,8 @@ public class Downloader implements Consumer<Song>
|
||||||
private Consumer<Song> next;
|
private Consumer<Song> next;
|
||||||
private BiConsumer<Song, Exception> messageHandler;
|
private BiConsumer<Song, Exception> messageHandler;
|
||||||
|
|
||||||
|
private File downloadDir = null;
|
||||||
|
|
||||||
public Downloader()
|
public Downloader()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -42,6 +51,7 @@ public class Downloader implements Consumer<Song>
|
||||||
public void accept(Song song)
|
public void accept(Song song)
|
||||||
{
|
{
|
||||||
downloadQueue.add(song);
|
downloadQueue.add(song);
|
||||||
|
getInfo(song);
|
||||||
exec.submit(new Runnable()
|
exec.submit(new Runnable()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
|
@ -57,29 +67,49 @@ public class Downloader implements Consumer<Song>
|
||||||
this.next = next;
|
this.next = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void getInfo(Song song)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
String cmd = "/usr/bin/youtube-dl --skip-download --print-json " + song.getUrl().toString();
|
||||||
|
Process exec = runCommand(cmd, 5);
|
||||||
|
InputStream input = exec.getInputStream();
|
||||||
|
JsonReader reader = Json.createReader(input);
|
||||||
|
JsonObject object = reader.readObject();
|
||||||
|
if (song.getTitle() == null)
|
||||||
|
song.setTitle(object.getString("title", null));
|
||||||
|
if (song.getArtist() == null)
|
||||||
|
song.setArtist(object.getString("uploader", null));
|
||||||
|
} catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private File getDownloadDir() throws IOException
|
||||||
|
{
|
||||||
|
if (downloadDir == null || !downloadDir.exists() || !downloadDir.canWrite())
|
||||||
|
downloadDir = Files.createTempDirectory("chords").toFile();
|
||||||
|
return downloadDir;
|
||||||
|
}
|
||||||
|
|
||||||
private void download(Song song)
|
private void download(Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
messageHandler.accept(song, null);
|
messageHandler.accept(song, null);
|
||||||
String cmd = "/usr/bin/youtube-dl -x -f=worstaudio/worst --audio-format=wav --no-playlist --write-info-json " + song.getUrl().toString();
|
String cmd = "/usr/bin/youtube-dl -x -f=worstaudio/worst --audio-format=wav --no-playlist -o " + getDownloadDir().getAbsolutePath() + "/%(title)s.%(ext)s " + song.getUrl().toString();
|
||||||
System.out.println("Running command: " + cmd);
|
Process exec = runCommand(cmd, 300);
|
||||||
// Process exec = Runtime.getRuntime().exec().split(" "));
|
|
||||||
Process exec = new ProcessBuilder(cmd.split(" ")).redirectOutput(ProcessBuilder.Redirect.PIPE).start();
|
|
||||||
boolean done = exec.waitFor(300, TimeUnit.SECONDS);
|
|
||||||
if (!done)
|
|
||||||
{
|
|
||||||
exec.destroyForcibly();
|
|
||||||
throw new RuntimeException("Took too long to download, giving up.");
|
|
||||||
}
|
|
||||||
InputStream in = exec.getInputStream();
|
InputStream in = exec.getInputStream();
|
||||||
String output = new String(in.readAllBytes(), Charset.defaultCharset());
|
String output = new String(in.readAllBytes(), Charset.defaultCharset());
|
||||||
|
String error = new String(exec.getErrorStream().readAllBytes(), Charset.defaultCharset());
|
||||||
System.out.println(output);
|
System.out.println(output);
|
||||||
Matcher matcher = Pattern.compile("Destination: (.*\\.wav)").matcher(output);
|
Matcher matcher = Pattern.compile("Destination: (.*\\.wav)").matcher(output);
|
||||||
if (matcher.find())
|
if (matcher.find())
|
||||||
song.setLocation(new File(matcher.group(1)));
|
song.setLocation(new File(matcher.group(1)));
|
||||||
else if (exec.exitValue() != 0)
|
else if (exec.exitValue() != 0)
|
||||||
throw new RuntimeException("youtube-dl failed with error " + exec.exitValue());
|
throw new RuntimeException("youtube-dl failed with error " + exec.exitValue() + ", output:\n" + error);
|
||||||
// return true;
|
// return true;
|
||||||
|
|
||||||
if (next != null)
|
if (next != null)
|
||||||
|
@ -95,6 +125,20 @@ public class Downloader implements Consumer<Song>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Process runCommand(String cmd, int timeoutSecs) throws RuntimeException, IOException, InterruptedException
|
||||||
|
{
|
||||||
|
System.out.println("Running command: " + cmd);
|
||||||
|
// Process exec = Runtime.getRuntime().exec().split(" "));
|
||||||
|
Process exec = new ProcessBuilder(cmd.split(" ")).redirectOutput(ProcessBuilder.Redirect.PIPE).redirectError(ProcessBuilder.Redirect.PIPE).start();
|
||||||
|
boolean done = exec.waitFor(timeoutSecs, TimeUnit.SECONDS);
|
||||||
|
if (!done)
|
||||||
|
{
|
||||||
|
exec.destroyForcibly();
|
||||||
|
throw new RuntimeException("Took too long, giving up.");
|
||||||
|
}
|
||||||
|
return exec;
|
||||||
|
}
|
||||||
|
|
||||||
public BiConsumer<Song, Exception> getMessageHandler()
|
public BiConsumer<Song, Exception> getMessageHandler()
|
||||||
{
|
{
|
||||||
return messageHandler;
|
return messageHandler;
|
||||||
|
|
|
@ -33,7 +33,7 @@ import net.dv8tion.jda.api.utils.cache.CacheFlag;
|
||||||
public class Main extends ListenerAdapter
|
public class Main extends ListenerAdapter
|
||||||
{
|
{
|
||||||
|
|
||||||
private static final double SEARCH_SCORE_THRESHOLD_AUTOPLAY = 0.9;
|
private static final double SEARCH_SCORE_THRESHOLD_AUTOPLAY = 9999; // disable autoplay it sucks
|
||||||
private static final double SEARCH_SCORE_THRESHOLD_DISPLAY = 0.6;
|
private static final double SEARCH_SCORE_THRESHOLD_DISPLAY = 0.6;
|
||||||
|
|
||||||
private MusicHandler musicHandler;
|
private MusicHandler musicHandler;
|
||||||
|
@ -83,15 +83,15 @@ public class Main extends ListenerAdapter
|
||||||
downloader.setMessageHandler((Song song, Exception ex) ->
|
downloader.setMessageHandler((Song song, Exception ex) ->
|
||||||
{
|
{
|
||||||
TextChannel channel = jda.getTextChannelById(song.getRequestedIn());
|
TextChannel channel = jda.getTextChannelById(song.getRequestedIn());
|
||||||
String bracketNo = "[" + song.getNumber() + "] ";
|
// String bracketNo = "[" + song.getNumber() + "] ";
|
||||||
if (channel != null)
|
if (channel != null)
|
||||||
if (ex == null)
|
if (ex == null)
|
||||||
if (song.getLocation() != null)
|
if (song.getLocation() != null)
|
||||||
channel.sendMessage(bracketNo + "Finished downloading " + song + " for " + song.getRequestedBy() + ", added to queue!").queue();
|
channel.sendMessage(/*bracketNo + */"Finished downloading " + song + " for " + song.getRequestedBy() + ", added to queue!").queue();
|
||||||
else
|
else
|
||||||
channel.sendMessage(bracketNo + "Now downloading " + song + " for " + song.getRequestedBy() + " ...").queue();
|
channel.sendMessage(/*bracketNo + */"Now downloading " + song + " for " + song.getRequestedBy() + " ...").queue();
|
||||||
else
|
else
|
||||||
channel.sendMessage(bracketNo + "Failed to download " + song + " for " + song.getRequestedBy() + "! Reason: " + ex.getMessage()).queue();
|
channel.sendMessage(/*bracketNo + */"Failed to download " + song + " for " + song.getRequestedBy() + "! Reason: " + ex.getMessage()).queue();
|
||||||
|
|
||||||
});
|
});
|
||||||
searcher = MetaSearcher.loadYAML(new File("searchproviders.yml"));
|
searcher = MetaSearcher.loadYAML(new File("searchproviders.yml"));
|
||||||
|
@ -220,7 +220,7 @@ public class Main extends ListenerAdapter
|
||||||
if (index >= 1 && index <= size)
|
if (index >= 1 && index <= size)
|
||||||
{
|
{
|
||||||
Result res = lastSearchResults.get(index - 1);
|
Result res = lastSearchResults.get(index - 1);
|
||||||
queueDownload(res.getLink(), event);
|
queueDownload(res, event);
|
||||||
// event.getChannel().sendMessage("Song removed.").queue();
|
// event.getChannel().sendMessage("Song removed.").queue();
|
||||||
} else if (size > 1)
|
} else if (size > 1)
|
||||||
event.getChannel().sendMessage("That's not a number between 1 and " + size + "!").queue();
|
event.getChannel().sendMessage("That's not a number between 1 and " + size + "!").queue();
|
||||||
|
@ -282,7 +282,7 @@ public class Main extends ListenerAdapter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void queueDownload(final URL url, GuildMessageReceivedEvent event)
|
private Song queueDownload(final URL url, GuildMessageReceivedEvent event)
|
||||||
{
|
{
|
||||||
Song song = new Song(url);
|
Song song = new Song(url);
|
||||||
song.setRequestedBy(event.getAuthor().getName());
|
song.setRequestedBy(event.getAuthor().getName());
|
||||||
|
@ -290,6 +290,16 @@ public class Main extends ListenerAdapter
|
||||||
song.setNumber(trackNumber);
|
song.setNumber(trackNumber);
|
||||||
trackNumber++;
|
trackNumber++;
|
||||||
downloader.accept(song);
|
downloader.accept(song);
|
||||||
|
return song;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Song queueDownload(Result res, GuildMessageReceivedEvent event)
|
||||||
|
{
|
||||||
|
Song song = queueDownload(res.getLink(), event);
|
||||||
|
song.setArtist(res.getArtist());
|
||||||
|
song.setTitle(res.getTitle());
|
||||||
|
song.setNumber(trackNumber);
|
||||||
|
return song;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onRestartCommand(GuildMessageReceivedEvent event)
|
private void onRestartCommand(GuildMessageReceivedEvent event)
|
||||||
|
|
|
@ -103,10 +103,17 @@ public class Song
|
||||||
@Override
|
@Override
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
|
String ret = "";
|
||||||
|
if (artist != null && !artist.isEmpty())
|
||||||
|
ret += artist + " - ";
|
||||||
|
|
||||||
if (title != null && !title.isEmpty())
|
if (title != null && !title.isEmpty())
|
||||||
return title;
|
ret += title;
|
||||||
else
|
|
||||||
return "track " + number;
|
if (ret.isEmpty())
|
||||||
|
ret = "track " + number;
|
||||||
|
|
||||||
|
return "[" + number + "] " + ret;
|
||||||
// return url.toExternalForm();
|
// return url.toExternalForm();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue