Compare commits
3 Commits
579fb01a20
...
66cdca8f0d
Author | SHA1 | Date |
---|---|---|
|
66cdca8f0d | |
|
cf24072f35 | |
|
b7f0794bc4 |
|
@ -8,6 +8,9 @@ package moe.nekojimi.chords;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
import java.util.concurrent.LinkedBlockingDeque;
|
import java.util.concurrent.LinkedBlockingDeque;
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -24,9 +27,9 @@ import java.util.regex.Pattern;
|
||||||
*/
|
*/
|
||||||
public class Downloader implements Consumer<Song>
|
public class Downloader implements Consumer<Song>
|
||||||
{
|
{
|
||||||
// private final Queue<Song> downloadQueue = new LinkedBlockingDeque<>();
|
private final List<Song> downloadQueue = new LinkedList<>();
|
||||||
private final LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>();
|
private final LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>();
|
||||||
private final ThreadPoolExecutor exec = new ThreadPoolExecutor(1, 4, 30, TimeUnit.SECONDS, workQueue);
|
private final ThreadPoolExecutor exec = new ThreadPoolExecutor(2, 4, 30, TimeUnit.SECONDS, workQueue);
|
||||||
private Consumer<Song> next;
|
private Consumer<Song> next;
|
||||||
private BiConsumer<Song, Exception> messageHandler;
|
private BiConsumer<Song, Exception> messageHandler;
|
||||||
|
|
||||||
|
@ -38,9 +41,14 @@ public class Downloader implements Consumer<Song>
|
||||||
@Override
|
@Override
|
||||||
public void accept(Song song)
|
public void accept(Song song)
|
||||||
{
|
{
|
||||||
exec.submit(() ->
|
downloadQueue.add(song);
|
||||||
|
exec.submit(new Runnable()
|
||||||
{
|
{
|
||||||
download(song);
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
download(song);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,12 +84,14 @@ public class Downloader implements Consumer<Song>
|
||||||
|
|
||||||
if (next != null)
|
if (next != null)
|
||||||
next.accept(song);
|
next.accept(song);
|
||||||
|
downloadQueue.remove(song);
|
||||||
messageHandler.accept(song, null);
|
messageHandler.accept(song, null);
|
||||||
} catch (Exception ex)
|
} catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
if (messageHandler != null)
|
if (messageHandler != null)
|
||||||
messageHandler.accept(song, ex);
|
messageHandler.accept(song, ex);
|
||||||
|
downloadQueue.remove(song);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,4 +105,9 @@ public class Downloader implements Consumer<Song>
|
||||||
this.messageHandler = messageHandler;
|
this.messageHandler = messageHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Song> getDownloadQueue()
|
||||||
|
{
|
||||||
|
return downloadQueue;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,17 @@
|
||||||
*/
|
*/
|
||||||
package moe.nekojimi.chords;
|
package moe.nekojimi.chords;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import javax.security.auth.login.LoginException;
|
import javax.security.auth.login.LoginException;
|
||||||
|
import moe.nekojimi.musicsearcher.Query;
|
||||||
|
import moe.nekojimi.musicsearcher.Result;
|
||||||
|
import moe.nekojimi.musicsearcher.providers.MetaSearcher;
|
||||||
|
import moe.nekojimi.musicsearcher.providers.Searcher;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
import net.dv8tion.jda.api.entities.*;
|
import net.dv8tion.jda.api.entities.*;
|
||||||
|
@ -26,10 +33,18 @@ 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_DISPLAY = 0.6;
|
||||||
|
|
||||||
private MusicHandler musicHandler;
|
private MusicHandler musicHandler;
|
||||||
private Downloader downloader;
|
private final Downloader downloader;
|
||||||
|
private final Searcher searcher;
|
||||||
private JDA jda;
|
private JDA jda;
|
||||||
|
|
||||||
|
private List<Result> lastSearchResults;
|
||||||
|
|
||||||
|
private int trackNumber = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param args the command line arguments
|
* @param args the command line arguments
|
||||||
*/
|
*/
|
||||||
|
@ -68,16 +83,18 @@ 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() + "] ";
|
||||||
if (channel != null)
|
if (channel != null)
|
||||||
if (ex == null)
|
if (ex == null)
|
||||||
if (song.getLocation() != null)
|
if (song.getLocation() != null)
|
||||||
channel.sendMessage("Finished downloading track for " + song.getRequestedBy() + ", added to queue!").queue();
|
channel.sendMessage(bracketNo + "Finished downloading " + song + " for " + song.getRequestedBy() + ", added to queue!").queue();
|
||||||
else
|
else
|
||||||
channel.sendMessage("Now downloading track for " + song.getRequestedBy() + " ...").queue();
|
channel.sendMessage(bracketNo + "Now downloading " + song + " for " + song.getRequestedBy() + " ...").queue();
|
||||||
else
|
else
|
||||||
channel.sendMessage("Failed to download track 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"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setJda(JDA jda)
|
public void setJda(JDA jda)
|
||||||
|
@ -148,7 +165,7 @@ public class Main extends ListenerAdapter
|
||||||
// onConnecting(channel, event.getChannel()); // Tell the user about our success
|
// onConnecting(channel, event.getChannel()); // Tell the user about our success
|
||||||
// } else
|
// } else
|
||||||
// onUnknownChannel(event.getChannel(), "your voice channel"); // Tell the user about our failure
|
// onUnknownChannel(event.getChannel(), "your voice channel"); // Tell the user about our failure
|
||||||
onJoinCommand(event, member.getGuild(), channel.getId());
|
onJoinCommand(event, member.getGuild(), channel.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -188,26 +205,93 @@ public class Main extends ListenerAdapter
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Song song = new Song(new URL(arg));
|
final URL url = new URL(arg);
|
||||||
song.setRequestedBy(event.getAuthor().getName());
|
queueDownload(url, event);
|
||||||
song.setRequestedIn(event.getChannel().getId());
|
|
||||||
// event.getChannel().sendMessage("Downloading ...").queue();
|
|
||||||
downloader.accept(song);
|
|
||||||
// boolean ok = downloadSong(song);
|
|
||||||
// if (ok)
|
|
||||||
// {
|
|
||||||
// musicHandler.addSong(song);
|
|
||||||
// if (musicHandler.isPlaying())
|
|
||||||
// musicHandler.setPlaying(true);
|
|
||||||
// event.getChannel().sendMessage("Downloaded and added to queue!").queue();
|
|
||||||
// }
|
|
||||||
|
|
||||||
} catch (MalformedURLException ex)
|
} catch (MalformedURLException mux)
|
||||||
{
|
{
|
||||||
event.getChannel().sendMessage("That's not a valid URL you idiot! " + ex.getMessage()).queue();
|
// not a URL, try parsing it as a search result
|
||||||
|
if (lastSearchResults != null && !lastSearchResults.isEmpty())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int index = Integer.parseInt(arg);
|
||||||
|
int size = lastSearchResults.size();
|
||||||
|
if (index >= 1 && index <= size)
|
||||||
|
{
|
||||||
|
Result res = lastSearchResults.get(index - 1);
|
||||||
|
queueDownload(res.getLink(), event);
|
||||||
|
// event.getChannel().sendMessage("Song removed.").queue();
|
||||||
|
} else if (size > 1)
|
||||||
|
event.getChannel().sendMessage("That's not a number between 1 and " + size + "!").queue();
|
||||||
|
else if (size == 1)
|
||||||
|
event.getChannel().sendMessage("There's only one song and that's not one of them!").queue();
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (NumberFormatException nfx)
|
||||||
|
{
|
||||||
|
// event.getChannel().sendMessage(arg + " isn't a number!").queue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise, try searching
|
||||||
|
CompletableFuture<List<Result>> search = searcher.search(Query.fullText(arg));
|
||||||
|
event.getChannel().sendMessage("Searching for \"" + arg + "\" ...").queue();
|
||||||
|
search.orTimeout(30, TimeUnit.SECONDS).whenCompleteAsync((List<Result> results, Throwable exec) ->
|
||||||
|
{
|
||||||
|
if (exec != null)
|
||||||
|
{
|
||||||
|
event.getChannel().sendMessage("Failed to search! Reason: " + exec.getMessage()).queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastSearchResults = results;
|
||||||
|
|
||||||
|
if (results.isEmpty())
|
||||||
|
{
|
||||||
|
event.getChannel().sendMessage("Found nothing! :(").queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (results.get(0).getScore() >= SEARCH_SCORE_THRESHOLD_AUTOPLAY)
|
||||||
|
{
|
||||||
|
queueDownload(results.get(0).getLink(), event);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String resultString = ">>> :mag: __Search results:__\n";
|
||||||
|
int i = 1;
|
||||||
|
for (Result result : results)
|
||||||
|
{
|
||||||
|
if (result.getScore() <= SEARCH_SCORE_THRESHOLD_DISPLAY && i > 5)
|
||||||
|
break;
|
||||||
|
if (i > 10)
|
||||||
|
break;
|
||||||
|
resultString += "**" + i + ":** "
|
||||||
|
+ "[" + result.getSourceAbbr() + "] "
|
||||||
|
+ "*" + result.getTitle() + "* "
|
||||||
|
+ "by " + (result.getArtist() != null ? result.getArtist().trim() : "unknown") + " "
|
||||||
|
// + (result.getAlbum() != null ? "from the album *" + result.getAlbum().trim() + "*" : "")
|
||||||
|
+ "\n";
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
resultString += "Type eg. `!play 1` to select";
|
||||||
|
event.getChannel().sendMessage(resultString).queue();
|
||||||
|
});
|
||||||
|
// event.getChannel().sendMessage("That's not a valid URL you idiot! " + ex.getMessage()).queue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void queueDownload(final URL url, GuildMessageReceivedEvent event)
|
||||||
|
{
|
||||||
|
Song song = new Song(url);
|
||||||
|
song.setRequestedBy(event.getAuthor().getName());
|
||||||
|
song.setRequestedIn(event.getChannel().getId());
|
||||||
|
song.setNumber(trackNumber);
|
||||||
|
trackNumber++;
|
||||||
|
downloader.accept(song);
|
||||||
|
}
|
||||||
|
|
||||||
private void onRestartCommand(GuildMessageReceivedEvent event)
|
private void onRestartCommand(GuildMessageReceivedEvent event)
|
||||||
{
|
{
|
||||||
// TODO: this needs to clear the current data queue
|
// TODO: this needs to clear the current data queue
|
||||||
|
@ -229,13 +313,40 @@ public class Main extends ListenerAdapter
|
||||||
|
|
||||||
private void onQueueCommand(GuildMessageReceivedEvent event, Guild guild)
|
private void onQueueCommand(GuildMessageReceivedEvent event, Guild guild)
|
||||||
{
|
{
|
||||||
Queue<Song> songQueue = musicHandler.getSongQueue();
|
String message = ">>> ";
|
||||||
String message = new String();
|
|
||||||
int i = 1;
|
int i = 1;
|
||||||
message += "Now playing: " + musicHandler.getCurrentSong() + "\n";
|
|
||||||
message += "---\n";
|
final Song currentSong = musicHandler.getCurrentSong();
|
||||||
for (Song song : songQueue)
|
if (currentSong != null)
|
||||||
message += (i) + ": " + song + "\n";
|
message += ":notes: **Now playing: " + currentSong + "**\n";
|
||||||
|
else
|
||||||
|
message += ":mute: **Not playing anything right now.**\n";
|
||||||
|
|
||||||
|
final Queue<Song> songQueue = musicHandler.getSongQueue();
|
||||||
|
if (!songQueue.isEmpty())
|
||||||
|
{
|
||||||
|
message += "__Ready to play:__\n";
|
||||||
|
for (Song song : songQueue)
|
||||||
|
{
|
||||||
|
message += ":bread: **" + (i) + ":** " + song + "\n";
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<Song> downloadQueue = downloader.getDownloadQueue();
|
||||||
|
if (!downloadQueue.isEmpty())
|
||||||
|
{
|
||||||
|
message += "__Downloading:__\n";
|
||||||
|
for (Song song : downloadQueue)
|
||||||
|
{
|
||||||
|
message += ":inbox_tray: **" + (i) + ":** " + song + "\n";
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (downloadQueue.isEmpty() && songQueue.isEmpty())
|
||||||
|
message += ":mailbox_with_no_mail: The track queue is empty.";
|
||||||
|
// :inbox_tray:
|
||||||
event.getChannel().sendMessage(message).queue();
|
event.getChannel().sendMessage(message).queue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
package moe.nekojimi.chords;
|
package moe.nekojimi.chords;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,17 +18,14 @@ public class Song
|
||||||
private String artist;
|
private String artist;
|
||||||
private final URL url;
|
private final URL url;
|
||||||
private File location = null;
|
private File location = null;
|
||||||
|
private int number;
|
||||||
|
|
||||||
private String requestedBy;
|
private String requestedBy;
|
||||||
private String requestedIn;
|
private String requestedIn;
|
||||||
|
|
||||||
public Song(URL url) throws MalformedURLException
|
public Song(URL url)
|
||||||
{
|
{
|
||||||
if (url.toString().equalsIgnoreCase("https://soundcloud.com/user-185855194/fart-with-extra-reverb")
|
this.url = url;
|
||||||
|| url.toString().equalsIgnoreCase("https://www.youtube.com/watch?v=hr7GyFM7pX4"))
|
|
||||||
this.url = new URL("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
|
|
||||||
else
|
|
||||||
this.url = url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTitle()
|
public String getTitle()
|
||||||
|
@ -42,6 +38,16 @@ public class Song
|
||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getNumber()
|
||||||
|
{
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumber(int number)
|
||||||
|
{
|
||||||
|
this.number = number;
|
||||||
|
}
|
||||||
|
|
||||||
public String getArtist()
|
public String getArtist()
|
||||||
{
|
{
|
||||||
return artist;
|
return artist;
|
||||||
|
@ -100,6 +106,7 @@ public class Song
|
||||||
if (title != null && !title.isEmpty())
|
if (title != null && !title.isEmpty())
|
||||||
return title;
|
return title;
|
||||||
else
|
else
|
||||||
return url.toExternalForm();
|
return "track " + number;
|
||||||
|
// return url.toExternalForm();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue