Skip to content

Commit

Permalink
- 一些优化
Browse files Browse the repository at this point in the history
  • Loading branch information
okjackcaptain committed Oct 3, 2022
1 parent 675e81f commit 1469c78
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 44 deletions.
6 changes: 4 additions & 2 deletions app/src/main/java/com/github/tvbox/osc/api/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class ApiConfig {

private JarLoader jarLoader = new JarLoader();

private String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36";
private String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";

private ApiConfig() {
sourceBeanList = new LinkedHashMap<>();
Expand Down Expand Up @@ -180,7 +180,9 @@ public void loadJar(boolean useCache, String spider, LoadConfigCallback callback
}
}

OkGo.<File>get(jarUrl).execute(new AbsCallback<File>() {
OkGo.<File>get(jarUrl)
.headers("User-Agent", userAgent)
.execute(new AbsCallback<File>() {

@Override
public File convertResponse(okhttp3.Response response) throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -731,12 +732,13 @@ private void playNext(boolean isProgress) {
hasNext = mVodInfo.playIndex + 1 < mVodInfo.seriesMap.get(mVodInfo.playFlag).size();
}
if (!hasNext) {
if(mVodInfo!=null && isProgress){
if(isProgress && mVodInfo!=null){
mVodInfo.playIndex=0;
Toast.makeText(this, "已经是最后一集了!,即将跳到第一集继续播放", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "已经是最后一集了!", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "已经是最后一集了!", Toast.LENGTH_SHORT).show();
return;
}else {
mVodInfo.playIndex++;
}
Expand All @@ -761,6 +763,10 @@ private void playPrevious() {
private int autoRetryCount = 0;

boolean autoRetry() {
if (loadFoundVideoUrls != null && loadFoundVideoUrls.size() > 0) {
autoRetryFromLoadFoundVideoUrls();
return true;
}
if (autoRetryCount < 2) {
play(false);
autoRetryCount++;
Expand All @@ -771,6 +777,18 @@ boolean autoRetry() {
}
}

void autoRetryFromLoadFoundVideoUrls() {
String videoUrl = loadFoundVideoUrls.poll();
HashMap<String,String> header = loadFoundVideoUrlsHeader.get(videoUrl);
playUrl(videoUrl, header);
}

void initParseLoadFound() {
loadFoundCount.set(0);
loadFoundVideoUrls = new LinkedList<String>();
loadFoundVideoUrlsHeader = new HashMap<String, HashMap<String, String>>();
}

public void play(boolean reset) {
VodInfo.VodSeries vs = mVodInfo.seriesMap.get(mVodInfo.playFlag).get(mVodInfo.playIndex);
EventBus.getDefault().post(new RefreshEvent(RefreshEvent.TYPE_REFRESH, mVodInfo.playIndex));
Expand All @@ -779,7 +797,7 @@ public void play(boolean reset) {
mController.setTitle(playTitleInfo);

stopParse();
loadFoundCount.set(0);
initParseLoadFound();
if(mVideoView!=null) mVideoView.release();
String subtitleCacheKey = mVodInfo.sourceKey + "-" + mVodInfo.id + "-" + mVodInfo.playFlag + "-" + mVodInfo.playIndex+ "-" + vs.name + "-subt";
String progressKey = mVodInfo.sourceKey + mVodInfo.id + mVodInfo.playFlag + mVodInfo.playIndex + vs.name;
Expand Down Expand Up @@ -899,7 +917,7 @@ void stopParse() {

private void doParse(ParseBean pb) {
stopParse();
loadFoundCount.set(0);
initParseLoadFound();
if (pb.getType() == 0) {
setTip("正在嗅探播放地址", true, false);
mHandler.removeMessages(100);
Expand Down Expand Up @@ -1097,6 +1115,8 @@ public void run() {
private WebView mSysWebView;
private SysWebClient mSysWebClient;
private Map<String, Boolean> loadedUrls = new HashMap<>();
private LinkedList<String> loadFoundVideoUrls = new LinkedList<>();
private HashMap<String, HashMap<String, String>> loadFoundVideoUrlsHeader = new HashMap<>();
private AtomicInteger loadFoundCount = new AtomicInteger(0);

void loadWebView(String url) {
Expand Down Expand Up @@ -1357,8 +1377,11 @@ WebResourceResponse checkIsVideo(String url, HashMap<String, String> headers) {

if (!ad) {
if (checkVideoFormat(url)) {
LOG.i("checkVideoFormat:" + url );
loadFoundVideoUrls.add(url);
loadFoundVideoUrlsHeader.put(url, headers);
LOG.i("loadFoundVideoUrl:" + url );
if (loadFoundCount.incrementAndGet() == 1) {
url = loadFoundVideoUrls.poll();
mHandler.removeMessages(100);
if (headers != null && !headers.isEmpty()) {
playUrl(url, headers);
Expand Down Expand Up @@ -1395,13 +1418,15 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque
HashMap<String, String> webHeaders = new HashMap<>();
try {
Map<String, String> hds = request.getRequestHeaders();
if (!hds.isEmpty() && hds.size() > 0) {
for (String k : hds.keySet()) {
for (String k : hds.keySet()) {
if (k.equalsIgnoreCase("user-agent")
|| k.equalsIgnoreCase("referer")
|| k.equalsIgnoreCase("origin")) {
webHeaders.put(k, hds.get(k));
}
}
} catch (Throwable th) {
th.printStackTrace();

}
WebResourceResponse response = checkIsVideo(url, webHeaders);
return response;
Expand Down Expand Up @@ -1526,20 +1551,25 @@ public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalk
}
if (!ad ) {
if (checkVideoFormat(url)) {
LOG.i("checkVideoFormat:" + url );
if (loadFoundCount.incrementAndGet() == 1) {
mHandler.removeMessages(100);
HashMap<String, String> webHeaders = new HashMap<>();
try {
Map<String, String> hds = request.getRequestHeaders();
if (!hds.isEmpty() && hds.size() > 0) {
for (String k : hds.keySet()) {
webHeaders.put(k, hds.get(k));
}
HashMap<String, String> webHeaders = new HashMap<>();
try {
Map<String, String> hds = request.getRequestHeaders();
for (String k : hds.keySet()) {
if (k.equalsIgnoreCase("user-agent")
|| k.equalsIgnoreCase("referer")
|| k.equalsIgnoreCase("origin")) {
webHeaders.put(k, hds.get(k));
}
} catch (Throwable th) {
th.printStackTrace();
}
} catch (Throwable th) {

}
loadFoundVideoUrls.add(url);
loadFoundVideoUrlsHeader.put(url, webHeaders);
LOG.i("loadFoundVideoUrl:" + url );
if (loadFoundCount.incrementAndGet() == 1) {
mHandler.removeMessages(100);
url = loadFoundVideoUrls.poll();
if (webHeaders != null && !webHeaders.isEmpty()) {
playUrl(url, webHeaders);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import com.github.tvbox.osc.server.ControlManager;
import com.github.tvbox.osc.ui.tv.QRCodeGen;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import me.jessyan.autosize.utils.AutoSizeUtils;

public class PushActivity extends BaseActivity {
Expand Down Expand Up @@ -42,8 +45,13 @@ public void onClick(View v) {
if (manager != null) {
if (manager.hasPrimaryClip() && manager.getPrimaryClip() != null && manager.getPrimaryClip().getItemCount() > 0) {
ClipData.Item addedText = manager.getPrimaryClip().getItemAt(0);
String clipText = addedText.getText().toString().trim();
Matcher m = Pattern.compile("(https?://[A-Za-z0-9:_@$#\\/\\.\\?\\=\\&\\%\\-]+)").matcher(clipText);
if (m.find()) {
clipText = m.group(1);
}
Intent newIntent = new Intent(mContext, DetailActivity.class);
newIntent.putExtra("id", addedText.getText().toString().trim());
newIntent.putExtra("id", clipText);
newIntent.putExtra("sourceKey", "push_agent");
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PushActivity.this.startActivity(newIntent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -779,6 +780,10 @@ private void playPrevious() {
private int autoRetryCount = 0;

boolean autoRetry() {
if (loadFoundVideoUrls != null && loadFoundVideoUrls.size() > 0) {
autoRetryFromLoadFoundVideoUrls();
return true;
}
if (autoRetryCount < 2) {
play(false);
autoRetryCount++;
Expand All @@ -789,6 +794,18 @@ boolean autoRetry() {
}
}

void autoRetryFromLoadFoundVideoUrls() {
String videoUrl = loadFoundVideoUrls.poll();
HashMap<String,String> header = loadFoundVideoUrlsHeader.get(videoUrl);
playUrl(videoUrl, header);
}

void initParseLoadFound() {
loadFoundCount.set(0);
loadFoundVideoUrls = new LinkedList<String>();
loadFoundVideoUrlsHeader = new HashMap<String, HashMap<String, String>>();
}

public void play(boolean reset) {
if(mVodInfo==null)return;
VodInfo.VodSeries vs = mVodInfo.seriesMap.get(mVodInfo.playFlag).get(mVodInfo.playIndex);
Expand All @@ -798,7 +815,7 @@ public void play(boolean reset) {
mController.setTitle(playTitleInfo);

stopParse();
loadFoundCount.set(0);
initParseLoadFound();
if(mVideoView!=null) mVideoView.release();
String subtitleCacheKey = mVodInfo.sourceKey + "-" + mVodInfo.id + "-" + mVodInfo.playFlag + "-" + mVodInfo.playIndex+ "-" + vs.name + "-subt";
String progressKey = mVodInfo.sourceKey + mVodInfo.id + mVodInfo.playFlag + mVodInfo.playIndex + vs.name;
Expand Down Expand Up @@ -919,7 +936,7 @@ void stopParse() {

private void doParse(ParseBean pb) {
stopParse();
loadFoundCount.set(0);
initParseLoadFound();
if (pb.getType() == 0) {
setTip("正在嗅探播放地址", true, false);
mHandler.removeMessages(100);
Expand Down Expand Up @@ -1117,6 +1134,8 @@ public void run() {
private WebView mSysWebView;
private SysWebClient mSysWebClient;
private Map<String, Boolean> loadedUrls = new HashMap<>();
private LinkedList<String> loadFoundVideoUrls = new LinkedList<>();
private HashMap<String, HashMap<String, String>> loadFoundVideoUrlsHeader = new HashMap<>();
private AtomicInteger loadFoundCount = new AtomicInteger(0);

void loadWebView(String url) {
Expand Down Expand Up @@ -1382,8 +1401,11 @@ WebResourceResponse checkIsVideo(String url, HashMap<String, String> headers) {

if (!ad) {
if (checkVideoFormat(url)) {
LOG.i("checkVideoFormat:" + url );
loadFoundVideoUrls.add(url);
loadFoundVideoUrlsHeader.put(url, headers);
LOG.i("loadFoundVideoUrl:" + url );
if (loadFoundCount.incrementAndGet() == 1) {
url = loadFoundVideoUrls.poll();
mHandler.removeMessages(100);
if (headers != null && !headers.isEmpty()) {
playUrl(url, headers);
Expand Down Expand Up @@ -1420,13 +1442,15 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque
HashMap<String, String> webHeaders = new HashMap<>();
try {
Map<String, String> hds = request.getRequestHeaders();
if (!hds.isEmpty() && hds.size() > 0) {
for (String k : hds.keySet()) {
for (String k : hds.keySet()) {
if (k.equalsIgnoreCase("user-agent")
|| k.equalsIgnoreCase("referer")
|| k.equalsIgnoreCase("origin")) {
webHeaders.put(k, hds.get(k));
}
}
} catch (Throwable th) {
th.printStackTrace();

}
WebResourceResponse response = checkIsVideo(url, webHeaders);
return response;
Expand Down Expand Up @@ -1551,20 +1575,25 @@ public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalk
}
if (!ad ) {
if (checkVideoFormat(url)) {
LOG.i("checkVideoFormat:" + url );
if (loadFoundCount.incrementAndGet() == 1) {
mHandler.removeMessages(100);
HashMap<String, String> webHeaders = new HashMap<>();
try {
Map<String, String> hds = request.getRequestHeaders();
if (!hds.isEmpty() && hds.size() > 0) {
for (String k : hds.keySet()) {
webHeaders.put(k, hds.get(k));
}
HashMap<String, String> webHeaders = new HashMap<>();
try {
Map<String, String> hds = request.getRequestHeaders();
for (String k : hds.keySet()) {
if (k.equalsIgnoreCase("user-agent")
|| k.equalsIgnoreCase("referer")
|| k.equalsIgnoreCase("origin")) {
webHeaders.put(k, hds.get(k));
}
} catch (Throwable th) {
th.printStackTrace();
}
} catch (Throwable th) {

}
loadFoundVideoUrls.add(url);
loadFoundVideoUrlsHeader.put(url, webHeaders);
LOG.i("loadFoundVideoUrl:" + url );
if (loadFoundCount.incrementAndGet() == 1) {
mHandler.removeMessages(100);
url = loadFoundVideoUrls.poll();
if (webHeaders != null && !webHeaders.isEmpty()) {
playUrl(url, webHeaders);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public static String getFilePrefixName(String fileName) {

//增加对flv|avi|mkv|rm|wmv|mpg等几种视频格式的支持
//private static final Pattern snifferMatch = Pattern.compile("http((?!http).){26,}?\\.(m3u8|mp4)\\?.*|http((?!http).){26,}\\.(m3u8|mp4)|http((?!http).){26,}?/m3u8\\?pt=m3u8.*|http((?!http).)*?default\\.ixigua\\.com/.*|http((?!http).)*?cdn-tos[^\\?]*|http((?!http).)*?/obj/tos[^\\?]*|http.*?/player/m3u8play\\.php\\?url=.*|http.*?/player/.*?[pP]lay\\.php\\?url=.*|http.*?/playlist/m3u8/\\?vid=.*|http.*?\\.php\\?type=m3u8&.*|http.*?/download.aspx\\?.*|http.*?/api/up_api.php\\?.*|https.*?\\.66yk\\.cn.*|http((?!http).)*?netease\\.com/file/.*");
private static final Pattern snifferMatch = Pattern.compile("http((?!http).)*?default\\.365yg\\.com/.*|http((?!http).){26,}?\\.(m3u8|mp4|flv|avi|mkv|rm|wmv|mpg)\\?.*|http((?!http).){26,}\\.(m3u8|mp4|flv|avi|mkv|rm|wmv|mpg)|http((?!http).){20,}?/m3u8\\?pt=m3u8.*|http((?!http).)*?default\\.ixigua\\.com/.*|http((?!http).)*?dycdn-tos\\.pstatp[^\\?]*|http.*?/play.{0,3}\\?[^url]{2,8}=.*|http.*?/player/m3u8play\\.php\\?url=.*|http.*?/player/.*?[pP]lay\\.php\\?url=.*|http.*?/playlist/m3u8/\\?vid=.*|http.*?\\.php\\?type=m3u8&.*|http.*?/download.aspx\\?.*|http.*?/api/up_api.php\\?.*|https.*?\\.66yk\\.cn.*|http((?!http).)*?netease\\.com/file/.*");
private static final Pattern snifferMatch = Pattern.compile("http((?!http).)*?default\\.365yg\\.com/.*|http((?!http).){20,}?\\.(m3u8|mp4|flv|avi|mkv|rm|wmv|mpg)\\?.*|http((?!http).){20,}\\.(m3u8|mp4|flv|avi|mkv|rm|wmv|mpg)|http((?!http).){20,}?/m3u8\\?pt=m3u8.*|http((?!http).)*?default\\.ixigua\\.com/.*|http((?!http).)*?dycdn-tos\\.pstatp[^\\?]*|http.*?/play.{0,3}\\?[^url]{2,8}=.*|http.*?/player/m3u8play\\.php\\?url=.*|http.*?/player/.*?[pP]lay\\.php\\?url=.*|http.*?/playlist/m3u8/\\?vid=.*|http.*?\\.php\\?type=m3u8&.*|http.*?/download.aspx\\?.*|http.*?/api/up_api.php\\?.*|https.*?\\.66yk\\.cn.*|http((?!http).)*?netease\\.com/file/.*");
public static boolean isVideoFormat(String url) {
if (url.contains("=http") || url.contains(".html")) {
if (url.contains("=http") || url.contains("?http") || url.contains(".html")) {
return false;
}
if (snifferMatch.matcher(url).find()) {
Expand Down

0 comments on commit 1469c78

Please sign in to comment.