Compare commits

..

No commits in common. "66cdca8f0d9afd1e93e735e26950ffd3bee3e466" and "579fb01a203360c9fdbb6ef1821621f6b93d5917" have entirely different histories.

3 changed files with 38 additions and 171 deletions

View File

@ -8,9 +8,6 @@ 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;
@ -27,9 +24,9 @@ import java.util.regex.Pattern;
*/ */
public class Downloader implements Consumer<Song> public class Downloader implements Consumer<Song>
{ {
private final List<Song> downloadQueue = new LinkedList<>(); // private final Queue<Song> downloadQueue = new LinkedBlockingDeque<>();
private final LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>(); private final LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>();
private final ThreadPoolExecutor exec = new ThreadPoolExecutor(2, 4, 30, TimeUnit.SECONDS, workQueue); private final ThreadPoolExecutor exec = new ThreadPoolExecutor(1, 4, 30, TimeUnit.SECONDS, workQueue);
private Consumer<Song> next; private Consumer<Song> next;
private BiConsumer<Song, Exception> messageHandler; private BiConsumer<Song, Exception> messageHandler;
@ -41,14 +38,9 @@ public class Downloader implements Consumer<Song>
@Override @Override
public void accept(Song song) public void accept(Song song)
{ {
downloadQueue.add(song); exec.submit(() ->
exec.submit(new Runnable()
{ {
@Override download(song);
public void run()
{
download(song);
}
}); });
} }
@ -84,14 +76,12 @@ 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);
} }
} }
@ -105,9 +95,4 @@ public class Downloader implements Consumer<Song>
this.messageHandler = messageHandler; this.messageHandler = messageHandler;
} }
public List<Song> getDownloadQueue()
{
return downloadQueue;
}
} }

View File

@ -5,17 +5,10 @@
*/ */
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.*;
@ -33,18 +26,10 @@ 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 final Downloader downloader; private 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
*/ */
@ -83,18 +68,16 @@ 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(bracketNo + "Finished downloading " + song + " for " + song.getRequestedBy() + ", added to queue!").queue(); channel.sendMessage("Finished downloading track for " + song.getRequestedBy() + ", added to queue!").queue();
else else
channel.sendMessage(bracketNo + "Now downloading " + song + " for " + song.getRequestedBy() + " ...").queue(); channel.sendMessage("Now downloading track for " + song.getRequestedBy() + " ...").queue();
else else
channel.sendMessage(bracketNo + "Failed to download " + song + " for " + song.getRequestedBy() + "! Reason: " + ex.getMessage()).queue(); channel.sendMessage("Failed to download track for " + song.getRequestedBy() + "! Reason: " + ex.getMessage()).queue();
}); });
searcher = MetaSearcher.loadYAML(new File("searchproviders.yml"));
} }
public void setJda(JDA jda) public void setJda(JDA jda)
@ -165,7 +148,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.getName()); onJoinCommand(event, member.getGuild(), channel.getId());
} }
/** /**
@ -205,93 +188,26 @@ public class Main extends ListenerAdapter
{ {
try try
{ {
final URL url = new URL(arg); Song song = new Song(new URL(arg));
queueDownload(url, event); song.setRequestedBy(event.getAuthor().getName());
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 mux) } catch (MalformedURLException ex)
{ {
// not a URL, try parsing it as a search result event.getChannel().sendMessage("That's not a valid URL you idiot! " + ex.getMessage()).queue();
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
@ -313,40 +229,13 @@ public class Main extends ListenerAdapter
private void onQueueCommand(GuildMessageReceivedEvent event, Guild guild) private void onQueueCommand(GuildMessageReceivedEvent event, Guild guild)
{ {
String message = ">>> "; Queue<Song> songQueue = musicHandler.getSongQueue();
String message = new String();
int i = 1; int i = 1;
message += "Now playing: " + musicHandler.getCurrentSong() + "\n";
final Song currentSong = musicHandler.getCurrentSong(); message += "---\n";
if (currentSong != null) for (Song song : songQueue)
message += ":notes: **Now playing: " + currentSong + "**\n"; message += (i) + ": " + song + "\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();
} }

View File

@ -6,6 +6,7 @@
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;
/** /**
@ -18,14 +19,17 @@ 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) public Song(URL url) throws MalformedURLException
{ {
this.url = url; if (url.toString().equalsIgnoreCase("https://soundcloud.com/user-185855194/fart-with-extra-reverb")
|| 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()
@ -38,16 +42,6 @@ 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;
@ -106,7 +100,6 @@ public class Song
if (title != null && !title.isEmpty()) if (title != null && !title.isEmpty())
return title; return title;
else else
return "track " + number; return url.toExternalForm();
// return url.toExternalForm();
} }
} }