Skip to content

Commit

Permalink
fix: again fixed parsing videos from ashdi.vip/tortuga.wtf players
Browse files Browse the repository at this point in the history
  • Loading branch information
MrIkso committed Jun 29, 2024
1 parent 6d1ae42 commit e831c20
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,22 @@
import com.mrikso.anitube.app.network.ApiClient;
import com.mrikso.anitube.app.utils.ParserUtils;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.lindstrom.m3u8.model.MasterPlaylist;
import io.lindstrom.m3u8.model.Resolution;
import io.lindstrom.m3u8.model.Variant;
import io.lindstrom.m3u8.parser.MasterPlaylistParser;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

import okhttp3.OkHttpClient;
import okhttp3.Request;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AhsdiVideosExtractor extends BaseVideoLinkExtracror {
private final String TAG = "AhsdiVideosExtractor";
private final String PLAYER_JS_PATTERN = "Playerjs\\(([^)]+)\\)";
Expand All @@ -44,30 +41,19 @@ private VideoLinksModel getModel(String masterU3u8, PlayerJsResponse playerJs) t
VideoLinksModel model = new VideoLinksModel(playerJs.getFile());
MasterPlaylist masterPlayList = masterPlaylistParser.readPlaylist(masterU3u8);
//Log.i(TAG, "start parse playlist");
//Log.i(TAG, masterPlayList.toString());
Log.i(TAG, masterPlayList.toString());
for (Variant variant : masterPlayList.variants()) {
String uri = variant.uri();
String newUri;
if(uri.startsWith("./")) {
newUri = playerJs.getFile().replace("playlist.m3u8", variant.uri().replaceFirst("./", ""));
}
else {
newUri = uri;
}
// String[] parts = newUri.split("/");
String newUri = uri;
Pattern pattern = Pattern.compile("/hls/(\\d+)/");
Matcher matcher = pattern.matcher(newUri);

// Pattern pattern = Pattern.compile("/hls/(\\d+)/");
// Matcher matcher = pattern.matcher(newUri);
Optional<Resolution> resolutionOptional = variant.resolution();
if(resolutionOptional.isPresent()){
qualitiesMap.put(ParserUtils.standardizeQuality(String.valueOf(resolutionOptional.get().height())), newUri);
}
/*if (matcher.find()) {
if (matcher.find()) {
String resolution = matcher.group(1);
Log.i(TAG, " " + resolution + "=>" + newUri);
qualitiesMap.put(ParserUtils.standardizeQuality(resolution), newUri);
}*/ else {
qualitiesMap.put("AUTO", newUri);
} else {
qualitiesMap.put("AUTO", playerJs.getFile());
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.mrikso.anitube.app.extractors;

import android.util.Log;

import androidx.core.util.Pair;

import com.google.gson.Gson;
import com.mrikso.anitube.app.extractors.model.PlayerJsResponse;
import com.mrikso.anitube.app.model.LoadState;
import com.mrikso.anitube.app.model.VideoLinksModel;
import com.mrikso.anitube.app.network.ApiClient;
import com.mrikso.anitube.app.utils.ParserUtils;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.lindstrom.m3u8.model.MasterPlaylist;
import io.lindstrom.m3u8.model.Variant;
import io.lindstrom.m3u8.parser.MasterPlaylistParser;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class TortugaVideosExtractor extends BaseVideoLinkExtracror {
private final String TAG = "TortugaVideosExtractor";
private final String PLAYER_JS_PATTERN = "Playerjs\\(([^)]+)\\)";
private final MasterPlaylistParser masterPlaylistParser = new MasterPlaylistParser();

public TortugaVideosExtractor(String url, OkHttpClient client) {
super(url, client);
}

private VideoLinksModel getModel(String masterU3u8, PlayerJsResponse playerJs) throws IOException {
Map<String, String> qualitiesMap = new HashMap<>();
VideoLinksModel model = new VideoLinksModel(playerJs.getFile());
MasterPlaylist masterPlayList = masterPlaylistParser.readPlaylist(masterU3u8);
//Log.i(TAG, "start parse playlist");
Log.i(TAG, masterPlayList.toString());
for (Variant variant : masterPlayList.variants()) {
String uri = variant.uri();

Pattern pattern = Pattern.compile("./(\\d+)/");
Matcher matcher = pattern.matcher(uri);

if (matcher.find()) {
String resolution = matcher.group(1);
Log.i(TAG, " " + resolution + "=>" + uri);
uri = playerJs.getFile().replaceAll("playlist.m3u8|index.m3u8", variant.uri().replaceFirst("./", ""));
qualitiesMap.put(ParserUtils.standardizeQuality(resolution), uri);
} else {
qualitiesMap.put("AUTO", playerJs.getFile());
}

/*if(uri.startsWith("./")) {
newUri = playerJs.getFile().replace("playlist.m3u8", variant.uri().replaceFirst("./", ""));
}
else {
newUri = uri;
}
Optional<Resolution> resolutionOptional = variant.resolution();
if(resolutionOptional.isPresent()){
qualitiesMap.put(ParserUtils.standardizeQuality(String.valueOf(resolutionOptional.get().height())), newUri);
}
else {
qualitiesMap.put("AUTO", newUri);
}*/

}
model.setHeaders(Collections.singletonMap("User-Agent", ApiClient.DESKTOP_USER_AGENT));
model.setLinksQuality(qualitiesMap);
model.setDefaultQuality(ParserUtils.standardizeQuality(playerJs.getDefaultQuality()));
model.setSubtileUrl(playerJs.getSubtitle());
return model;
}

private Single<Pair<String, PlayerJsResponse>> downloadManifest() {
return Single.create(emitter -> {
String page = client.newCall(
new Request.Builder().url(getUrl()).get().build())
.execute()
.body()
.string();
Gson gson = new Gson();
// String jsCode = getDocument().selectFirst("script").data();
// Log.i(TAG, " " + getDocument().bo());
String json = ParserUtils.getMatcherResult(PLAYER_JS_PATTERN, page, 1);
// Log.i(TAG, " " + json);
PlayerJsResponse playerJs = gson.fromJson(json, PlayerJsResponse.class);

String masterPlaylist = client.newCall(
new Request.Builder().url(playerJs.getFile()).get().build())
.execute()
.body()
.string();

emitter.onSuccess(new Pair<>(masterPlaylist, playerJs));
});
}

@Override
public Single<Pair<LoadState, VideoLinksModel>> parse() {
return downloadManifest()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(v -> new Pair<>(LoadState.DONE, getModel(v.first, v.second)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.mrikso.anitube.app.extractors.MoonAnimeArtExtractor;
import com.mrikso.anitube.app.extractors.PeertubeExtractor;
import com.mrikso.anitube.app.extractors.StreamSBExtractor;
import com.mrikso.anitube.app.extractors.TortugaVideosExtractor;
import com.mrikso.anitube.app.extractors.UdropExtractor;
import com.mrikso.anitube.app.extractors.VeohVideodExtractor;
import com.mrikso.anitube.app.extractors.csstExtractor;
Expand All @@ -23,10 +24,6 @@
import com.mrikso.anitube.app.model.LoadState;
import com.mrikso.anitube.app.model.VideoLinksModel;

import io.reactivex.rxjava3.core.Single;

import okhttp3.OkHttpClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -35,6 +32,9 @@

import javax.inject.Inject;

import io.reactivex.rxjava3.core.Single;
import okhttp3.OkHttpClient;

public class DirectVideoUrlParser {
private static final String TAG = "DirectVideoUrlParser";
private final OkHttpClient client;
Expand Down Expand Up @@ -69,7 +69,7 @@ public Single<Pair<LoadState, VideoLinksModel>> getDirectUrl(String iframeUrl) {
BaseVideoLinkExtracror extracror;

if (iframeDomain.contains("tortuga.wtf")) {
extracror = new AhsdiVideosExtractor(iframeUrl, client);
extracror = new TortugaVideosExtractor(iframeUrl, client);
} else if (iframeDomain.contains("ashdi.vip")) {
extracror = new AhsdiVideosExtractor(iframeUrl, client);
} else if (iframeDomain.contains("udrop.com")) {
Expand Down

0 comments on commit e831c20

Please sign in to comment.