Fix WinkTv capture

This commit is contained in:
Jafea7 2025-05-04 21:17:44 +10:00
parent 8a482b8fe9
commit 67e48ad969
2 changed files with 96 additions and 72 deletions

View File

@ -63,7 +63,7 @@ public class WinkTv extends AbstractSite {
@Override
public Double getTokenBalance() throws IOException {
return 0d;
return 0.0;
}
@Override
@ -111,6 +111,7 @@ public class WinkTv extends AbstractSite {
if (StringUtil.isBlank(q)) {
return Collections.emptyList();
}
String url = "https://api.winktv.co.kr/v1/live";
FormBody body = new FormBody.Builder()
.add("offset", "0")
@ -118,6 +119,7 @@ public class WinkTv extends AbstractSite {
.add("orderBy", "user")
.add("searchVal", URLEncoder.encode(q, "utf-8"))
.build();
Request req = new Request.Builder()
.url(url)
.header(USER_AGENT, getConfig().getSettings().httpUserAgent)
@ -128,15 +130,24 @@ public class WinkTv extends AbstractSite {
.header(ORIGIN, getBaseUrl())
.post(body)
.build();
try (Response response = getHttpClient().execute(req)) {
if (response.isSuccessful()) {
if (!response.isSuccessful()) {
throw new HttpException(response.code(), response.message());
}
JSONObject json = new JSONObject(response.body().string());
if (json.optBoolean("result") && json.has("list")) {
List<Model> models = new ArrayList<>();
log.debug("WinkTv: Parsed JSON: {}", json.toString());
if (!json.optBoolean("result") || !json.has("list")) {
return Collections.emptyList();
}
JSONArray results = json.getJSONArray("list");
if (results.length() == 0) {
return Collections.emptyList();
}
List<Model> models = new ArrayList<>();
for (int i = 0; i < results.length(); i++) {
JSONObject result = results.getJSONObject(i);
WinkTvModel model = createModel(result.optString("userId"));
@ -144,12 +155,6 @@ public class WinkTv extends AbstractSite {
models.add(model);
}
return models;
} else {
return Collections.emptyList();
}
} else {
throw new HttpException(response.code(), response.message());
}
}
}
@ -165,14 +170,14 @@ public class WinkTv extends AbstractSite {
@Override
public Model createModelFromUrl(String url) {
String[] patterns = {
String[] patterns = new String[]{
"https://.*?winktv.co.kr/live/play/([_a-zA-Z0-9]+)",
"https://.*?winktv.co.kr/channel/([_a-zA-Z0-9]+)",
"https://.*?pandalive.co.kr/live/play/([_a-zA-Z0-9]+)",
"https://.*?pandalive.co.kr/channel/([_a-zA-Z0-9]+)"
};
for (String p : patterns) {
Matcher m = Pattern.compile(p).matcher(url);
for (String pattern : patterns) {
Matcher m = Pattern.compile(pattern).matcher(url);
if (m.matches()) {
String modelName = m.group(1);
return createModel(modelName);

View File

@ -9,9 +9,10 @@ import ctbrec.Config;
import ctbrec.io.HttpException;
import ctbrec.recorder.download.RecordingProcess;
import ctbrec.recorder.download.StreamSource;
import ctbrec.recorder.download.hls.HlsdlDownload;
import ctbrec.recorder.download.hls.MergedFfmpegHlsDownload;
import lombok.Getter;
import lombok.Setter;
// import lombok.Getter;
// import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.Request;
@ -50,6 +51,7 @@ public class WinkTvModel extends AbstractModel {
if (ignoreCache) {
try {
JSONObject json = getModelInfo();
log.debug("WinkTvModel-isOnline: {}", json.toString());
if (json.has(KEY_MEDIA)) {
JSONObject media = json.getJSONObject(KEY_MEDIA);
boolean isLive = media.optBoolean("isLive");
@ -71,7 +73,9 @@ public class WinkTvModel extends AbstractModel {
@Override
public State getOnlineState(boolean failFast) throws IOException, ExecutionException {
if (!failFast || onlineState == UNKNOWN || onlineState == UNCHECKED) {
if (failFast && onlineState != UNKNOWN) {
return onlineState;
}
try {
onlineState = isOnline(true) ? ONLINE : OFFLINE;
} catch (InterruptedException e) {
@ -80,7 +84,6 @@ public class WinkTvModel extends AbstractModel {
} catch (IOException | ExecutionException e) {
onlineState = OFFLINE;
}
}
return onlineState;
}
@ -98,8 +101,12 @@ public class WinkTvModel extends AbstractModel {
if (playlist.hasStreamInfo()) {
StreamSource src = new StreamSource();
src.setBandwidth(playlist.getStreamInfo().getBandwidth());
src.setHeight(playlist.getStreamInfo().getResolution().height);
src.setWidth(playlist.getStreamInfo().getResolution().width);
src.setHeight(Optional.ofNullable(playlist.getStreamInfo().getResolution())
.map(res -> res.height)
.orElse(0));
src.setWidth(Optional.ofNullable(playlist.getStreamInfo().getResolution())
.map(res -> res.width)
.orElse(0));
src.setMediaPlaylistUrl(playlist.getUri());
log.trace("Media playlist {}", src.getMediaPlaylistUrl());
sources.add(src);
@ -113,11 +120,17 @@ public class WinkTvModel extends AbstractModel {
Request req = new Request.Builder()
.url(url)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.header(ACCEPT, "*/*")
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
.header(REFERER, this.getUrl())
.header(ORIGIN, this.getSite().getBaseUrl())
.build();
try (Response response = getSite().getHttpClient().execute(req)) {
if (response.isSuccessful()) {
String body = response.body().string();
InputStream inputStream = new ByteArrayInputStream(body.getBytes(UTF_8));
InputStream inputStream = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
Playlist playlist = parser.parse();
MasterPlaylist master = playlist.getMasterPlaylist();
@ -130,35 +143,13 @@ public class WinkTvModel extends AbstractModel {
private String getMasterPlaylistUrl() throws IOException {
JSONObject json = getModelInfo();
JSONObject info = json.getJSONObject("bjInfo");
long userIdx = info.optLong("idx");
String url = "https://api.winktv.co.kr/v1/live/play";
FormBody body = new FormBody.Builder()
.add("action", "watch")
.add("userIdx", String.valueOf(userIdx))
.add("password", "")
.build();
Request req = new Request.Builder()
.url(url)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
.header(REFERER, getUrl())
.header(ORIGIN, getSite().getBaseUrl())
.post(body)
.build();
try (Response response = getSite().getHttpClient().execute(req)) {
if (response.isSuccessful()) {
JSONObject jsonResponse = new JSONObject(response.body().string());
JSONObject playlist = jsonResponse.getJSONObject("PlayList");
if (json.has("PlayList")) {
JSONObject playlist = json.getJSONObject("PlayList");
JSONObject hls = playlist.getJSONArray("hls").getJSONObject(0);
String hlsUrl = hls.optString("url");
String hlsUrl = hls.optString("url") + "&player_version=1.20.0";
return hlsUrl;
} else {
log.debug("Error while get master playlist url for {}: {}", getName(), response.body().string());
throw new HttpException(response.code(), response.message());
}
throw new IOException("Master playlist URL not found");
}
}
@ -179,18 +170,21 @@ public class WinkTvModel extends AbstractModel {
}
private JSONObject getModelInfo() throws IOException {
if (modelInfo == null || Duration.between(lastInfoRequest, Instant.now()).getSeconds() < 5) {
if (Objects.nonNull(modelInfo) && Duration.between(lastInfoRequest, Instant.now()).getSeconds() < 5L) {
return modelInfo;
}
lastInfoRequest = Instant.now();
modelInfo = loadModelInfo();
}
return modelInfo;
}
private JSONObject loadModelInfo() throws IOException {
String url = "https://api.winktv.co.kr/v1/member/bj";
String url = "https://api.winktv.co.kr/v1/live/play";
FormBody body = new FormBody.Builder()
.add("userId", getName())
.add("info", KEY_MEDIA)
.add("action", "watch")
.add("password", "")
.add("shareLinkType", "")
.build();
Request req = new Request.Builder()
.url(url)
@ -205,6 +199,7 @@ public class WinkTvModel extends AbstractModel {
try (Response response = getSite().getHttpClient().execute(req)) {
if (response.isSuccessful()) {
JSONObject jsonResponse = new JSONObject(response.body().string());
log.debug("WinkTvModel-loadModelInfo: {}", jsonResponse.toString());
return jsonResponse;
} else {
throw new HttpException(response.code(), response.message());
@ -212,11 +207,32 @@ public class WinkTvModel extends AbstractModel {
}
}
public String getPreviewURL() throws IOException {
JSONObject json = this.getModelInfo();
if (json.has("media")) {
JSONObject media = json.getJSONObject("media");
return media.optString("ivsThumbnail");
}
if (json.has("bjInfo")) {
JSONObject info = json.getJSONObject("bjInfo");
return info.optString("thumbUrl");
}
return "";
}
@Override
public boolean follow() throws IOException {
return false;
}
public boolean isAdult() {
return adult;
}
public void setAdult(boolean a) {
adult = a;
}
@Override
public boolean unfollow() throws IOException {
return false;
@ -236,6 +252,9 @@ public class WinkTvModel extends AbstractModel {
@Override
public RecordingProcess createDownload() {
return new MergedFfmpegHlsDownload(getSite().getHttpClient());
if (Config.getInstance().getSettings().useHlsdl) {
return new HlsdlDownload();
}
return new MergedFfmpegHlsDownload(new WinkTvHttpClient(Config.getInstance()));
}
}