forked from j62/ctbrec
1
0
Fork 0

Add getOnlineCheckTimeout() to Site interface so that each site can specify its own timeout

This commit is contained in:
0xb00bface 2022-05-28 18:30:12 +02:00
parent 5dc5083150
commit 73598fa590
3 changed files with 89 additions and 53 deletions

View File

@ -24,7 +24,6 @@ import static ctbrec.Model.State.UNKNOWN;
public class OnlineMonitor extends Thread { public class OnlineMonitor extends Thread {
private static final Logger LOG = LoggerFactory.getLogger(OnlineMonitor.class); private static final Logger LOG = LoggerFactory.getLogger(OnlineMonitor.class);
private static final boolean IGNORE_CACHE = true; private static final boolean IGNORE_CACHE = true;
private static final long TIMEOUT_IN_MILLIS = 2000;
private volatile boolean running = false; private volatile boolean running = false;
private final Recorder recorder; private final Recorder recorder;
@ -81,15 +80,16 @@ public class OnlineMonitor extends Thread {
} }
// wait for all jobs to finish // wait for all jobs to finish
for (ModelAwareFuture future : futures) { for (ModelAwareFuture future : futures) {
Duration timeout = future.getModel().getSite().getOnlineCheckTimeout();
try { try {
future.get(TIMEOUT_IN_MILLIS, TimeUnit.MILLISECONDS); future.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOG.debug("Online check interrupted for model {}", future.getModel(), e); LOG.debug("Online check interrupted for model {}", future.getModel(), e);
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (ExecutionException e) { } catch (ExecutionException e) {
LOG.info("Error while checking online state for model {}", future.getModel(), e); LOG.info("Error while checking online state for model {}", future.getModel(), e);
} catch (TimeoutException e) { } catch (TimeoutException e) {
LOG.debug("Online check didn't finish after {}ms for model {}", TIMEOUT_IN_MILLIS, future.getModel()); LOG.debug("Online check didn't finish after {}ms for model {}", timeout.toMillis(), future.getModel());
} }
} }
} }

View File

@ -1,36 +1,64 @@
package ctbrec.sites; package ctbrec.sites;
import java.io.IOException;
import java.util.List;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.Model; import ctbrec.Model;
import ctbrec.io.HttpClient; import ctbrec.io.HttpClient;
import ctbrec.recorder.Recorder; import ctbrec.recorder.Recorder;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
public interface Site { public interface Site {
public String getTitle(); String getTitle();
public String getName();
public String getBaseUrl(); String getName();
public String getAffiliateLink();
public void setRecorder(Recorder recorder); String getBaseUrl();
public Recorder getRecorder();
public Model createModel(String name); String getAffiliateLink();
public Double getTokenBalance() throws IOException;
public String getBuyTokensLink(); void setRecorder(Recorder recorder);
public boolean login() throws IOException;
public HttpClient getHttpClient(); Recorder getRecorder();
public void init() throws IOException;
public void shutdown(); Model createModel(String name);
public boolean supportsTips();
public boolean supportsFollow(); Double getTokenBalance() throws IOException;
public boolean supportsSearch();
public boolean isSiteForModel(Model m); String getBuyTokensLink();
public boolean credentialsAvailable();
public void setEnabled(boolean enabled); boolean login() throws IOException;
public boolean isEnabled();
public List<Model> search(String q) throws IOException, InterruptedException; HttpClient getHttpClient();
public boolean searchRequiresLogin();
public Model createModelFromUrl(String url); void init() throws IOException;
public void setConfig(Config config);
void shutdown();
boolean supportsTips();
boolean supportsFollow();
boolean supportsSearch();
boolean isSiteForModel(Model m);
boolean credentialsAvailable();
void setEnabled(boolean enabled);
boolean isEnabled();
List<Model> search(String q) throws IOException, InterruptedException;
boolean searchRequiresLogin();
Model createModelFromUrl(String url);
void setConfig(Config config);
default Duration getOnlineCheckTimeout() {
return Duration.ofSeconds(2);
}
} }

View File

@ -1,18 +1,5 @@
package ctbrec.sites.chaturbate; package ctbrec.sites.chaturbate;
import static ctbrec.io.HttpConstants.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONObject;
import ctbrec.Model; import ctbrec.Model;
import ctbrec.StringUtil; import ctbrec.StringUtil;
import ctbrec.io.HtmlParser; import ctbrec.io.HtmlParser;
@ -20,6 +7,20 @@ import ctbrec.io.HttpClient;
import ctbrec.sites.AbstractSite; import ctbrec.sites.AbstractSite;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static ctbrec.io.HttpConstants.*;
import static java.nio.charset.StandardCharsets.UTF_8;
public class Chaturbate extends AbstractSite { public class Chaturbate extends AbstractSite {
@ -69,7 +70,7 @@ public class Chaturbate extends AbstractSite {
.url(url) .url(url)
.header(USER_AGENT, getConfig().getSettings().httpUserAgent) .header(USER_AGENT, getConfig().getSettings().httpUserAgent)
.build(); .build();
Response resp = getHttpClient().execute(req); try (Response resp = getHttpClient().execute(req)) {
if (resp.isSuccessful()) { if (resp.isSuccessful()) {
String profilePage = resp.body().string(); String profilePage = resp.body().string();
String tokenText = HtmlParser.getText(profilePage, "span.tokencount"); String tokenText = HtmlParser.getText(profilePage, "span.tokencount");
@ -79,6 +80,7 @@ public class Chaturbate extends AbstractSite {
throw new IOException("HTTP response: " + resp.code() + " - " + resp.message()); throw new IOException("HTTP response: " + resp.code() + " - " + resp.message());
} }
} }
}
@Override @Override
public String getBuyTokensLink() { public String getBuyTokensLink() {
@ -122,7 +124,7 @@ public class Chaturbate extends AbstractSite {
@Override @Override
public List<Model> search(String q) throws IOException, InterruptedException { public List<Model> search(String q) throws IOException, InterruptedException {
String url = baseUrl + "/ax/search/?keywords=" + URLEncoder.encode(q, "utf-8"); String url = baseUrl + "/ax/search/?keywords=" + URLEncoder.encode(q, UTF_8);
List<Model> result = new ArrayList<>(); List<Model> result = new ArrayList<>();
// search online models // search online models
@ -177,4 +179,10 @@ public class Chaturbate extends AbstractSite {
return super.createModelFromUrl(url); return super.createModelFromUrl(url);
} }
} }
@Override
public Duration getOnlineCheckTimeout() {
int msBetweenRequests = getConfig().getSettings().chaturbateMsBetweenRequests;
return Duration.ofMillis(msBetweenRequests + 2000L);
}
} }