Fix loading of Streamate model ID

This commit is contained in:
0xb00bface 2021-12-18 17:18:45 +01:00
parent e10e704298
commit e3d8d0abb6
3 changed files with 43 additions and 60 deletions

View File

@ -2,7 +2,6 @@ package ctbrec.sites.streamate;
import ctbrec.Model; import ctbrec.Model;
import ctbrec.StringUtil; import ctbrec.StringUtil;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
import ctbrec.sites.AbstractSite; import ctbrec.sites.AbstractSite;
import okhttp3.Request; import okhttp3.Request;
@ -71,7 +70,7 @@ public class Streamate extends AbstractSite {
} }
@Override @Override
public HttpClient getHttpClient() { public StreamateHttpClient getHttpClient() {
if (httpClient == null) { if (httpClient == null) {
httpClient = new StreamateHttpClient(getConfig()); httpClient = new StreamateHttpClient(getConfig());
} }
@ -114,9 +113,9 @@ public class Streamate extends AbstractSite {
public List<Model> search(String q) throws IOException, InterruptedException { public List<Model> 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"); String url = NAIAD_URL + "/autocomplete?filters=gender:&performerCount=10&domain=streamate.com&tagCount=5&query=" + URLEncoder.encode(q, "utf-8");
LOG.debug("Search: {}", url); LOG.debug("Search: {}", url);
String saKey = httpClient.getSaKey(); String saKey = getHttpClient().getSaKey();
String smtid = UUID.randomUUID() + "G0211569057409"; String smtid = UUID.randomUUID() + "G0211569057409";
Request req = httpClient.newRequestBuilder() Request req = getHttpClient().newRequestBuilder()
.url(url) .url(url)
.header(ORIGIN, getBaseUrl()) .header(ORIGIN, getBaseUrl())
.header("sakey", saKey) .header("sakey", saKey)

View File

@ -130,7 +130,7 @@ public class StreamateHttpClient extends HttpClient {
return loggedIn; return loggedIn;
} }
private String getXsrfToken() { protected String getXsrfToken() {
if (xsrfToken.isEmpty()) { if (xsrfToken.isEmpty()) {
loadXsrfToken(); loadXsrfToken();
} }

View File

@ -1,34 +1,30 @@
package ctbrec.sites.streamate; 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.ParseException;
import com.iheartradio.m3u8.PlaylistException; import com.iheartradio.m3u8.PlaylistException;
import com.squareup.moshi.JsonReader; import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter; import com.squareup.moshi.JsonWriter;
import ctbrec.AbstractModel; import ctbrec.AbstractModel;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.Model;
import ctbrec.NotImplementedExcetion; import ctbrec.NotImplementedExcetion;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
import ctbrec.recorder.download.StreamSource; import ctbrec.recorder.download.StreamSource;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.RequestBody; import okhttp3.RequestBody;
import okhttp3.Response; 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 { 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 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 boolean online = false;
private transient List<StreamSource> streamSources = new ArrayList<>(); private final transient List<StreamSource> streamSources = new ArrayList<>();
private int[] resolution; private int[] resolution;
private Long id; private Long id = MODEL_ID_UNDEFINED;
@Override @Override
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
@ -65,14 +64,10 @@ public class StreamateModel extends AbstractModel {
@Override @Override
public State getOnlineState(boolean failFast) throws IOException, ExecutionException { public State getOnlineState(boolean failFast) throws IOException, ExecutionException {
if(failFast) { if (!failFast && onlineState == UNKNOWN) {
return onlineState; return online ? ONLINE : OFFLINE;
} else {
if(onlineState == UNKNOWN) {
return online ? ONLINE : OFFLINE;
}
return onlineState;
} }
return onlineState;
} }
@Override @Override
@ -92,7 +87,8 @@ public class StreamateModel extends AbstractModel {
.build(); .build();
try (Response response = site.getHttpClient().execute(req)) { try (Response response = site.getHttpClient().execute(req)) {
if (response.isSuccessful()) { 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 formats = json.getJSONObject("formats");
JSONObject hls = formats.getJSONObject("mp4-hls"); JSONObject hls = formats.getJSONObject("mp4-hls");
@ -133,33 +129,21 @@ public class StreamateModel extends AbstractModel {
resolution = null; resolution = null;
} }
void loadModelId() throws IOException { void loadModelId() throws IOException, InterruptedException {
String url = "https://www.streamate.com/api/performer/lookup?nicknames=" + getName(); List<Model> models = getSite().search(getName());
StreamateHttpClient httpClient = (StreamateHttpClient) getSite().getHttpClient(); if (!models.isEmpty()) {
Request request = httpClient.newRequestBuilder() id = ((StreamateModel)models.get(0)).getId();
.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());
}
} }
} }
@Override @Override
public int[] getStreamResolution(boolean failFast) throws ExecutionException { public int[] getStreamResolution(boolean failFast) throws ExecutionException {
if(resolution == null) { if (resolution == null) {
if(failFast) { if (failFast) {
return new int[2]; return new int[2];
} }
try { try {
if(!isOnline()) { if (!isOnline()) {
return new int[2]; return new int[2];
} }
List<StreamSource> sources = getStreamSources(); List<StreamSource> sources = getStreamSources();
@ -168,17 +152,15 @@ public class StreamateModel extends AbstractModel {
if (best.height == StreamSource.ORIGIN) { if (best.height == StreamSource.ORIGIN) {
best = sources.get(sources.size() - 2); best = sources.get(sources.size() - 2);
} }
resolution = new int[] { best.width, best.height }; resolution = new int[]{best.width, best.height};
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage()); LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage());
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (ExecutionException | IOException | ParseException | PlaylistException e) { } catch (ExecutionException | IOException | ParseException | PlaylistException e) {
LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage()); LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage());
} }
return resolution;
} else {
return resolution;
} }
return resolution;
} }
@Override @Override
@ -205,7 +187,7 @@ public class StreamateModel extends AbstractModel {
requestParams.put("fav", follow); requestParams.put("fav", follow);
RequestBody body = RequestBody.Companion.create(requestParams.toString(), JSON); 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() Request request = new Request.Builder()
.url(url) .url(url)
.addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) .addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
@ -215,7 +197,7 @@ public class StreamateModel extends AbstractModel {
.post(body) .post(body)
.build(); .build();
try (Response response = getSite().getHttpClient().execute(request)) { 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()) { if (response.isSuccessful()) {
JSONObject json = new JSONObject(content); JSONObject json = new JSONObject(content);
return json.optBoolean("success"); return json.optBoolean("success");
@ -241,11 +223,13 @@ public class StreamateModel extends AbstractModel {
@Override @Override
public void writeSiteSpecificData(JsonWriter writer) throws IOException { public void writeSiteSpecificData(JsonWriter writer) throws IOException {
if (id == null) { if (id == null || Objects.equals(id, MODEL_ID_UNDEFINED)) {
try { try {
loadModelId(); loadModelId();
} catch (IOException e) { } 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); writer.name("id").value(id);