diff --git a/common/src/main/java/ctbrec/sites/manyvids/MVLiveClient.java b/common/src/main/java/ctbrec/sites/manyvids/MVLiveClient.java index 8b6b9f22..423f821b 100644 --- a/common/src/main/java/ctbrec/sites/manyvids/MVLiveClient.java +++ b/common/src/main/java/ctbrec/sites/manyvids/MVLiveClient.java @@ -59,7 +59,7 @@ public class MVLiveClient { running = true; if (ws == null && !connecting) { - fetchAuthenticationCookies(); + httpClient.fetchAuthenticationCookies(); JSONObject response = getRoomLocation(model); roomNumber = response.optString("floorId"); roomId = response.optString("roomId"); @@ -88,18 +88,6 @@ public class MVLiveClient { } } - private void fetchAuthenticationCookies() throws IOException { - Request req = new Request.Builder() - .url("https://www.manyvids.com/tak-live-redirect.php") - .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) - .build(); - try (Response response = httpClient.execute(req)) { - if (!response.isSuccessful()) { - throw new HttpException(response.code(), response.message()); - } - } - } - private String getPhpSessionIdCookie() { List cookies = httpClient.getCookiesByName("PHPSESSID"); return cookies.stream().map(c -> c.name() + "=" + c.value()).findFirst().orElse(""); diff --git a/common/src/main/java/ctbrec/sites/manyvids/MVLiveHttpClient.java b/common/src/main/java/ctbrec/sites/manyvids/MVLiveHttpClient.java index e85f609a..aa4ce032 100644 --- a/common/src/main/java/ctbrec/sites/manyvids/MVLiveHttpClient.java +++ b/common/src/main/java/ctbrec/sites/manyvids/MVLiveHttpClient.java @@ -1,8 +1,14 @@ package ctbrec.sites.manyvids; +import static ctbrec.io.HttpConstants.*; + import java.io.IOException; +import ctbrec.Config; import ctbrec.io.HttpClient; +import ctbrec.io.HttpException; +import okhttp3.Request; +import okhttp3.Response; public class MVLiveHttpClient extends HttpClient { @@ -14,4 +20,26 @@ public class MVLiveHttpClient extends HttpClient { public boolean login() throws IOException { return false; } + + public MVLiveHttpClient newSession() { + MVLiveHttpClient newClient = new MVLiveHttpClient(); + newClient.client = newClient.client.newBuilder() + .cookieJar(createCookieJar()) + .build(); + newClient.reconfigure(); + newClient.cookieJar.clear(); + return newClient; + } + + public void fetchAuthenticationCookies() throws IOException { + Request req = new Request.Builder() + .url("https://www.manyvids.com/tak-live-redirect.php") + .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) + .build(); + try (Response response = execute(req)) { + if (!response.isSuccessful()) { + throw new HttpException(response.code(), response.message()); + } + } + } } diff --git a/common/src/main/java/ctbrec/sites/manyvids/MVLiveMergedHlsDownload.java b/common/src/main/java/ctbrec/sites/manyvids/MVLiveMergedHlsDownload.java index 8c9cdd10..26a4fc0d 100644 --- a/common/src/main/java/ctbrec/sites/manyvids/MVLiveMergedHlsDownload.java +++ b/common/src/main/java/ctbrec/sites/manyvids/MVLiveMergedHlsDownload.java @@ -32,6 +32,15 @@ public class MVLiveMergedHlsDownload extends MergedFfmpegHlsDownload { return t; }); scheduler.scheduleAtFixedRate(() -> updateCloudFlareCookies(), 2, 2, TimeUnit.MINUTES); + + try { + + ((MVLiveModel)getModel()).getRoomLocation(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + updateCloudFlareCookies(); super.start(); } finally { scheduler.shutdown(); diff --git a/common/src/main/java/ctbrec/sites/manyvids/MVLiveModel.java b/common/src/main/java/ctbrec/sites/manyvids/MVLiveModel.java index 2eb72385..c03646dc 100644 --- a/common/src/main/java/ctbrec/sites/manyvids/MVLiveModel.java +++ b/common/src/main/java/ctbrec/sites/manyvids/MVLiveModel.java @@ -39,6 +39,7 @@ public class MVLiveModel extends AbstractModel { private static final transient Logger LOG = LoggerFactory.getLogger(MVLiveModel.class); + private MVLiveHttpClient httpClient; private MVLiveClient client; private String roomNumber; @@ -87,7 +88,7 @@ public class MVLiveModel extends AbstractModel { .url(url) .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) .build(); - try (Response response = getSite().getHttpClient().execute(req)) { + try (Response response = getHttpClient().execute(req)) { if (response.isSuccessful()) { String body = response.body().string(); LOG.debug(body); @@ -109,7 +110,7 @@ public class MVLiveModel extends AbstractModel { .url(url) .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) .build(); - try (Response response = site.getHttpClient().execute(req)) { + try (Response response = getHttpClient().execute(req)) { if (!response.isSuccessful()) { throw new HttpException(response.code(), response.message()); } @@ -128,7 +129,21 @@ public class MVLiveModel extends AbstractModel { return roomNumber; } - private JSONObject getRoomLocation() throws IOException { + private void fetchGeneralCookies() throws IOException { + Request req = new Request.Builder() + .url(getSite().getBaseUrl()) + .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) + .build(); + try (Response response = getHttpClient().execute(req)) { + if (!response.isSuccessful()) { + throw new HttpException(response.code(), response.message()); + } + } + } + + public JSONObject getRoomLocation() throws IOException { + fetchGeneralCookies(); + httpClient.fetchAuthenticationCookies(); String url = MVLive.WS_ORIGIN + "/api/roomlocation/" + getName() + "?private=false"; Request req = new Request.Builder() .url(url) @@ -137,7 +152,7 @@ public class MVLiveModel extends AbstractModel { .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) .header(REFERER, MVLive.WS_ORIGIN + "/stream/" + getName()) .build(); - try (Response response = site.getHttpClient().execute(req)) { + try (Response response = getHttpClient().execute(req)) { if (response.isSuccessful()) { JSONObject json = new JSONObject(response.body().string()); return json; @@ -149,9 +164,7 @@ public class MVLiveModel extends AbstractModel { private synchronized MVLiveClient getClient() { if (client == null) { - MVLive site = (MVLive) getSite(); - MVLiveHttpClient httpClient = (MVLiveHttpClient) site.getHttpClient(); - client = new MVLiveClient(httpClient); + client = new MVLiveClient(getHttpClient()); } return client; } @@ -183,9 +196,17 @@ public class MVLiveModel extends AbstractModel { @Override public Download createDownload() { if (Config.isServerMode() && !Config.getInstance().getSettings().recordSingleFile) { - return new HlsDownload(getSite().getHttpClient()); + return new HlsDownload(getHttpClient()); } else { - return new MVLiveMergedHlsDownload(getSite().getHttpClient()); + return new MVLiveMergedHlsDownload(getHttpClient()); } } + + private synchronized MVLiveHttpClient getHttpClient() { + if (httpClient == null) { + MVLiveHttpClient siteHttpClient = (MVLiveHttpClient) getSite().getHttpClient(); + httpClient = siteHttpClient.newSession(); + } + return httpClient; + } }