diff --git a/common/src/main/java/ctbrec/sites/streamate/Streamate.java b/common/src/main/java/ctbrec/sites/streamate/Streamate.java index 41bd47fd..739e308a 100644 --- a/common/src/main/java/ctbrec/sites/streamate/Streamate.java +++ b/common/src/main/java/ctbrec/sites/streamate/Streamate.java @@ -2,7 +2,6 @@ package ctbrec.sites.streamate; import ctbrec.Model; import ctbrec.StringUtil; -import ctbrec.io.HttpClient; import ctbrec.io.HttpException; import ctbrec.sites.AbstractSite; import okhttp3.Request; @@ -71,7 +70,7 @@ public class Streamate extends AbstractSite { } @Override - public HttpClient getHttpClient() { + public StreamateHttpClient getHttpClient() { if (httpClient == null) { httpClient = new StreamateHttpClient(getConfig()); } @@ -114,9 +113,9 @@ public class Streamate extends AbstractSite { public List search(String q) throws IOException, InterruptedException { String url = NAIAD_URL + "/autocomplete?filters=gender:&performerCount=10&domain=streamate.com&tagCount=5&query=" + URLEncoder.encode(q, "utf-8"); LOG.debug("Search: {}", url); - String saKey = httpClient.getSaKey(); + String saKey = getHttpClient().getSaKey(); String smtid = UUID.randomUUID() + "G0211569057409"; - Request req = httpClient.newRequestBuilder() + Request req = getHttpClient().newRequestBuilder() .url(url) .header(ORIGIN, getBaseUrl()) .header("sakey", saKey) diff --git a/common/src/main/java/ctbrec/sites/streamate/StreamateHttpClient.java b/common/src/main/java/ctbrec/sites/streamate/StreamateHttpClient.java index 85258b2b..285b9854 100644 --- a/common/src/main/java/ctbrec/sites/streamate/StreamateHttpClient.java +++ b/common/src/main/java/ctbrec/sites/streamate/StreamateHttpClient.java @@ -130,7 +130,7 @@ public class StreamateHttpClient extends HttpClient { return loggedIn; } - private String getXsrfToken() { + protected String getXsrfToken() { if (xsrfToken.isEmpty()) { loadXsrfToken(); } diff --git a/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java b/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java index a2a8a477..f6821dd7 100644 --- a/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java +++ b/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java @@ -1,34 +1,30 @@ package ctbrec.sites.streamate; -import static ctbrec.Model.State.*; -import static ctbrec.io.HttpConstants.*; -import static ctbrec.sites.streamate.StreamateHttpClient.*; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import java.util.concurrent.ExecutionException; - -import org.json.JSONArray; -import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.iheartradio.m3u8.ParseException; import com.iheartradio.m3u8.PlaylistException; import com.squareup.moshi.JsonReader; import com.squareup.moshi.JsonWriter; - import ctbrec.AbstractModel; import ctbrec.Config; +import ctbrec.Model; import ctbrec.NotImplementedExcetion; import ctbrec.io.HttpException; import ctbrec.recorder.download.StreamSource; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.*; +import java.util.concurrent.ExecutionException; + +import static ctbrec.Model.State.*; +import static ctbrec.io.HttpConstants.*; +import static ctbrec.sites.streamate.StreamateHttpClient.JSON; public class StreamateModel extends AbstractModel { @@ -36,10 +32,13 @@ public class StreamateModel extends AbstractModel { private static final Logger LOG = LoggerFactory.getLogger(StreamateModel.class); + private static final Long MODEL_ID_UNDEFINED = -1L; + private static final String HTTP_BODY_IS_NULL = "HTTP response body is null"; + private boolean online = false; - private transient List streamSources = new ArrayList<>(); + private final transient List streamSources = new ArrayList<>(); private int[] resolution; - private Long id; + private Long id = MODEL_ID_UNDEFINED; @Override public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { @@ -65,14 +64,10 @@ public class StreamateModel extends AbstractModel { @Override public State getOnlineState(boolean failFast) throws IOException, ExecutionException { - if(failFast) { - return onlineState; - } else { - if(onlineState == UNKNOWN) { - return online ? ONLINE : OFFLINE; - } - return onlineState; + if (!failFast && onlineState == UNKNOWN) { + return online ? ONLINE : OFFLINE; } + return onlineState; } @Override @@ -92,7 +87,8 @@ public class StreamateModel extends AbstractModel { .build(); try (Response response = site.getHttpClient().execute(req)) { if (response.isSuccessful()) { - JSONObject json = new JSONObject(response.body().string()); + String body = Objects.requireNonNull(response.body(), HTTP_BODY_IS_NULL).string(); + JSONObject json = new JSONObject(body); JSONObject formats = json.getJSONObject("formats"); JSONObject hls = formats.getJSONObject("mp4-hls"); @@ -133,33 +129,21 @@ public class StreamateModel extends AbstractModel { resolution = null; } - void loadModelId() throws IOException { - String url = "https://www.streamate.com/api/performer/lookup?nicknames=" + getName(); - StreamateHttpClient httpClient = (StreamateHttpClient) getSite().getHttpClient(); - Request request = httpClient.newRequestBuilder() - .url(url) - .build(); - try (Response response = site.getHttpClient().execute(request)) { - if (response.isSuccessful()) { - String body = response.body().string(); - LOG.info(body); - JSONObject json = new JSONObject(body); - JSONObject performer = json.getJSONObject("result"); - id = performer.getLong(getName()); - } else { - throw new HttpException(response.code(), response.message()); - } + void loadModelId() throws IOException, InterruptedException { + List models = getSite().search(getName()); + if (!models.isEmpty()) { + id = ((StreamateModel)models.get(0)).getId(); } } @Override public int[] getStreamResolution(boolean failFast) throws ExecutionException { - if(resolution == null) { - if(failFast) { + if (resolution == null) { + if (failFast) { return new int[2]; } try { - if(!isOnline()) { + if (!isOnline()) { return new int[2]; } List sources = getStreamSources(); @@ -168,17 +152,15 @@ public class StreamateModel extends AbstractModel { if (best.height == StreamSource.ORIGIN) { best = sources.get(sources.size() - 2); } - resolution = new int[] { best.width, best.height }; + resolution = new int[]{best.width, best.height}; } catch (InterruptedException e) { LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage()); Thread.currentThread().interrupt(); } catch (ExecutionException | IOException | ParseException | PlaylistException e) { LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage()); } - return resolution; - } else { - return resolution; } + return resolution; } @Override @@ -205,7 +187,7 @@ public class StreamateModel extends AbstractModel { requestParams.put("fav", follow); RequestBody body = RequestBody.Companion.create(requestParams.toString(), JSON); - String url = site.getBaseUrl() + "/ajax/fav-notify.php?userid="+userId+"&sakey="+saKey+"&pid="+id+"&fav="+follow+"&domain=streamate.com"; + String url = site.getBaseUrl() + "/ajax/fav-notify.php?userid=" + userId + "&sakey=" + saKey + "&pid=" + id + "&fav=" + follow + "&domain=streamate.com"; Request request = new Request.Builder() .url(url) .addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) @@ -215,7 +197,7 @@ public class StreamateModel extends AbstractModel { .post(body) .build(); try (Response response = getSite().getHttpClient().execute(request)) { - String content = response.body().string(); + String content = Objects.requireNonNull(response.body(), HTTP_BODY_IS_NULL).string(); if (response.isSuccessful()) { JSONObject json = new JSONObject(content); return json.optBoolean("success"); @@ -241,11 +223,13 @@ public class StreamateModel extends AbstractModel { @Override public void writeSiteSpecificData(JsonWriter writer) throws IOException { - if (id == null) { + if (id == null || Objects.equals(id, MODEL_ID_UNDEFINED)) { try { loadModelId(); } catch (IOException e) { - LOG.error("Couldn't load model ID for {}. This can cause problems with saving / loading the model", getName()); + LOG.error("Couldn't load model ID for {}. This can cause problems with saving / loading the model", getName(), e); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } } writer.name("id").value(id); @@ -255,4 +239,4 @@ public class StreamateModel extends AbstractModel { public void receiveTip(Double tokens) throws IOException { throw new NotImplementedExcetion("Tipping is not implemented for Streamate"); } -} \ No newline at end of file +}