From 9cf4bacae1b3a19d90381d29f45ba318c0920149 Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Fri, 16 Jun 2023 21:27:20 +0200 Subject: [PATCH] User another request to determine the model ID --- .../sites/streamate/StreamateModel.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java b/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java index a9c1ba86..5e9a0697 100644 --- a/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java +++ b/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java @@ -6,7 +6,6 @@ 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; @@ -19,6 +18,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.net.URLEncoder; import java.util.*; import java.util.concurrent.ExecutionException; @@ -26,6 +26,7 @@ import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL; import static ctbrec.Model.State.*; import static ctbrec.io.HttpConstants.*; import static ctbrec.sites.streamate.StreamateHttpClient.JSON; +import static java.nio.charset.StandardCharsets.UTF_8; public class StreamateModel extends AbstractModel { @@ -70,11 +71,6 @@ public class StreamateModel extends AbstractModel { return onlineState; } - @Override - public void setOnlineState(State onlineState) { - this.onlineState = onlineState; - } - @Override public List getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException { String url = "https://sea1c-ls.naiadsystems.com/sea1c-edge-ls/80/live/s:" + getName() + ".json"; @@ -129,10 +125,22 @@ public class StreamateModel extends AbstractModel { resolution = null; } - void loadModelId() throws IOException, InterruptedException { - List models = getSite().search(getName()); - if (!models.isEmpty()) { - id = ((StreamateModel)models.get(0)).getId(); + void loadModelId() throws IOException { + String url = "https://www.streamate.com/api/performer/lookup?nicknames" + URLEncoder.encode(getName(), UTF_8); + Request req = new Request.Builder().url(url) + .addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) + .addHeader(ACCEPT, "*/*") + .addHeader(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage()) + .addHeader(REFERER, Streamate.BASE_URL + '/' + getName()) + .addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST) + .build(); + try (Response response = site.getHttpClient().execute(req)) { + if (response.isSuccessful()) { + String body = Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string(); + id = new JSONObject(body).getJSONObject("result").getLong(getName()); + } else { + throw new HttpException(response.code(), response.message()); + } } } @@ -228,8 +236,6 @@ public class StreamateModel extends AbstractModel { loadModelId(); } catch (IOException e) { 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);