Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import org.schabi.newpipe.extractor.downloader.Request;
import org.schabi.newpipe.extractor.downloader.Response;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -30,22 +29,20 @@ class MockDownloader extends Downloader {
public MockDownloader(@Nonnull final String path) {
this.path = path;
this.mocks = new HashMap<>();
final File[] files = new File(path).listFiles();
if (files != null) {
for (final File file : files) {
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
final TestRequestResponse response;
try(final InputStreamReader reader = new InputStreamReader(
new FileInputStream(file), StandardCharsets.UTF_8)) {
response = new GsonBuilder()
.create()
.fromJson(reader, TestRequestResponse.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}

try (final var directoryStream = Files.newDirectoryStream(Paths.get(path),
entry -> entry.getFileName().toString()
.startsWith(RecordingDownloader.FILE_NAME_PREFIX))) {
for (final Path entry : directoryStream) {
try (final var reader = Files.newBufferedReader(entry)) {
final var response = new GsonBuilder()
.create()
.fromJson(reader, TestRequestResponse.class);
mocks.put(response.getRequest(), response.getResponse());
}
}
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
import org.schabi.newpipe.extractor.downloader.Response;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -45,27 +41,30 @@ class RecordingDownloader extends Downloader {
"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";

private int index = 0;
private final String path;
private final Path path;

/**
* Creates the folder described by {@code stringPath} if it does not exist.
* Deletes existing files starting with {@link RecordingDownloader#FILE_NAME_PREFIX}.
* @param stringPath Path to the folder where the json files will be saved to.
*/
public RecordingDownloader(final String stringPath) {
this.path = stringPath;
final Path path = Paths.get(stringPath);
final File folder = path.toFile();
if (folder.exists()) {
for (final File file : folder.listFiles()) {
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
file.delete();
this.path = Paths.get(stringPath);

if (Files.exists(path)) {
try (final var directoryStream = Files.newDirectoryStream(path,
entry -> entry.getFileName().toString()
.startsWith(RecordingDownloader.FILE_NAME_PREFIX))) {
for (final Path entry : directoryStream) {
Files.delete(entry);
}
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
} else {
try {
Files.createDirectories(path);
} catch (IOException e) {
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}
Expand All @@ -84,20 +83,13 @@ public Response execute(@Nonnull final Request request) throws IOException,
response.latestUrl()
);

final File outputFile = new File(
path + File.separator + FILE_NAME_PREFIX + index + ".json");
final Path outputPath = path.resolve(FILE_NAME_PREFIX + index + ".json");
index++;
outputFile.createNewFile();

try (final OutputStreamWriter writer = new OutputStreamWriter(
new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {

try (final var writer = Files.newBufferedWriter(outputPath)) {
new GsonBuilder()
.setPrettyPrinting()
.create()
.toJson(new TestRequestResponse(request, response), writer);

writer.flush();
.setPrettyPrinting()
.create()
.toJson(new TestRequestResponse(request, response), writer);
}

return response;
Expand Down