make Subtitles extends Stream

This commit is contained in:
kapodamy 2018-06-27 12:52:25 -03:00 committed by GitHub
parent d45247bfc5
commit c246c89bc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 105 additions and 26 deletions

View File

@ -1,4 +1,4 @@
package org.schabi.newpipe.extractor;
package org.schabi.newpipe.extractor;//TODO ¡git it!
/*
* Created by Adam Howard on 08/11/15.
@ -36,7 +36,13 @@ public enum MediaFormat {
M4A (0x3, "m4a", "m4a", "audio/mp4"),
WEBMA (0x4, "WebM", "webm", "audio/webm"),
MP3 (0x5, "MP3", "mp3", "audio/mpeg"),
OPUS (0x6, "opus", "opus", "audio/opus");
OPUS (0x6, "opus", "opus", "audio/opus"),
// subtitles formats
VTT (0x7, "WebVTT", "vtt", "text/vtt"),
TTML (0x8, "Timed Text Markup Language", "ttml", "application/ttml+xml"),
TRANSCRIPT1 (0x9, "TranScript v1", "srv1", "text/xml"),
TRANSCRIPT2 (0xA, "TranScript v2", "srv2", "text/xml"),
TRANSCRIPT3 (0xB, "TranScript v3", "srv3", "text/xml");
public final int id;
public final String name;

View File

@ -1,4 +1,4 @@
package org.schabi.newpipe.extractor.services.soundcloud;
package org.schabi.newpipe.extractor.services.soundcloud;// TODO: ¡git it!
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
@ -172,13 +172,13 @@ public class SoundcloudStreamExtractor extends StreamExtractor {
@Override
@Nonnull
public List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException {
public List<SubtitlesStream> getSubtitlesDefault() throws IOException, ExtractionException {
return Collections.emptyList();
}
@Override
@Nonnull
public List<Subtitles> getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException {
public List<SubtitlesStream> getSubtitles(MediaFormat format) throws IOException, ExtractionException {
return Collections.emptyList();
}

View File

@ -1,4 +1,4 @@
package org.schabi.newpipe.extractor.services.youtube.extractors;
package org.schabi.newpipe.extractor.services.youtube.extractors;// TODO: ¡git it!
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
@ -7,6 +7,7 @@ import com.grack.nanojson.JsonParserException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.ScriptableObject;
@ -429,15 +430,15 @@ public class YoutubeStreamExtractor extends StreamExtractor {
@Override
@Nonnull
public List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException {
return getSubtitles(SubtitlesFormat.TTML);
public List<SubtitlesStream> getSubtitlesDefault() throws IOException, ExtractionException {
return getSubtitles(MediaFormat.TTML);
}
@Override
@Nonnull
public List<Subtitles> getSubtitles(final SubtitlesFormat format) throws IOException, ExtractionException {
public List<SubtitlesStream> getSubtitles(final MediaFormat format) throws IOException, ExtractionException {
assertPageFetched();
List<Subtitles> subtitles = new ArrayList<>();
List<SubtitlesStream> subtitles = new ArrayList<>();
for (final SubtitlesInfo subtitlesInfo : subtitlesInfos) {
subtitles.add(subtitlesInfo.getSubtitle(format));
}
@ -463,8 +464,13 @@ public class YoutubeStreamExtractor extends StreamExtractor {
assertPageFetched();
try {
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
collector.commit(extractVideoPreviewInfo(doc.select("div[class=\"watch-sidebar-section\"]")
.first().select("li").first()));
Elements watch = doc.select("div[class=\"watch-sidebar-section\"]");
if (watch.size() < 1) {
return null;// prevent the snackbar notification "report error" on age-restricted videos
}
collector.commit(extractVideoPreviewInfo(watch.first().select("li").first()));
return collector.getItems().get(0);
} catch (Exception e) {
@ -814,7 +820,6 @@ public class YoutubeStreamExtractor extends StreamExtractor {
final String languageCode;
final boolean isGenerated;
final Locale locale;
public SubtitlesInfo(final String baseUrl, final String languageCode, final boolean isGenerated) {
this.cleanUrl = baseUrl
@ -822,13 +827,10 @@ public class YoutubeStreamExtractor extends StreamExtractor {
.replaceAll("&tlang=[^&]*", ""); // Remove translation language
this.languageCode = languageCode;
this.isGenerated = isGenerated;
final String[] splits = languageCode.split("-");
this.locale = splits.length == 2 ? new Locale(splits[0], splits[1]) : new Locale(languageCode);
}
public Subtitles getSubtitle(final SubtitlesFormat format) {
return new Subtitles(format, locale, cleanUrl + "&fmt=" + format.getExtension(), isGenerated);
public SubtitlesStream getSubtitle(final MediaFormat format) {
return new SubtitlesStream(format, languageCode, cleanUrl + "&fmt=" + format.getSuffix(), isGenerated);
}
}

View File

@ -1,4 +1,4 @@
package org.schabi.newpipe.extractor.stream;
package org.schabi.newpipe.extractor.stream;// TODO: ¡git it!
/*
* Created by Christian Schabesberger on 10.08.15.
@ -21,8 +21,8 @@ package org.schabi.newpipe.extractor.stream;
*/
import org.schabi.newpipe.extractor.Extractor;
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.Subtitles;
import org.schabi.newpipe.extractor.UrlIdHandler;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@ -133,9 +133,9 @@ public abstract class StreamExtractor extends Extractor {
public abstract List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionException;
@Nonnull
public abstract List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException;
public abstract List<SubtitlesStream> getSubtitlesDefault() throws IOException, ExtractionException;
@Nonnull
public abstract List<Subtitles> getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException;
public abstract List<SubtitlesStream> getSubtitles(MediaFormat format) throws IOException, ExtractionException;
public abstract StreamType getStreamType() throws ParsingException;
public abstract StreamInfoItem getNextVideo() throws IOException, ExtractionException;

View File

@ -1,4 +1,4 @@
package org.schabi.newpipe.extractor.stream;
package org.schabi.newpipe.extractor.stream;// TODO: ¡git it!
import org.schabi.newpipe.extractor.*;
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
@ -282,7 +282,7 @@ public class StreamInfo extends Info {
private List<InfoItem> relatedStreams;
private long startPosition = 0;
private List<Subtitles> subtitles;
private List<SubtitlesStream> subtitles;
private String[] donationLinks;
private String[] affiliateLinks;
@ -472,11 +472,11 @@ public class StreamInfo extends Info {
this.startPosition = startPosition;
}
public List<Subtitles> getSubtitles() {
public List<SubtitlesStream> getSubtitles() {
return subtitles;
}
public void setSubtitles(List<Subtitles> subtitles) {
public void setSubtitles(List<SubtitlesStream> subtitles) {
this.subtitles = subtitles;
}

View File

@ -0,0 +1,71 @@
package org.schabi.newpipe.extractor.stream;// TODO: ¡git it!
import org.schabi.newpipe.extractor.MediaFormat;
import java.io.Serializable;
import java.util.Locale;
public class SubtitlesStream extends Stream implements Serializable {
private final MediaFormat format;
private final Locale locale;
private final String URL;
private final boolean autoGenerated;
private final String code;
public SubtitlesStream(MediaFormat format, String languageCode, String URL, boolean autoGenerated) {
super(URL, format);
/*
* Locale.forLanguageTag only for API >= 21
* Locale.Builder only for API >= 21
* Country codes doesn't work well without
*/
final String[] splits = languageCode.split("-");
switch (splits.length) {
default:
this.locale = new Locale(splits[0]);
break;
case 3:
this.locale = new Locale(splits[0], splits[1], splits[2]);// complex variants doesn't work!
break;
case 2:
this.locale = new Locale(splits[0], splits[1]);
break;
}
this.code = languageCode;
this.format = format;
this.URL = URL;
this.autoGenerated = autoGenerated;
}
public String getExtension() {
return format.suffix;
}
public String getURL() {
return URL;
}
public boolean isAutoGenerated() {
return autoGenerated;
}
@Override
public boolean equalStats(Stream cmp) {
return super.equalStats(cmp) && cmp instanceof SubtitlesStream &&
url.equals(((SubtitlesStream) cmp).url) &&
code.equals(((SubtitlesStream) cmp).code) &&
format == ((SubtitlesStream) cmp).format &&
autoGenerated == ((SubtitlesStream) cmp).autoGenerated;
}
public String getDisplayLanguageName() {
return locale.getDisplayName(locale);
}
public String getLanguageTag() {
return code;
}
}