forked from j62/ctbrec
1
0
Fork 0

Code cleanup

This commit is contained in:
0xb00bface 2022-10-15 20:04:55 +02:00
parent 856743c00a
commit 8b31df73c5
2 changed files with 87 additions and 107 deletions

View File

@ -1,14 +1,6 @@
package ctbrec.ui.sites.chaturbate;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.sites.chaturbate.Chaturbate;
import ctbrec.sites.chaturbate.ChaturbateModelParser;
@ -16,13 +8,23 @@ import ctbrec.ui.SiteUiFactory;
import ctbrec.ui.tabs.PaginatedScheduledService;
import javafx.concurrent.Task;
import okhttp3.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static ctbrec.io.HttpConstants.*;
public class ChaturbateUpdateService extends PaginatedScheduledService {
private static final Logger LOG = LoggerFactory.getLogger(ChaturbateUpdateService.class);
private String url;
private boolean loginRequired;
private Chaturbate chaturbate;
private final boolean loginRequired;
private final Chaturbate chaturbate;
public ChaturbateUpdateService(String url, boolean loginRequired, Chaturbate chaturbate) {
this.url = url;
@ -40,7 +42,7 @@ public class ChaturbateUpdateService extends PaginatedScheduledService {
@Override
protected Task<List<Model>> createTask() {
return new Task<List<Model>>() {
return new Task<>() {
@Override
public List<Model> call() throws IOException {
if (loginRequired && !chaturbate.credentialsAvailable()) {
@ -51,7 +53,11 @@ public class ChaturbateUpdateService extends PaginatedScheduledService {
if (loginRequired) {
SiteUiFactory.getUi(chaturbate).login();
}
var request = new Request.Builder().url(pageUrl).build();
var request = new Request.Builder()
.url(pageUrl)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.header(ACCEPT, MIMETYPE_TEXT_HTML)
.build();
try (var response = chaturbate.getHttpClient().execute(request)) {
if (response.isSuccessful()) {
List<Model> models = ChaturbateModelParser.parseModels(chaturbate, response.body().string());

View File

@ -1,38 +1,11 @@
package ctbrec.sites.chaturbate;
import static ctbrec.Model.State.*;
import static ctbrec.io.HttpConstants.*;
import static java.nio.charset.StandardCharsets.*;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.iheartradio.m3u8.Encoding;
import com.iheartradio.m3u8.Format;
import com.iheartradio.m3u8.ParseException;
import com.iheartradio.m3u8.ParsingMode;
import com.iheartradio.m3u8.PlaylistException;
import com.iheartradio.m3u8.PlaylistParser;
import com.iheartradio.m3u8.*;
import com.iheartradio.m3u8.data.MasterPlaylist;
import com.iheartradio.m3u8.data.Playlist;
import com.iheartradio.m3u8.data.PlaylistData;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import ctbrec.AbstractModel;
import ctbrec.Config;
import ctbrec.io.HttpException;
@ -41,6 +14,22 @@ import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ExecutionException;
import static ctbrec.Model.State.*;
import static ctbrec.io.HttpConstants.*;
import static java.nio.charset.StandardCharsets.UTF_8;
public class ChaturbateModel extends AbstractModel { // NOSONAR
@ -50,9 +39,11 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
private transient StreamInfo streamInfo;
private transient Instant lastStreamInfoRequest = Instant.EPOCH;
/**
* This constructor exists only for deserialization. Please don't call it directly
*/
@SuppressWarnings("unused")
public ChaturbateModel() {
}
@ -115,30 +106,18 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
private void setOnlineStateByRoomStatus(String roomStatus) {
if (roomStatus != null) {
switch (roomStatus) {
case PUBLIC:
case "Unknown":
onlineState = ONLINE;
break;
case "offline":
onlineState = OFFLINE;
break;
case "private":
case "hidden":
case "password protected":
onlineState = PRIVATE;
break;
case "away":
onlineState = AWAY;
break;
case "group":
onlineState = State.GROUP;
break;
default:
case PUBLIC, "Unknown" -> onlineState = ONLINE;
case "offline" -> onlineState = OFFLINE;
case "private", "hidden", "password protected" -> onlineState = PRIVATE;
case "away" -> onlineState = AWAY;
case "group" -> onlineState = State.GROUP;
default -> {
LOG.debug("Unknown show type {}", roomStatus);
onlineState = State.UNKNOWN;
}
}
}
}
@Override
public void receiveTip(Double tokens) throws IOException {
@ -165,7 +144,6 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
@Override
public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException {
try {
streamInfo = loadStreamInfo();
MasterPlaylist masterPlaylist = getMasterPlaylist();
List<StreamSource> sources = new ArrayList<>();
@ -186,10 +164,6 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
}
}
return sources;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ExecutionException(e);
}
}
@Override
@ -203,15 +177,15 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
}
private boolean follow(boolean follow) throws IOException {
// do an initial request to get cookies
Request req = new Request.Builder()
.url(getUrl())
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.build();
try (Response resp = site.getHttpClient().execute(req)) {
// do an initial request to get cookies
}
Response resp = site.getHttpClient().execute(req);
resp.close();
String url = null;
String url;
if (follow) {
url = getSite().getBaseUrl() + "/follow/follow/" + getName() + "/";
} else {
@ -246,11 +220,11 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
}
}
private StreamInfo getStreamInfo() throws IOException, InterruptedException {
private StreamInfo getStreamInfo() throws IOException {
return getStreamInfo(false);
}
private StreamInfo getStreamInfo(boolean failFast) throws IOException, InterruptedException {
private StreamInfo getStreamInfo(boolean failFast) throws IOException {
if (failFast) {
return streamInfo;
} else {
@ -289,7 +263,7 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
}
}
private int[] getResolution() throws IOException, ParseException, PlaylistException, InterruptedException {
private int[] getResolution() throws IOException, ParseException, PlaylistException {
int[] res = new int[2];
if (!getStreamInfo().url.startsWith("http")) {
return res;
@ -325,7 +299,7 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
return res;
}
public MasterPlaylist getMasterPlaylist() throws IOException, ParseException, PlaylistException, InterruptedException {
public MasterPlaylist getMasterPlaylist() throws IOException, ParseException, PlaylistException {
return getMasterPlaylist(getStreamInfo());
}