|
|
|
|
@ -25,7 +25,6 @@ |
|
|
|
|
|
|
|
|
|
package com.github.tvbox.osc.subtitle; |
|
|
|
|
|
|
|
|
|
import android.net.Uri; |
|
|
|
|
import android.text.TextUtils; |
|
|
|
|
import android.util.Log; |
|
|
|
|
|
|
|
|
|
@ -35,16 +34,19 @@ import com.github.tvbox.osc.subtitle.format.FormatSRT; |
|
|
|
|
import com.github.tvbox.osc.subtitle.format.FormatSTL; |
|
|
|
|
import com.github.tvbox.osc.subtitle.model.TimedTextObject; |
|
|
|
|
import com.github.tvbox.osc.subtitle.runtime.AppTaskExecutor; |
|
|
|
|
import com.github.tvbox.osc.util.FileUtils; |
|
|
|
|
import com.github.tvbox.osc.util.UnicodeReader; |
|
|
|
|
import com.lzy.okgo.OkGo; |
|
|
|
|
|
|
|
|
|
import org.apache.commons.io.input.ReaderInputStream; |
|
|
|
|
import org.mozilla.universalchardet.UniversalDetector; |
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.InputStream; |
|
|
|
|
import java.io.Reader; |
|
|
|
|
import java.nio.charset.Charset; |
|
|
|
|
|
|
|
|
|
import okhttp3.Response; |
|
|
|
|
|
|
|
|
|
@ -77,12 +79,12 @@ public class SubtitleLoader { |
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
try { |
|
|
|
|
final TimedTextObject timedTextObject = loadFromRemote(remoteSubtitlePath); |
|
|
|
|
final SubtitleLoadSuccessResult subtitleLoadSuccessResult = loadFromRemote(remoteSubtitlePath); |
|
|
|
|
if (callback != null) { |
|
|
|
|
AppTaskExecutor.mainThread().execute(new Runnable() { |
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
callback.onSuccess(timedTextObject); |
|
|
|
|
callback.onSuccess(subtitleLoadSuccessResult); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
@ -109,12 +111,12 @@ public class SubtitleLoader { |
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
try { |
|
|
|
|
final TimedTextObject timedTextObject = loadFromLocal(localSubtitlePath); |
|
|
|
|
final SubtitleLoadSuccessResult subtitleLoadSuccessResult = loadFromLocal(localSubtitlePath); |
|
|
|
|
if (callback != null) { |
|
|
|
|
AppTaskExecutor.mainThread().execute(new Runnable() { |
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
callback.onSuccess(timedTextObject); |
|
|
|
|
callback.onSuccess(subtitleLoadSuccessResult); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
@ -135,7 +137,7 @@ public class SubtitleLoader { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public TimedTextObject loadSubtitle(String path) { |
|
|
|
|
public SubtitleLoadSuccessResult loadSubtitle(String path) { |
|
|
|
|
if (TextUtils.isEmpty(path)) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
@ -152,47 +154,75 @@ public class SubtitleLoader { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static TimedTextObject loadFromRemote(final String remoteSubtitlePath) |
|
|
|
|
private static SubtitleLoadSuccessResult loadFromRemote(final String remoteSubtitlePath) |
|
|
|
|
throws IOException, FatalParsingException { |
|
|
|
|
Log.d(TAG, "parseRemote: remoteSubtitlePath = " + remoteSubtitlePath); |
|
|
|
|
String referer = ""; |
|
|
|
|
String from = ""; |
|
|
|
|
if (remoteSubtitlePath.contains("alicloud")) { |
|
|
|
|
referer = "https://www.aliyundrive.com/"; |
|
|
|
|
from = "aliyundrive"; |
|
|
|
|
} else if (remoteSubtitlePath.contains("assrt.net")) { |
|
|
|
|
referer = "https://secure.assrt.net/"; |
|
|
|
|
from = "assrt"; |
|
|
|
|
} |
|
|
|
|
String ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36"; |
|
|
|
|
Response response = OkGo.<String>get(remoteSubtitlePath) |
|
|
|
|
.headers("Referer", referer) |
|
|
|
|
.headers("User-Agent", ua) |
|
|
|
|
.execute(); |
|
|
|
|
String content = response.body().string(); |
|
|
|
|
byte[] bytes = response.body().bytes(); |
|
|
|
|
try { |
|
|
|
|
Uri uri = Uri.parse(remoteSubtitlePath); |
|
|
|
|
InputStream subtitle = new ByteArrayInputStream(content.getBytes()); |
|
|
|
|
UniversalDetector detector = new UniversalDetector(null); |
|
|
|
|
detector.handleData(bytes, 0, bytes.length); |
|
|
|
|
detector.dataEnd(); |
|
|
|
|
String encoding = detector.getDetectedCharset(); |
|
|
|
|
String content = new String(bytes, encoding); |
|
|
|
|
InputStream is = new ByteArrayInputStream(content.getBytes()); |
|
|
|
|
String filename = ""; |
|
|
|
|
if (from == "aliyundrive") { |
|
|
|
|
filename = uri.getQueryParameter("response-content-disposition"); |
|
|
|
|
filename = "zimu." + filename.substring(filename.lastIndexOf(".")+1); |
|
|
|
|
} else { |
|
|
|
|
filename = uri.getPath(); |
|
|
|
|
filename = "zimu." + filename.substring(filename.lastIndexOf(".")+1); |
|
|
|
|
String contentDispostion = response.header("content-disposition", ""); |
|
|
|
|
String[] cd = contentDispostion.split(";"); |
|
|
|
|
if (cd.length > 1) { |
|
|
|
|
String filenameInfo = cd[1]; |
|
|
|
|
filenameInfo = filenameInfo.trim(); |
|
|
|
|
if (filenameInfo.startsWith("filename=")) { |
|
|
|
|
filename = filenameInfo.replace("filename=", ""); |
|
|
|
|
filename = filename.replace("\"", ""); |
|
|
|
|
} else if (filenameInfo.startsWith("filename*=")) { |
|
|
|
|
filename = filenameInfo.substring(filenameInfo.lastIndexOf("''")+2); |
|
|
|
|
} |
|
|
|
|
filename = filename.trim(); |
|
|
|
|
} |
|
|
|
|
return loadAndParse(subtitle, filename); |
|
|
|
|
SubtitleLoadSuccessResult subtitleLoadSuccessResult = new SubtitleLoadSuccessResult(); |
|
|
|
|
subtitleLoadSuccessResult.timedTextObject = loadAndParse(is, filename); |
|
|
|
|
subtitleLoadSuccessResult.fileName = filename; |
|
|
|
|
subtitleLoadSuccessResult.content = content; |
|
|
|
|
subtitleLoadSuccessResult.subtitlePath = remoteSubtitlePath; |
|
|
|
|
return subtitleLoadSuccessResult; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static TimedTextObject loadFromLocal(final String localSubtitlePath) |
|
|
|
|
private static SubtitleLoadSuccessResult loadFromLocal(final String localSubtitlePath) |
|
|
|
|
throws IOException, FatalParsingException { |
|
|
|
|
Log.d(TAG, "parseLocal: localSubtitlePath = " + localSubtitlePath); |
|
|
|
|
File file = new File(localSubtitlePath); |
|
|
|
|
return loadAndParse(new FileInputStream(file), file.getPath()); |
|
|
|
|
if (!file.exists()) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
byte[] bytes = FileUtils.readSimple(file); |
|
|
|
|
UniversalDetector detector = new UniversalDetector(null); |
|
|
|
|
detector.handleData(bytes, 0, bytes.length); |
|
|
|
|
detector.dataEnd(); |
|
|
|
|
String encoding = detector.getDetectedCharset(); |
|
|
|
|
String content = new String(bytes, encoding); |
|
|
|
|
InputStream is = new ByteArrayInputStream(content.getBytes()); |
|
|
|
|
String filePath = file.getPath(); |
|
|
|
|
SubtitleLoadSuccessResult subtitleLoadSuccessResult = new SubtitleLoadSuccessResult(); |
|
|
|
|
subtitleLoadSuccessResult.timedTextObject = loadAndParse(is, filePath); |
|
|
|
|
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1); |
|
|
|
|
subtitleLoadSuccessResult.fileName = fileName; |
|
|
|
|
subtitleLoadSuccessResult.subtitlePath = localSubtitlePath; |
|
|
|
|
return subtitleLoadSuccessResult; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static TimedTextObject loadAndParse(final InputStream is, final String filePath) |
|
|
|
|
@ -201,7 +231,7 @@ public class SubtitleLoader { |
|
|
|
|
String ext = fileName.substring(fileName.lastIndexOf(".")); |
|
|
|
|
Log.d(TAG, "parse: name = " + fileName + ", ext = " + ext); |
|
|
|
|
Reader reader = new UnicodeReader(is); //处理有BOM头的utf8
|
|
|
|
|
InputStream newInputStream = new ReaderInputStream(reader, "UTF-8"); |
|
|
|
|
InputStream newInputStream = new ReaderInputStream(reader, Charset.defaultCharset()); |
|
|
|
|
if (".srt".equalsIgnoreCase(ext)) { |
|
|
|
|
return new FormatSRT().parseFile(fileName, newInputStream); |
|
|
|
|
} else if (".ass".equalsIgnoreCase(ext)) { |
|
|
|
|
@ -215,7 +245,7 @@ public class SubtitleLoader { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public interface Callback { |
|
|
|
|
void onSuccess(TimedTextObject timedTextObject); |
|
|
|
|
void onSuccess(SubtitleLoadSuccessResult SubtitleLoadSuccessResult); |
|
|
|
|
|
|
|
|
|
void onError(Exception exception); |
|
|
|
|
} |
|
|
|
|
|