-Refactored subtitle storing to reduce redundancy.

This commit is contained in:
John Zhen Mo 2018-02-01 23:24:22 -08:00
parent c1cfb2b223
commit 502fd53cc4
1 changed files with 28 additions and 13 deletions

View File

@ -85,8 +85,9 @@ public class YoutubeStreamExtractor extends StreamExtractor {
private JsonObject playerArgs; private JsonObject playerArgs;
@Nonnull @Nonnull
private final Map<String, String> videoInfoPage = new HashMap<>(); private final Map<String, String> videoInfoPage = new HashMap<>();
@Nonnull @Nonnull
private List<Subtitles> availableSubtitles = new ArrayList<>(); private List<SubtitlesInfo> subtitlesInfos = new ArrayList<>();
private boolean isAgeRestricted; private boolean isAgeRestricted;
@ -432,11 +433,11 @@ public class YoutubeStreamExtractor extends StreamExtractor {
@Override @Override
@Nonnull @Nonnull
public List<Subtitles> getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException { public List<Subtitles> getSubtitles(final SubtitlesFormat format) throws IOException, ExtractionException {
assertPageFetched(); assertPageFetched();
List<Subtitles> subtitles = new ArrayList<>(); List<Subtitles> subtitles = new ArrayList<>();
for (final Subtitles subtitle : availableSubtitles) { for (final SubtitlesInfo subtitlesInfo : subtitlesInfos) {
if (subtitle.getFileType() == format) subtitles.add(subtitle); subtitles.add(subtitlesInfo.getSubtitle(format));
} }
return subtitles; return subtitles;
} }
@ -553,8 +554,8 @@ public class YoutubeStreamExtractor extends StreamExtractor {
decryptionCode = loadDecryptionCode(playerUrl); decryptionCode = loadDecryptionCode(playerUrl);
} }
if (availableSubtitles.isEmpty()) { if (subtitlesInfos.isEmpty()) {
availableSubtitles.addAll(getAvailableSubtitles()); subtitlesInfos.addAll(getAvailableSubtitlesInfo());
} }
} }
@ -709,7 +710,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
} }
@Nonnull @Nonnull
private List<Subtitles> getAvailableSubtitles() throws SubtitlesException { private List<SubtitlesInfo> getAvailableSubtitlesInfo() throws SubtitlesException {
// If the video is age restricted getPlayerConfig will fail // If the video is age restricted getPlayerConfig will fail
if(isAgeRestricted) return Collections.emptyList(); if(isAgeRestricted) return Collections.emptyList();
@ -744,17 +745,13 @@ public class YoutubeStreamExtractor extends StreamExtractor {
if(captionsSize == 0) return Collections.emptyList(); if(captionsSize == 0) return Collections.emptyList();
// Obtain the base url, this only needs to be done once // Obtain the base url, this only needs to be done once
List<Subtitles> result = new ArrayList<>(); List<SubtitlesInfo> result = new ArrayList<>();
for (int i = 0; i < captionsSize; i++) { for (int i = 0; i < captionsSize; i++) {
final String languageCode = captionsArray.getObject(i).getString("languageCode"); final String languageCode = captionsArray.getObject(i).getString("languageCode");
final String baseUrl = captionsArray.getObject(i).getString("baseUrl"); final String baseUrl = captionsArray.getObject(i).getString("baseUrl");
final boolean isAutoGenerated = captionsArray.getObject(i).getString("vssId").startsWith("a."); final boolean isAutoGenerated = captionsArray.getObject(i).getString("vssId").startsWith("a.");
result.add(new Subtitles(SubtitlesFormat.TTML, languageCode, result.add(new SubtitlesInfo(baseUrl, languageCode, isAutoGenerated));
getSubtitleFormatUrl(baseUrl, SubtitlesFormat.TTML), isAutoGenerated));
result.add(new Subtitles(SubtitlesFormat.VTT, languageCode,
getSubtitleFormatUrl(baseUrl, SubtitlesFormat.VTT), isAutoGenerated));
// todo: add transcripts, they are currently omitted since they are incompatible with ExoPlayer
} }
return result; return result;
@ -773,6 +770,24 @@ public class YoutubeStreamExtractor extends StreamExtractor {
} }
} }
private class SubtitlesInfo {
final String cleanUrl;
final String languageCode;
final boolean isGenerated;
public SubtitlesInfo(final String baseUrl, final String languageCode, final boolean isGenerated) {
this.cleanUrl = baseUrl
.replaceAll("&fmt=[^&]*", "") // Remove preexisting format if exists
.replaceAll("&tlang=[^&]*", ""); // Remove translation language
this.languageCode = languageCode;
this.isGenerated = isGenerated;
}
public Subtitles getSubtitle(final SubtitlesFormat format) {
return new Subtitles(format, languageCode, cleanUrl + "&fmt=" + format.getExtension(), isGenerated);
}
}
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Utils // Utils
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/