mirror of
https://github.com/dirtydishes/dreamio.git
synced 2026-06-06 13:37:24 +00:00
filter false opensubtitles subtitle candidates
This commit is contained in:
parent
6008272d0a
commit
11ed364094
6 changed files with 465 additions and 9 deletions
|
|
@ -84,6 +84,11 @@ final class DreamioWebViewController: UIViewController {
|
|||
const postedSubtitleURLs = new Set();
|
||||
const subtitleURLPattern = /https?:\/\/[^\s"'<>]+(?:\.srt|\.vtt|\.ass|\.ssa|\.sub|opensubtitles|subtitle)[^\s"'<>]*/ig;
|
||||
const subtitleSignalPattern = /subtitle|subtitles|opensubtitles|vtt|srt|ass|ssa/i;
|
||||
const subtitleExtensions = new Set(["srt", "vtt", "ass", "ssa", "sub"]);
|
||||
const nonSubtitleExtensions = new Set([
|
||||
"aac", "avi", "bmp", "css", "gif", "heic", "ico", "jpeg", "jpg", "js", "json",
|
||||
"m4a", "m4v", "mkv", "mov", "mp3", "mp4", "mpeg", "mpg", "png", "svg", "ts", "webm", "webp"
|
||||
]);
|
||||
const subtitleObjectKeys = [
|
||||
"attributes",
|
||||
"files",
|
||||
|
|
@ -125,14 +130,51 @@ final class DreamioWebViewController: UIViewController {
|
|||
}
|
||||
};
|
||||
|
||||
const isDirectSubtitleFileURL = (url) => {
|
||||
try {
|
||||
const parsed = new URL(url, window.location.href);
|
||||
const extension = parsed.pathname.split(".").pop().toLowerCase();
|
||||
return subtitleExtensions.has(extension)
|
||||
|| Array.from(subtitleExtensions).some((ext) => parsed.href.toLowerCase().includes(`.${ext}?`) || parsed.href.toLowerCase().includes(`.${ext}&`));
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const isProbablyNonSubtitleAssetURL = (url) => {
|
||||
try {
|
||||
const extension = new URL(url, window.location.href).pathname.split(".").pop().toLowerCase();
|
||||
return nonSubtitleExtensions.has(extension);
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const isOpenSubtitlesDownloadURL = (url) => {
|
||||
try {
|
||||
const parsed = new URL(url, window.location.href);
|
||||
const host = parsed.hostname.toLowerCase();
|
||||
const path = parsed.pathname.toLowerCase();
|
||||
if (!host.includes("opensubtitles")) {
|
||||
return false;
|
||||
}
|
||||
if (/\/manifest\.json(?:_\d+)?$/i.test(path)) {
|
||||
return false;
|
||||
}
|
||||
return /\/api\/v1\/download(?:\/|$)/i.test(path)
|
||||
|| /\/download(?:\/|$)/i.test(path)
|
||||
|| /\/subtitles?(?:\/|$)/i.test(path);
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const isSubtitleURL = (url) => {
|
||||
if (!url || isOpenSubtitlesManifestID(url)) {
|
||||
return false;
|
||||
}
|
||||
subtitleURLPattern.lastIndex = 0;
|
||||
const matches = subtitleURLPattern.test(url) || /api\.opensubtitles\.com\/api\/v1\/download/i.test(url);
|
||||
subtitleURLPattern.lastIndex = 0;
|
||||
return matches;
|
||||
return !isProbablyNonSubtitleAssetURL(url)
|
||||
&& (isDirectSubtitleFileURL(url) || isOpenSubtitlesDownloadURL(url));
|
||||
};
|
||||
|
||||
const findResolverURL = () => {
|
||||
|
|
|
|||
|
|
@ -132,6 +132,10 @@ struct StreamCandidate {
|
|||
|
||||
enum SubtitleCandidateParser {
|
||||
private static let supportedExtensions = ["srt", "vtt", "ass", "ssa", "sub"]
|
||||
private static let nonSubtitleExtensions = [
|
||||
"aac", "avi", "bmp", "css", "gif", "heic", "ico", "jpeg", "jpg", "js", "json",
|
||||
"m4a", "m4v", "mkv", "mov", "mp3", "mp4", "mpeg", "mpg", "png", "svg", "ts", "webm", "webp"
|
||||
]
|
||||
private static let urlFields = ["url", "href", "src", "link", "subtitles", "subtitle", "subtitleUrl", "subtitleURL", "file", "download", "fileUrl", "fileURL"]
|
||||
private static let labelFields = ["label", "name", "title", "file_name", "filename", "lang", "language", "id"]
|
||||
private struct CandidateContext {
|
||||
|
|
@ -244,14 +248,14 @@ enum SubtitleCandidateParser {
|
|||
return nil
|
||||
}
|
||||
|
||||
let lowercased = url.absoluteString.lowercased()
|
||||
if isOpenSubtitlesManifestIdentifier(url) {
|
||||
return nil
|
||||
}
|
||||
guard supportedExtensions.contains(url.pathExtension.lowercased())
|
||||
|| supportedExtensions.contains(where: { lowercased.contains(".\($0)?") || lowercased.contains(".\($0)&") })
|
||||
|| lowercased.contains("subtitle")
|
||||
|| lowercased.contains("opensubtitles")
|
||||
guard !nonSubtitleExtensions.contains(url.pathExtension.lowercased()) else {
|
||||
return nil
|
||||
}
|
||||
guard isDirectSubtitleFile(url)
|
||||
|| isOpenSubtitlesDownloadURL(url)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -259,6 +263,25 @@ enum SubtitleCandidateParser {
|
|||
return url
|
||||
}
|
||||
|
||||
private static func isDirectSubtitleFile(_ url: URL) -> Bool {
|
||||
let lowercased = url.absoluteString.lowercased()
|
||||
return supportedExtensions.contains(url.pathExtension.lowercased())
|
||||
|| supportedExtensions.contains(where: { lowercased.contains(".\($0)?") || lowercased.contains(".\($0)&") })
|
||||
}
|
||||
|
||||
private static func isOpenSubtitlesDownloadURL(_ url: URL) -> Bool {
|
||||
guard url.host?.localizedCaseInsensitiveContains("opensubtitles") == true else {
|
||||
return false
|
||||
}
|
||||
let path = url.path.lowercased()
|
||||
guard !isOpenSubtitlesManifestIdentifier(url) else {
|
||||
return false
|
||||
}
|
||||
return path.range(of: #"(^|/)api/v1/download(/|$)"#, options: .regularExpression) != nil
|
||||
|| path.range(of: #"(^|/)download(/|$)"#, options: .regularExpression) != nil
|
||||
|| path.range(of: #"(^|/)subtitles?(/|$)"#, options: .regularExpression) != nil
|
||||
}
|
||||
|
||||
private static func isOpenSubtitlesManifestIdentifier(_ url: URL) -> Bool {
|
||||
guard url.host?.localizedCaseInsensitiveContains("opensubtitles") == true else {
|
||||
return false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue