pull/171/head
FongMi 3 years ago
parent f1c194c2b3
commit 3a5a37e523
  1. 19
      app/src/main/java/com/fongmi/android/tv/server/Nano.java
  2. 26
      app/src/main/java/com/fongmi/android/tv/utils/M3U8.java

@ -10,6 +10,7 @@ import com.fongmi.android.tv.server.process.Local;
import com.fongmi.android.tv.server.process.Process;
import com.fongmi.android.tv.utils.M3U8;
import com.github.catvod.Init;
import com.google.common.net.HttpHeaders;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -55,6 +56,13 @@ public class Nano extends NanoHTTPD {
return newFixedLengthResponse(status, MIME_PLAINTEXT, text);
}
public static Response redirect(String url, Map<String, String> headers) {
Response response = newFixedLengthResponse(Response.Status.REDIRECT, MIME_HTML, "");
for (Map.Entry<String, String> entry : headers.entrySet()) response.addHeader(entry.getKey(), entry.getValue());
response.addHeader(HttpHeaders.LOCATION, url);
return response;
}
@Override
public Response serve(IHTTPSession session) {
String url = session.getUri().trim();
@ -84,13 +92,10 @@ public class Nano extends NanoHTTPD {
}
private Response m3u8(IHTTPSession session) {
try {
String url = session.getParms().get("url");
String result = M3U8.get(url, session.getHeaders());
return newChunkedResponse(Response.Status.OK, MIME_PLAINTEXT, new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)));
} catch (Exception e) {
return error(e.getMessage());
}
String url = session.getParms().get("url");
String result = M3U8.get(url, session.getHeaders());
if (result.isEmpty()) return redirect(url, session.getHeaders());
return newChunkedResponse(Response.Status.OK, MIME_PLAINTEXT, new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)));
}
private Response proxy(Map<String, String> params) {

@ -14,6 +14,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import okhttp3.Headers;
import okhttp3.Response;
public class M3U8 {
@ -25,14 +26,21 @@ public class M3U8 {
private static final Pattern REGEX_MEDIA_DURATION = Pattern.compile(TAG_MEDIA_DURATION + ":([\\d\\.]+)\\b");
private static final Pattern REGEX_URI = Pattern.compile("URI=\"(.+?)\"");
public static String get(String url, Map<String, String> headers) throws Exception {
String result = OkHttp.newCall(url, getHeader(headers)).execute().body().string();
Matcher matcher = Pattern.compile("#EXT-X-STREAM-INF(.*)\\n?(.*)").matcher(result);
if (matcher.find() && matcher.groupCount() > 1) return get(UriUtil.resolve(url, matcher.group(2)), headers);
StringBuilder sb = new StringBuilder();
for (String line : result.split("\n")) sb.append(shouldResolve(line) ? resolve(url, line) : line).append("\n");
List<String> ads = Sniffer.getRegex(Uri.parse(url));
return clean(sb.toString(), ads);
public static String get(String url, Map<String, String> headers) {
try {
Response response = OkHttp.newCall(url, getHeader(headers)).execute();
if (response.header(HttpHeaders.ACCEPT_RANGES) != null) return "";
String result = response.body().string();
Matcher matcher = Pattern.compile("#EXT-X-STREAM-INF(.*)\\n?(.*)").matcher(result);
if (matcher.find() && matcher.groupCount() > 1) return get(UriUtil.resolve(url, matcher.group(2)), headers);
StringBuilder sb = new StringBuilder();
for (String line : result.split("\n")) sb.append(shouldResolve(line) ? resolve(url, line) : line).append("\n");
List<String> ads = Sniffer.getRegex(Uri.parse(url));
return clean(sb.toString(), ads);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
private static String clean(String line, List<String> ads) {
@ -48,7 +56,7 @@ public class M3U8 {
Matcher m1 = REGEX_X_DISCONTINUITY.matcher(line);
while (m1.find()) {
String group = m1.group();
BigDecimal t = BigDecimal.ZERO;;
BigDecimal t = BigDecimal.ZERO;
Matcher m2 = REGEX_MEDIA_DURATION.matcher(group);
while (m2.find()) t = t.add(new BigDecimal(m2.group(1)));
for (String ad : ads) if (t.toString().startsWith(ad)) line = line.replaceAll(group, "");

Loading…
Cancel
Save