pull/605/head
jhengazuji 4 months ago
parent 665498ec5c
commit 191a96b14d
  1. 4
      app/src/main/java/com/fongmi/android/tv/player/extractor/JianPian.java
  2. 63
      app/src/main/java/com/fongmi/android/tv/player/extractor/Thunder.java
  3. 20
      app/src/main/java/com/fongmi/android/tv/player/extractor/Youtube.java

@ -10,7 +10,7 @@ import com.p2p.P2PClass;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
public class JianPian implements Source.Extractor {
@ -19,7 +19,7 @@ public class JianPian implements Source.Extractor {
@Override
public boolean match(Uri uri) {
return Arrays.asList("tvbox-xg", "jianpian", "ftp").contains(UrlUtil.scheme(uri));
return List.of("tvbox-xg", "jianpian", "ftp").contains(UrlUtil.scheme(uri));
}
private void init() {

@ -3,10 +3,12 @@ package com.fongmi.android.tv.player.extractor;
import android.net.Uri;
import android.os.SystemClock;
import com.fongmi.android.tv.R;
import com.fongmi.android.tv.bean.Episode;
import com.fongmi.android.tv.exception.ExtractException;
import com.fongmi.android.tv.player.Source;
import com.fongmi.android.tv.utils.Download;
import com.fongmi.android.tv.utils.ResUtil;
import com.fongmi.android.tv.utils.UrlUtil;
import com.github.catvod.utils.Path;
import com.github.catvod.utils.Util;
@ -16,12 +18,11 @@ import com.xunlei.downloadlib.parameter.TorrentFileInfo;
import com.xunlei.downloadlib.parameter.XLTaskInfo;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Thunder implements Source.Extractor {
@ -29,7 +30,7 @@ public class Thunder implements Source.Extractor {
@Override
public boolean match(Uri uri) {
return Arrays.asList("magnet", "ed2k").contains(UrlUtil.scheme(uri));
return List.of("magnet", "ed2k").contains(UrlUtil.scheme(uri));
}
@Override
@ -39,15 +40,17 @@ public class Thunder implements Source.Extractor {
private String addTorrentTask(Uri uri) throws Exception {
File torrent = new File(uri.getPath());
File parent = torrent.getParentFile();
String name = uri.getQueryParameter("name");
int index = Integer.parseInt(uri.getQueryParameter("index"));
taskId = XLTaskHelper.get().addTorrentTask(torrent, Objects.requireNonNull(torrent.getParentFile()), index);
while (true) {
XLTaskInfo taskInfo = XLTaskHelper.get().getBtSubTaskInfo(taskId, index).mTaskInfo;
if (taskInfo.mTaskStatus == 3) throw new ExtractException(taskInfo.getErrorMsg());
if (taskInfo.mTaskStatus != 0) return XLTaskHelper.get().getLocalUrl(new File(torrent.getParent(), name));
else SystemClock.sleep(300);
taskId = XLTaskHelper.get().addTorrentTask(torrent, parent, index);
for (int i = 0; i < 100; i++) {
XLTaskInfo info = XLTaskHelper.get().getBtSubTaskInfo(taskId, index).mTaskInfo;
if (info.mTaskStatus == 3) throw new ExtractException(info.getErrorMsg());
if (info.mTaskStatus != 0) return XLTaskHelper.get().getLocalUrl(new File(parent, name));
SystemClock.sleep(100);
}
throw new ExtractException(ResUtil.getString(R.string.error_play_timeout));
}
private String addThunderTask(String url) {
@ -69,45 +72,49 @@ public class Thunder implements Source.Extractor {
XLTaskHelper.get().release();
}
public static class Parser implements Callable<List<Episode>> {
public record Parser(String url) implements Callable<List<Episode>> {
private static final Pattern THUNDER = Pattern.compile("(magnet|thunder|ed2k):.*");
private final String url;
private int time;
private static final Pattern PATTERN = Pattern.compile("(magnet|thunder|ed2k):.*");
public static boolean match(String url) {
return THUNDER.matcher(url).find() || isTorrent(url);
return PATTERN.matcher(url).find() || isTorrent(url);
}
public static Parser get(String url) {
return new Parser(url);
}
public Parser(String url) {
this.url = url;
private Episode create(GetTaskId taskId) {
return Episode.create(taskId.getFileName(), taskId.getRealUrl());
}
private void sleep() {
SystemClock.sleep(10);
time += 10;
private Episode create(TorrentFileInfo info) {
return Episode.create(info.getFileName(), info.getSize(), info.getPlayUrl());
}
private static boolean isTorrent(String url) {
return !url.startsWith("magnet") && url.split(";")[0].endsWith(".torrent");
return !url.startsWith("magnet") && url.split(";")[0].toLowerCase().endsWith(".torrent");
}
@Override
public List<Episode> call() {
boolean torrent = isTorrent(url);
List<Episode> episodes = new ArrayList<>();
GetTaskId taskId = XLTaskHelper.get().parse(url, Path.thunder(Util.md5(url)));
if (!torrent && !taskId.getRealUrl().startsWith("magnet")) return Arrays.asList(Episode.create(taskId.getFileName(), taskId.getRealUrl()));
if (torrent) Download.create(url, taskId.getSaveFile()).start();
else while (XLTaskHelper.get().getTaskInfo(taskId).getTaskStatus() != 2 && time < 5000) sleep();
List<TorrentFileInfo> medias = XLTaskHelper.get().getTorrentInfo(taskId.getSaveFile()).getMedias();
for (TorrentFileInfo media : medias) episodes.add(Episode.create(media.getFileName(), media.getSize(), media.getPlayUrl()));
XLTaskHelper.get().stopTask(taskId);
return episodes;
if (!torrent && !taskId.getRealUrl().startsWith("magnet")) return Arrays.asList(create(taskId));
if (torrent && url.startsWith("http")) Download.create(url, taskId.getSaveFile()).get();
if (!torrent) waitDone(taskId);
try {
return XLTaskHelper.get().getTorrentInfo(taskId.getSaveFile()).getMedias().stream().map(this::create).collect(Collectors.toList());
} finally {
XLTaskHelper.get().stopTask(taskId);
}
}
private void waitDone(GetTaskId taskId) {
for (int i = 0; i < 100; i++) {
if (XLTaskHelper.get().getTaskInfo(taskId).getTaskStatus() == 2) return;
SystemClock.sleep(100);
}
}
}
}

@ -35,7 +35,6 @@ public class Youtube implements Source.Extractor {
private static final String MPD = "<MPD xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:mpeg:dash:schema:mpd:2011' xsi:schemaLocation='urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd' type='static' mediaPresentationDuration='PT%sS' minBufferTime='PT1.500S' profiles='urn:mpeg:dash:profile:isoff-on-demand:2011'>\n" + "<Period duration='PT%sS' start='PT0S'>\n" + "%s\n" + "%s\n" + "</Period>\n" + "</MPD>";
private static final String ADAPT = "<AdaptationSet lang='chi'>\n" + "<ContentComponent contentType='%s'/>\n" + "<Representation id='%d' bandwidth='%d' codecs='%s' mimeType='%s' %s>\n" + "<BaseURL>%s</BaseURL>\n" + "<SegmentBase indexRange='%s'>\n" + "<Initialization range='%s'/>\n" + "</SegmentBase>\n" + "</Representation>\n" + "</AdaptationSet>";
private static final Pattern PATTERN_LIST = Pattern.compile("(youtube\\.com|youtu\\.be).*list=");
public Youtube() {
NewPipe.init(NewPipeImpl.get(), Localization.fromLocale(Locale.getDefault()));
@ -133,46 +132,41 @@ public class Youtube implements Source.Extractor {
public void exit() {
}
public static class Parser implements Callable<List<Episode>> {
public record Parser(String url) implements Callable<List<Episode>> {
private YoutubePlaylistExtractor extractor;
private final String url;
private static final Pattern PATTERN = Pattern.compile("(youtube\\.com|youtu\\.be).*list=");
public static boolean match(String url) {
return PATTERN_LIST.matcher(url).find();
return PATTERN.matcher(url).find();
}
public static Parser get(String url) {
return new Parser(url);
}
public Parser(String url) {
this.url = url;
}
@Override
public List<Episode> call() {
try {
ListLinkHandler handler = YoutubePlaylistLinkHandlerFactory.getInstance().fromUrl(url);
extractor = new YoutubePlaylistExtractor(ServiceList.YouTube, handler);
YoutubePlaylistExtractor extractor = new YoutubePlaylistExtractor(ServiceList.YouTube, handler);
extractor.forceLocalization(NewPipe.getPreferredLocalization());
extractor.fetchPage();
List<Episode> episodes = new ArrayList<>();
add(episodes, extractor.getInitialPage());
add(extractor, episodes, extractor.getInitialPage());
return episodes;
} catch (Exception e) {
return Collections.emptyList();
}
}
private void add(List<Episode> episodes, ListExtractor.InfoItemsPage<StreamInfoItem> page) {
private void add(YoutubePlaylistExtractor extractor, List<Episode> episodes, ListExtractor.InfoItemsPage<StreamInfoItem> page) {
for (StreamInfoItem item : page.getItems()) {
if (item.getDuration() == -1) continue;
episodes.add(Episode.create(item.getName(), item.getUrl()));
}
if (page.hasNextPage()) {
try {
add(episodes, extractor.getPage(page.getNextPage()));
add(extractor, episodes, extractor.getPage(page.getNextPage()));
} catch (Exception e) {
e.printStackTrace();
}

Loading…
Cancel
Save