Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate code to Java #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,246 changes: 1,246 additions & 0 deletions src/main/java/com/slskbatchdl/Config.java

Large diffs are not rendered by default.

117 changes: 117 additions & 0 deletions src/main/java/com/slskbatchdl/Download.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.slskbatchdl;

import com.slskbatchdl.models.Track;
import com.slskbatchdl.utils.Printing;
import com.slskbatchdl.utils.Utils;
import com.slskbatchdl.utils.ProgressBar;
import com.slskbatchdl.utils.SearchResponse;
import com.slskbatchdl.utils.SoulseekClientException;
import com.slskbatchdl.utils.TransferOptions;
import com.slskbatchdl.utils.TransferState;
import com.slskbatchdl.utils.DownloadWrapper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.CancellationToken;
import java.util.concurrent.CancellationTokenSource;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class Download {

private static final ConcurrentMap<String, DownloadWrapper> downloads = new ConcurrentHashMap<>();

public static void downloadFile(SearchResponse response, com.slskbatchdl.utils.File file, String filePath, Track track, ProgressBar progress, Config config, CancellationToken ct, CancellationTokenSource searchCts) throws Exception {
Program.waitForLogin(config);
Files.createDirectories(Paths.get(filePath).getParent());
String origPath = filePath;
filePath += ".incomplete";

Printing.writeLineIf("Downloading: " + track + " to '" + filePath + "'", config.debugInfo);

TransferOptions transferOptions = new TransferOptions(
state -> {
if (downloads.containsKey(file.getFilename())) {
downloads.get(file.getFilename()).setTransfer(state.getTransfer());
}
},
progressUpdated -> {
if (downloads.containsKey(file.getFilename())) {
downloads.get(file.getFilename()).setBytesTransferred(progressUpdated.getPreviousBytesTransferred());
}
}
);

try (CancellationTokenSource downloadCts = ct != null ? CancellationTokenSource.createLinkedTokenSource(ct) : new CancellationTokenSource();
OutputStream outputStream = new FileOutputStream(filePath)) {

DownloadWrapper wrapper = new DownloadWrapper(origPath, response, file, track, downloadCts, progress);
downloads.put(file.getFilename(), wrapper);

int maxRetries = 3;
int retryCount = 0;
while (true) {
try {
Program.client.downloadAsync(response.getUsername(), file.getFilename(),
() -> outputStream,
file.getSize(), outputStream.getChannel().position(),
transferOptions, downloadCts.getToken());

break;
} catch (SoulseekClientException e) {
retryCount++;

Printing.writeLineIf("Error while downloading: " + e, config.debugInfo, ConsoleColor.DarkYellow);

if (retryCount >= maxRetries || Program.isConnectedAndLoggedIn()) {
throw e;
}

Program.waitForLogin(config);
}
}
} catch (Exception e) {
if (Files.exists(Paths.get(filePath))) {
try {
Files.delete(Paths.get(filePath));
} catch (IOException ignored) {
}
}
downloads.remove(file.getFilename());
DownloadWrapper d = downloads.get(file.getFilename());
if (d != null) {
synchronized (d) {
d.updateText();
}
}
throw e;
}

try {
if (searchCts != null) {
searchCts.cancel();
}
} catch (Exception ignored) {
}

try {
Utils.move(filePath, origPath);
} catch (IOException e) {
Printing.writeLine("Failed to rename .incomplete file", ConsoleColor.DarkYellow, true);
}

downloads.remove(file.getFilename());

DownloadWrapper x = downloads.get(file.getFilename());
if (x != null) {
synchronized (x) {
x.setSuccess(true);
x.updateText();
}
}
}
}
126 changes: 126 additions & 0 deletions src/main/java/com/slskbatchdl/Enums.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.slskbatchdl;

public class Enums {
public enum FailureReason {
None(0),
InvalidSearchString(1),
OutOfDownloadRetries(2),
NoSuitableFileFound(3),
AllDownloadsFailed(4);

private final int value;

FailureReason(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

public enum TrackState {
Initial(0),
Downloaded(1),
Failed(2),
AlreadyExists(3),
NotFoundLastTime(4);

private final int value;

TrackState(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

public enum SkipMode {
Name(0),
Tag(2),
Index(4);

private final int value;

SkipMode(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

public enum InputType {
CSV,
YouTube,
Spotify,
Bandcamp,
String,
List,
None
}

public enum TrackType {
Normal(0),
Album(1),
Aggregate(2),
AlbumAggregate(3);

private final int value;

TrackType(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

public enum M3uOption {
None,
Index,
Playlist,
All
}

public enum PrintOption {
None(0),
Tracks(1),
Results(2),
Full(4);

private final int value;

PrintOption(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

public enum AlbumArtOption {
Default,
Most,
Largest
}

public enum Verbosity {
Silent,
Error,
Warning,
Normal,
Verbose
}

public enum AlbumFailOption {
Ignore,
Keep,
Delete
}
}
Loading