Compare commits
5 Commits
1dba55620f
...
3ff544d699
Author | SHA1 | Date |
---|---|---|
|
3ff544d699 | |
|
9fa5e36cdc | |
|
d50657ef90 | |
|
f1cc31dcd9 | |
|
0914cadaf5 |
|
@ -92,20 +92,6 @@ public final class Chords extends ListenerAdapter
|
|||
|
||||
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)
|
||||
{
|
||||
System.out.println("Using local address: " + settings.getLocalAddr());
|
||||
|
@ -124,6 +110,20 @@ public final class Chords extends ListenerAdapter
|
|||
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();
|
||||
listener.setJda(jda);
|
||||
}
|
||||
|
@ -269,8 +269,8 @@ public final class Chords extends ListenerAdapter
|
|||
song.setNumber(trackNumber);
|
||||
trackNumber++;
|
||||
request.setSong(song);
|
||||
downloader.accept(new Downloader.DownloadTask(request, queueManager));
|
||||
request.getInvocation().respond("Request pending...");
|
||||
downloader.accept(new Downloader.DownloadTask(request, queueManager));
|
||||
return song;
|
||||
}
|
||||
|
||||
|
@ -421,6 +421,9 @@ public final class Chords extends ListenerAdapter
|
|||
private class DownloaderMessageHandler implements BiConsumer<SongRequest, Exception>
|
||||
{
|
||||
|
||||
private static final String PROGRESS_SYMBOLS = " ▏▎▍▌▋▊▉█";
|
||||
// private static final String PROGRESS_SYMBOLS = " ⠀⡀⣀⣠⣤⣦⣶⣾⣿";
|
||||
|
||||
public DownloaderMessageHandler()
|
||||
{
|
||||
}
|
||||
|
@ -432,7 +435,7 @@ public final class Chords extends ListenerAdapter
|
|||
// TextChannel channel = jda.getTextChannelById(song.getRequestedIn());
|
||||
// String bracketNo = "[" + song.getNumber() + "] ";
|
||||
if (ex == null)
|
||||
if (song.getLocation() != null)
|
||||
if (song.getLocation() != null && request.getProgress() >= 100)
|
||||
{
|
||||
request.getInvocation().respond("Finished downloading " + song + ", added to queue!");
|
||||
log("DOWN", "Downloaded " + song);
|
||||
|
@ -448,7 +451,12 @@ public final class Chords extends ListenerAdapter
|
|||
String bitFmt = (bitrate <= 0 ? "??" : bitrate) + "k";
|
||||
formatDetails = " (" + bitFmt + ", " + sizeFmt + ")";
|
||||
}
|
||||
request.getInvocation().respond("Now downloading " + song + formatDetails + " ...");
|
||||
String progressDetails = "";
|
||||
if (request.getProgress() >= 0)
|
||||
{
|
||||
progressDetails = " [" + String.format("%.1f", request.getProgress()) + "%]";
|
||||
}
|
||||
request.getInvocation().respond("Now downloading " + song + formatDetails + progressDetails + " ...");
|
||||
log("DOWN", "Downloading " + song + "...");
|
||||
}
|
||||
else
|
||||
|
|
|
@ -13,6 +13,7 @@ import java.nio.file.Files;
|
|||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -38,12 +39,14 @@ public class Downloader implements Consumer<DownloadTask>
|
|||
{
|
||||
|
||||
private static final int DOWNLOAD_TIMEOUT = 300;
|
||||
private static final int INFO_TIMEOUT = 10;
|
||||
private static final int INFO_TIMEOUT = 30;
|
||||
private static final int FORMAT_TIMEOUT = 5;
|
||||
|
||||
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+(.*)$");
|
||||
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 LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>();
|
||||
|
@ -140,18 +143,58 @@ public class Downloader implements Consumer<DownloadTask>
|
|||
|
||||
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());
|
||||
int i = 0;
|
||||
while (!list.get(i).contains("Available formats"))
|
||||
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++)
|
||||
{
|
||||
String line = list.get(i);
|
||||
String[] split = line.split("\\s\\s+", 4);
|
||||
String[] split = line.split("\\s+", Math.max(4, noteCol - 1));
|
||||
if (split.length < 4)
|
||||
continue;
|
||||
formats.add(new Format(split[0], split[1], split[2], split[3]));
|
||||
final Format format = new Format(split[codeCol], split[extCol], split[resCol], split[noteCol]);
|
||||
if (sizeCol >= 0)
|
||||
format.setSize(Util.parseSize(split[sizeCol]));
|
||||
if (bitrateCol >= 0)
|
||||
format.setSampleRate(Util.parseSampleRate(split[bitrateCol]));
|
||||
formats.add(format);
|
||||
}
|
||||
song.setFormats(formats);
|
||||
|
||||
|
@ -218,19 +261,39 @@ public class Downloader implements Consumer<DownloadTask>
|
|||
+ " -f " + formatCodes + "worstaudio/bestaudio/worst/best"
|
||||
+ " --audio-format=wav"
|
||||
+ " --no-playlist"
|
||||
+ " --extractor-args youtube:player_client=android"
|
||||
+ " -o " + getDownloadDir().getAbsolutePath() + "/%(title)s.%(ext)s "
|
||||
+ song.getUrl().toString();
|
||||
|
||||
Process exec = runCommand(cmd, DOWNLOAD_TIMEOUT);
|
||||
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());
|
||||
System.out.println(output);
|
||||
Matcher matcher = DESTINATION_PATTERN.matcher(output);
|
||||
if (matcher.find())
|
||||
song.setLocation(new File(matcher.group(1)));
|
||||
else if (exec.exitValue() != 0)
|
||||
// System.out.println(output);
|
||||
|
||||
if (exec.exitValue() != 0)
|
||||
throw new RuntimeException("youtube-dl failed with error " + exec.exitValue() + ", output:\n" + error);
|
||||
|
||||
task.request.setProgress(100);
|
||||
// return true;
|
||||
|
||||
if (task.getDestination() != null)
|
||||
|
|
|
@ -18,8 +18,6 @@ import javax.json.JsonObject;
|
|||
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 extension;
|
||||
|
@ -97,29 +95,9 @@ class Format
|
|||
return size;
|
||||
|
||||
// try to find eg. "1.32MiB" inside 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;
|
||||
long size = Util.parseSize(note);
|
||||
|
||||
}
|
||||
return -1;
|
||||
return size;
|
||||
}
|
||||
|
||||
public int getSampleRate()
|
||||
|
@ -128,12 +106,7 @@ class Format
|
|||
return samplerate;
|
||||
|
||||
// try to find eg. "51k" inside note
|
||||
Matcher matcher = SAMPLE_RATE_PATTERN.matcher(note);
|
||||
if (matcher.find())
|
||||
{
|
||||
return Integer.parseInt(matcher.group(1)) * 1000;
|
||||
}
|
||||
return -1;
|
||||
return Util.parseSampleRate(note);
|
||||
}
|
||||
|
||||
public void setSampleRate(int samplerate)
|
||||
|
|
|
@ -38,6 +38,8 @@ public class SongRequest
|
|||
|
||||
private Song song;
|
||||
|
||||
private double progress = -1;
|
||||
private double eta = -1;
|
||||
|
||||
|
||||
public List<Result> getSearchResults()
|
||||
|
@ -120,4 +122,24 @@ public class SongRequest
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package moe.nekojimi.chords;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2022 jimj316
|
||||
|
@ -25,6 +27,10 @@ import java.nio.ByteBuffer;
|
|||
*/
|
||||
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)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
@ -34,4 +40,41 @@ public class Util
|
|||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue