Piped-Backend/src/main/java/me/kavin/piped/utils/DownloaderImpl.java

55 lines
1.9 KiB
Java
Raw Normal View History

2020-11-12 21:19:45 +00:00
package me.kavin.piped.utils;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.Builder;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.downloader.Request;
import org.schabi.newpipe.extractor.downloader.Response;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import me.kavin.piped.consts.Constants;
public class DownloaderImpl extends Downloader {
/**
* Executes a request with HTTP/2.
*/
@Override
public Response execute(Request request) throws IOException, ReCaptchaException {
2021-02-24 09:52:29 +00:00
// TODO: HTTP/3 aka QUIC
Builder builder = HttpRequest.newBuilder(URI.create(request.url()));
2020-11-12 21:19:45 +00:00
2021-02-24 09:52:29 +00:00
byte[] data = request.dataToSend();
BodyPublisher publisher = data == null ? HttpRequest.BodyPublishers.noBody()
: HttpRequest.BodyPublishers.ofByteArray(data);
2020-11-12 21:19:45 +00:00
2021-02-24 09:52:29 +00:00
builder.method(request.httpMethod(), publisher);
request.headers().forEach((name, values) -> values.forEach(value -> builder.header(name, value)));
2020-11-12 21:19:45 +00:00
2021-02-24 09:52:29 +00:00
builder.setHeader("User-Agent", Constants.USER_AGENT);
2020-11-12 21:19:45 +00:00
2021-02-24 09:52:29 +00:00
HttpResponse<String> response = null;
2020-11-12 21:19:45 +00:00
2021-02-24 09:52:29 +00:00
try {
response = Constants.h2client.send(builder.build(), BodyHandlers.ofString());
} catch (InterruptedException e) {
// ignored
}
2020-11-12 21:19:45 +00:00
2021-02-24 09:52:29 +00:00
// TODO: Implement solver
if (response.statusCode() == 429) {
throw new ReCaptchaException("reCaptcha Challenge requested", String.valueOf(response.uri()));
}
2020-11-12 21:19:45 +00:00
2021-02-24 09:52:29 +00:00
return new Response(response.statusCode(), "UNDEFINED", response.headers().map(), response.body(),
String.valueOf(response.uri()));
2020-11-12 21:19:45 +00:00
}
}