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

125 lines
5.2 KiB
Java
Raw Normal View History

2020-11-12 21:19:45 +00:00
package me.kavin.piped.utils;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.Scheduler;
import com.grack.nanojson.JsonParserException;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.obj.SolvedCaptcha;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
2020-11-12 21:19:45 +00:00
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 java.io.IOException;
import java.net.HttpCookie;
import java.util.concurrent.TimeUnit;
2020-11-12 21:19:45 +00:00
public class DownloaderImpl extends Downloader {
private static HttpCookie saved_cookie;
private static long cookie_received;
private static final Object cookie_lock = new Object();
final LoadingCache<Request, Response> responseCache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
.scheduler(Scheduler.systemScheduler())
.maximumSize(1000).build(this::executeRequest);
2021-11-17 09:54:46 +00:00
@Override
public Response execute(@NotNull Request request) {
2021-11-17 09:54:46 +00:00
return responseCache.get(request);
}
2020-11-12 21:19:45 +00:00
/**
* Executes a request with HTTP/2.
*/
2021-11-17 09:54:46 +00:00
public Response executeRequest(Request request) throws IOException, ReCaptchaException {
2020-11-12 21:19:45 +00:00
2021-02-24 09:52:29 +00:00
// TODO: HTTP/3 aka QUIC
var bytes = request.dataToSend();
RequestBody body = null;
if (bytes != null)
body = RequestBody.create(bytes);
2020-11-12 21:19:45 +00:00
var builder = new okhttp3.Request.Builder()
.url(request.url())
.method(request.httpMethod(), body)
.header("User-Agent", Constants.USER_AGENT);
2020-11-12 21:19:45 +00:00
if (saved_cookie != null && !saved_cookie.hasExpired())
builder.header("Cookie", saved_cookie.getName() + "=" + saved_cookie.getValue());
request.headers().forEach((name, values) -> values.forEach(value -> builder.header(name, value)));
var response = Constants.h2client.newCall(builder.build()).execute();
2020-11-12 21:19:45 +00:00
if (response.code() == 429) {
synchronized (cookie_lock) {
if (saved_cookie != null && saved_cookie.hasExpired()
|| (System.currentTimeMillis() - cookie_received > TimeUnit.MINUTES.toMillis(30)))
saved_cookie = null;
String redir_url = String.valueOf(response.request().url());
if (saved_cookie == null && redir_url.startsWith("https://www.google.com/sorry")) {
var formBuilder = new FormBody.Builder();
String sitekey = null, data_s = null;
for (Element el : Jsoup.parse(response.body().string()).selectFirst("form").children()) {
String name;
if (!(name = el.tagName()).equals("script")) {
if (name.equals("input"))
formBuilder.add(el.attr("name"), el.attr("value"));
else if (name.equals("div") && el.attr("id").equals("recaptcha")) {
sitekey = el.attr("data-sitekey");
data_s = el.attr("data-s");
}
}
}
if (StringUtils.isEmpty(sitekey) || StringUtils.isEmpty(data_s))
throw new ReCaptchaException("Could not get recaptcha", redir_url);
SolvedCaptcha solved = null;
try {
solved = CaptchaSolver.solve(redir_url, sitekey, data_s);
} catch (JsonParserException | InterruptedException e) {
e.printStackTrace();
}
formBuilder.add("g-recaptcha-response", solved.getRecaptchaResponse());
var formReqBuilder = new okhttp3.Request.Builder()
.url("https://www.google.com/sorry/index")
.header("User-Agent", Constants.USER_AGENT)
.post(formBuilder.build());
var formResponse = Constants.h2_no_redir_client.newCall(formReqBuilder.build()).execute();
saved_cookie = HttpCookie.parse(URLUtils.silentDecode(StringUtils
.substringAfter(formResponse.headers().get("Location"), "google_abuse=")))
.get(0);
cookie_received = System.currentTimeMillis();
}
if (saved_cookie != null) // call again as captcha has been solved or cookie has not expired.
execute(request);
}
2021-02-24 09:52:29 +00:00
}
2020-11-12 21:19:45 +00:00
return new Response(response.code(), response.message(), response.headers().toMultimap(), response.body().string(),
String.valueOf(response.request().url()));
2020-11-12 21:19:45 +00:00
}
}