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 {
private static final Logger LOG = LoggerFactory.getLogger(OnlineMonitor.class);
private static final boolean IGNORE_CACHE = true;
private static final long TIMEOUT_IN_MILLIS = 2000;
private volatile boolean running = false;
private final Recorder recorder;
@ -81,15 +80,16 @@ public class OnlineMonitor extends Thread {
}
// wait for all jobs to finish
for (ModelAwareFuture future : futures) {
Duration timeout = future.getModel().getSite().getOnlineCheckTimeout();
try {
future.get(TIMEOUT_IN_MILLIS, TimeUnit.MILLISECONDS);
future.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
LOG.debug("Online check interrupted for model {}", future.getModel(), e);
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
LOG.info("Error while checking online state for model {}", future.getModel(), 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;
import java.io.IOException;
import java.util.List;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.io.HttpClient;
import ctbrec.recorder.Recorder;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
public interface Site {
public String getTitle();
public String getName();
public String getBaseUrl();
public String getAffiliateLink();
public void setRecorder(Recorder recorder);
public Recorder getRecorder();
public Model createModel(String name);
public Double getTokenBalance() throws IOException;
public String getBuyTokensLink();
public boolean login() throws IOException;
public HttpClient getHttpClient();
public void init() throws IOException;
public void shutdown();
public boolean supportsTips();
public boolean supportsFollow();
public boolean supportsSearch();
public boolean isSiteForModel(Model m);
public boolean credentialsAvailable();
public void setEnabled(boolean enabled);
public boolean isEnabled();
public List<Model> search(String q) throws IOException, InterruptedException;
public boolean searchRequiresLogin();
public Model createModelFromUrl(String url);
public void setConfig(Config config);
String getTitle();
String getName();
String getBaseUrl();
String getAffiliateLink();
void setRecorder(Recorder recorder);
Recorder getRecorder();
Model createModel(String name);
Double getTokenBalance() throws IOException;
String getBuyTokensLink();
boolean login() throws IOException;
HttpClient getHttpClient();
void init() throws IOException;
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;
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.StringUtil;
import ctbrec.io.HtmlParser;
@ -20,6 +7,20 @@ import ctbrec.io.HttpClient;
import ctbrec.sites.AbstractSite;
import okhttp3.Request;
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 {
@ -69,14 +70,15 @@ public class Chaturbate extends AbstractSite {
.url(url)
.header(USER_AGENT, getConfig().getSettings().httpUserAgent)
.build();
Response resp = getHttpClient().execute(req);
if (resp.isSuccessful()) {
String profilePage = resp.body().string();
String tokenText = HtmlParser.getText(profilePage, "span.tokencount");
int tokens = Integer.parseInt(tokenText);
return (double) tokens;
} else {
throw new IOException("HTTP response: " + resp.code() + " - " + resp.message());
try (Response resp = getHttpClient().execute(req)) {
if (resp.isSuccessful()) {
String profilePage = resp.body().string();
String tokenText = HtmlParser.getText(profilePage, "span.tokencount");
int tokens = Integer.parseInt(tokenText);
return (double) tokens;
} else {
throw new IOException("HTTP response: " + resp.code() + " - " + resp.message());
}
}
}
@ -122,7 +124,7 @@ public class Chaturbate extends AbstractSite {
@Override
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<>();
// search online models
@ -170,11 +172,17 @@ public class Chaturbate extends AbstractSite {
@Override
public Model createModelFromUrl(String url) {
Matcher m = Pattern.compile("https?://.*?chaturbate.com(?:/p)?/([^/]*?)/?").matcher(url);
if(m.matches()) {
if (m.matches()) {
String modelName = m.group(1);
return createModel(modelName);
} else {
return super.createModelFromUrl(url);
}
}
@Override
public Duration getOnlineCheckTimeout() {
int msBetweenRequests = getConfig().getSettings().chaturbateMsBetweenRequests;
return Duration.ofMillis(msBetweenRequests + 2000L);
}
}