Compare commits

..

No commits in common. "3ff544d699d898c650912f76e5e80b6cf07a1b21" and "1dba55620f380666307c9a3bec6cad2aae99d561" have entirely different histories.

5 changed files with 56 additions and 165 deletions

View File

@ -92,6 +92,20 @@ public final class Chords extends ListenerAdapter
JDABuilder builder = JDABuilder.createDefault(settings.getDiscordToken(), intents); JDABuilder builder = JDABuilder.createDefault(settings.getDiscordToken(), intents);
// Disable parts of the cache
builder.disableCache(CacheFlag.MEMBER_OVERRIDES);
// Enable the bulk delete event
builder.setBulkDeleteSplittingEnabled(false);
// Disable compression (not recommended)
builder.setCompression(Compression.NONE);
// Set activity (like "playing Something")
builder.setActivity(Activity.playing("music!"));
final Chords listener = new Chords();
builder.addEventListeners(listener);
builder.setAutoReconnect(true);
if (settings.getLocalAddr() != null) if (settings.getLocalAddr() != null)
{ {
System.out.println("Using local address: " + settings.getLocalAddr()); System.out.println("Using local address: " + settings.getLocalAddr());
@ -110,20 +124,6 @@ public final class Chords extends ListenerAdapter
builder.setWebsocketFactory(webSocketFactory); builder.setWebsocketFactory(webSocketFactory);
} }
// Disable parts of the cache
builder.disableCache(CacheFlag.MEMBER_OVERRIDES);
// Enable the bulk delete event
builder.setBulkDeleteSplittingEnabled(false);
// Disable compression (not recommended)
builder.setCompression(Compression.NONE);
// Set activity (like "playing Something")
builder.setActivity(Activity.playing("music!"));
final Chords listener = new Chords();
builder.addEventListeners(listener);
builder.setAutoReconnect(true);
JDA jda = builder.build(); JDA jda = builder.build();
listener.setJda(jda); listener.setJda(jda);
} }
@ -269,8 +269,8 @@ public final class Chords extends ListenerAdapter
song.setNumber(trackNumber); song.setNumber(trackNumber);
trackNumber++; trackNumber++;
request.setSong(song); request.setSong(song);
request.getInvocation().respond("Request pending...");
downloader.accept(new Downloader.DownloadTask(request, queueManager)); downloader.accept(new Downloader.DownloadTask(request, queueManager));
request.getInvocation().respond("Request pending...");
return song; return song;
} }
@ -421,9 +421,6 @@ public final class Chords extends ListenerAdapter
private class DownloaderMessageHandler implements BiConsumer<SongRequest, Exception> private class DownloaderMessageHandler implements BiConsumer<SongRequest, Exception>
{ {
private static final String PROGRESS_SYMBOLS = " ▏▎▍▌▋▊▉█";
// private static final String PROGRESS_SYMBOLS = " ⠀⡀⣀⣠⣤⣦⣶⣾⣿";
public DownloaderMessageHandler() public DownloaderMessageHandler()
{ {
} }
@ -435,7 +432,7 @@ public final class Chords extends ListenerAdapter
// TextChannel channel = jda.getTextChannelById(song.getRequestedIn()); // TextChannel channel = jda.getTextChannelById(song.getRequestedIn());
// String bracketNo = "[" + song.getNumber() + "] "; // String bracketNo = "[" + song.getNumber() + "] ";
if (ex == null) if (ex == null)
if (song.getLocation() != null && request.getProgress() >= 100) if (song.getLocation() != null)
{ {
request.getInvocation().respond("Finished downloading " + song + ", added to queue!"); request.getInvocation().respond("Finished downloading " + song + ", added to queue!");
log("DOWN", "Downloaded " + song); log("DOWN", "Downloaded " + song);
@ -451,12 +448,7 @@ public final class Chords extends ListenerAdapter
String bitFmt = (bitrate <= 0 ? "??" : bitrate) + "k"; String bitFmt = (bitrate <= 0 ? "??" : bitrate) + "k";
formatDetails = " (" + bitFmt + ", " + sizeFmt + ")"; formatDetails = " (" + bitFmt + ", " + sizeFmt + ")";
} }
String progressDetails = ""; request.getInvocation().respond("Now downloading " + song + formatDetails + " ...");
if (request.getProgress() >= 0)
{
progressDetails = " [" + String.format("%.1f", request.getProgress()) + "%]";
}
request.getInvocation().respond("Now downloading " + song + formatDetails + progressDetails + " ...");
log("DOWN", "Downloading " + song + "..."); log("DOWN", "Downloading " + song + "...");
} }
else else

View File

@ -13,7 +13,6 @@ import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Scanner;
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;
@ -39,14 +38,12 @@ public class Downloader implements Consumer<DownloadTask>
{ {
private static final int DOWNLOAD_TIMEOUT = 300; private static final int DOWNLOAD_TIMEOUT = 300;
private static final int INFO_TIMEOUT = 30; private static final int INFO_TIMEOUT = 10;
private static final int FORMAT_TIMEOUT = 5; private static final int FORMAT_TIMEOUT = 5;
private static final int BITRATE_TARGET = (int) AudioSendHandler.INPUT_FORMAT.getSampleRate(); private static final int BITRATE_TARGET = (int) AudioSendHandler.INPUT_FORMAT.getSampleRate();
private static final Pattern FORMAT_PATTERN = Pattern.compile("^([\\w]+)\\s+([\\w]+)\\s+(\\w+ ?\\w*)\\s+(.*)$"); private static final Pattern FORMAT_PATTERN = Pattern.compile("^([\\w]+)\\s+([\\w]+)\\s+(\\w+ ?\\w*)\\s+(.*)$");
public static final Pattern DESTINATION_PATTERN = Pattern.compile("Destination: (.*\\.wav)"); public static final Pattern DESTINATION_PATTERN = Pattern.compile("Destination: (.*\\.wav)");
public static final Pattern PROGRESS_PATTERN = Pattern.compile("\\[download\\].*?([\\d\\.]+)%");
private static final Pattern ETA_PATTERN = Pattern.compile("\\[download\\].*?ETA\\s+(\\d{1,2}:\\d{2})");
private final List<DownloadTask> downloadQueue = new LinkedList<>(); private final List<DownloadTask> downloadQueue = new LinkedList<>();
private final LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>(); private final LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>();
@ -143,58 +140,18 @@ public class Downloader implements Consumer<DownloadTask>
List<Format> formats = new ArrayList<>(); List<Format> formats = new ArrayList<>();
int codeCol = 0;
int extCol = 1;
int resCol = 2;
int noteCol = 3;
int sizeCol = -1;
int bitrateCol = -1;
List<String> list = output.lines().collect(Collectors.toList()); List<String> list = output.lines().collect(Collectors.toList());
int i = 0; int i = 0;
while (!list.get(i).contains("Available formats")) while (!list.get(i).contains("Available formats"))
i++; i++;
i++; i++;
if (list.get(i).contains("FILESIZE"))
{
String[] split = list.get(i).split("\\s+");
for (int j = 0; j < split.length; j++)
{
switch (split[j])
{
case "ID":
codeCol = j;
break;
case "EXT":
extCol = j;
break;
case "RESOLUTION":
resCol = j;
break;
case "MORE":
noteCol = j;
break;
case "FILESIZE":
sizeCol = j;
break;
case "TBR":
bitrateCol = j;
}
}
i += 2;
}
for (; i < list.size(); i++) for (; i < list.size(); i++)
{ {
String line = list.get(i); String line = list.get(i);
String[] split = line.split("\\s+", Math.max(4, noteCol - 1)); String[] split = line.split("\\s\\s+", 4);
if (split.length < 4) if (split.length < 4)
continue; continue;
final Format format = new Format(split[codeCol], split[extCol], split[resCol], split[noteCol]); formats.add(new Format(split[0], split[1], split[2], split[3]));
if (sizeCol >= 0)
format.setSize(Util.parseSize(split[sizeCol]));
if (bitrateCol >= 0)
format.setSampleRate(Util.parseSampleRate(split[bitrateCol]));
formats.add(format);
} }
song.setFormats(formats); song.setFormats(formats);
@ -261,39 +218,19 @@ public class Downloader implements Consumer<DownloadTask>
+ " -f " + formatCodes + "worstaudio/bestaudio/worst/best" + " -f " + formatCodes + "worstaudio/bestaudio/worst/best"
+ " --audio-format=wav" + " --audio-format=wav"
+ " --no-playlist" + " --no-playlist"
+ " --extractor-args youtube:player_client=android"
+ " -o " + getDownloadDir().getAbsolutePath() + "/%(title)s.%(ext)s " + " -o " + getDownloadDir().getAbsolutePath() + "/%(title)s.%(ext)s "
+ song.getUrl().toString(); + song.getUrl().toString();
Process exec = runCommand(cmd, DOWNLOAD_TIMEOUT); Process exec = runCommand(cmd, DOWNLOAD_TIMEOUT);
InputStream in = exec.getInputStream(); InputStream in = exec.getInputStream();
String output = new String(in.readAllBytes(), Charset.defaultCharset());
Scanner sc = new Scanner(in);
while (sc.hasNextLine())
{
String line = sc.nextLine();
System.out.println(line);
Matcher progMatcher = PROGRESS_PATTERN.matcher(line);
if (progMatcher.find())
{
task.request.setProgress(Double.parseDouble(progMatcher.group(1)));
messageHandler.accept(task.request, null);
}
Matcher destMatcher = DESTINATION_PATTERN.matcher(line);
if (destMatcher.find())
song.setLocation(new File(destMatcher.group(1)));
}
// String output = new String(in.readAllBytes(), Charset.defaultCharset());
String error = new String(exec.getErrorStream().readAllBytes(), Charset.defaultCharset()); String error = new String(exec.getErrorStream().readAllBytes(), Charset.defaultCharset());
// System.out.println(output); System.out.println(output);
Matcher matcher = DESTINATION_PATTERN.matcher(output);
if (exec.exitValue() != 0) 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() + ", output:\n" + error); throw new RuntimeException("youtube-dl failed with error " + exec.exitValue() + ", output:\n" + error);
task.request.setProgress(100);
// return true; // return true;
if (task.getDestination() != null) if (task.getDestination() != null)

View File

@ -18,6 +18,8 @@ import javax.json.JsonObject;
class Format class Format
{ {
private static final Pattern SIZE_PATTERN = Pattern.compile("\\b([0-9]+\\.?[0-9]*)([kkMmGg])i?[bB]\\b");
private static final Pattern SAMPLE_RATE_PATTERN = Pattern.compile("\\b([0-9]+)k(b(ps?))?\\b");
private final String code; private final String code;
private final String extension; private final String extension;
@ -95,9 +97,29 @@ class Format
return size; return size;
// try to find eg. "1.32MiB" inside note // try to find eg. "1.32MiB" inside note
long size = Util.parseSize(note); Matcher matcher = SIZE_PATTERN.matcher(note);
if (matcher.find())
{
double value = Double.parseDouble(matcher.group(1));
String mag = matcher.group(2).toUpperCase();
long mult = 1;
switch (mag)
{
case "K":
mult = 1024;
break;
case "M":
mult = 1024 * 1024;
break;
case "G":
mult = 1024 * 1024 * 1024;
break;
}
value *= mult;
return (long) value;
return size; }
return -1;
} }
public int getSampleRate() public int getSampleRate()
@ -106,7 +128,12 @@ class Format
return samplerate; return samplerate;
// try to find eg. "51k" inside note // try to find eg. "51k" inside note
return Util.parseSampleRate(note); Matcher matcher = SAMPLE_RATE_PATTERN.matcher(note);
if (matcher.find())
{
return Integer.parseInt(matcher.group(1)) * 1000;
}
return -1;
} }
public void setSampleRate(int samplerate) public void setSampleRate(int samplerate)

View File

@ -38,8 +38,6 @@ public class SongRequest
private Song song; private Song song;
private double progress = -1;
private double eta = -1;
public List<Result> getSearchResults() public List<Result> getSearchResults()
@ -122,24 +120,4 @@ public class SongRequest
this.song = song; this.song = song;
} }
public double getProgress()
{
return progress;
}
public void setProgress(double progress)
{
this.progress = progress;
}
public double getEta()
{
return eta;
}
public void setEta(double eta)
{
this.eta = eta;
}
} }

View File

@ -1,8 +1,6 @@
package moe.nekojimi.chords; package moe.nekojimi.chords;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/* /*
* Copyright (C) 2022 jimj316 * Copyright (C) 2022 jimj316
@ -27,10 +25,6 @@ import java.util.regex.Pattern;
*/ */
public class Util public class Util
{ {
private static final Pattern SIZE_PATTERN = Pattern.compile("\\b([0-9]+\\.?[0-9]*)([kkMmGg])i?[bB]\\b");
private static final Pattern SAMPLE_RATE_PATTERN = Pattern.compile("\\b([0-9]+)k(b(ps?))?\\b");
public static String printSamples(ByteBuffer buf) public static String printSamples(ByteBuffer buf)
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -40,41 +34,4 @@ public class Util
} }
return sb.toString(); return sb.toString();
} }
public static int parseSampleRate(String input)
{
Matcher matcher = SAMPLE_RATE_PATTERN.matcher(input);
if (matcher.find())
{
return Integer.parseInt(matcher.group(1)) * 1000;
}
return -1;
}
public static long parseSize(String note)
{
Matcher matcher = SIZE_PATTERN.matcher(note);
if (matcher.find())
{
double value = Double.parseDouble(matcher.group(1));
String mag = matcher.group(2).toUpperCase();
long mult = 1;
switch (mag)
{
case "K":
mult = 1024;
break;
case "M":
mult = 1024 * 1024;
break;
case "G":
mult = 1024 * 1024 * 1024;
break;
}
value *= mult;
return (long) value;
}
return -1L;
}
} }