Skip to content

Commit 044639c

Browse files
StypoxAudricV
authored andcommitted
Solve some review comments
1 parent c33d392 commit 044639c

5 files changed

Lines changed: 66 additions & 124 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampRadioStreamExtractor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.grack.nanojson.JsonParser;
66
import com.grack.nanojson.JsonParserException;
77
import org.jsoup.Jsoup;
8+
import org.jsoup.nodes.Element;
89
import org.schabi.newpipe.extractor.MediaFormat;
910
import org.schabi.newpipe.extractor.NewPipe;
1011
import org.schabi.newpipe.extractor.StreamingService;
@@ -80,9 +81,11 @@ public String getUrl() throws ParsingException {
8081

8182
@Nonnull
8283
@Override
83-
public String getUploaderName() {
84-
return Jsoup.parse(showInfo.getString("image_caption"))
85-
.getElementsByTag("a").first().text();
84+
public String getUploaderName() throws ParsingException {
85+
return Jsoup.parse(showInfo.getString("image_caption")).getElementsByTag("a").stream()
86+
.map(Element::text)
87+
.findFirst()
88+
.orElseThrow(() -> new ParsingException("Could not get uploader name"));
8689
}
8790

8891
@Nullable

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/DeliveryType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Streaming format types used by YouTube in their streams.
55
*
66
* <p>
7-
* It is different of {@link org.schabi.newpipe.extractor.stream.DeliveryMethod delivery methods}!
7+
* It is different from {@link org.schabi.newpipe.extractor.stream.DeliveryMethod delivery methods}!
88
* </p>
99
*/
1010
public enum DeliveryType {

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/dashmanifestcreators/YoutubeDashManifestCreatorsUtils.java

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -83,59 +83,18 @@ private YoutubeDashManifestCreatorsUtils() {
8383
*/
8484
public static final String ALR_YES = "&alr=yes";
8585

86-
/**
87-
* Constant which represents the {@code MPD} element of DASH manifests.
88-
*/
86+
// XML elements of DASH MPD manifests
87+
// see https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
8988
public static final String MPD = "MPD";
90-
91-
/**
92-
* Constant which represents the {@code Period} element of DASH manifests.
93-
*/
9489
public static final String PERIOD = "Period";
95-
96-
/**
97-
* Constant which represents the {@code AdaptationSet} element of DASH manifests.
98-
*/
9990
public static final String ADAPTATION_SET = "AdaptationSet";
100-
101-
/**
102-
* Constant which represents the {@code Role} element of DASH manifests.
103-
*/
10491
public static final String ROLE = "Role";
105-
106-
/**
107-
* Constant which represents the {@code Representation} element of DASH manifests.
108-
*/
10992
public static final String REPRESENTATION = "Representation";
110-
111-
/**
112-
* Constant which represents the {@code AudioChannelConfiguration} element of DASH manifests.
113-
*/
11493
public static final String AUDIO_CHANNEL_CONFIGURATION = "AudioChannelConfiguration";
115-
116-
/**
117-
* Constant which represents the {@code SegmentTemplate} element of DASH manifests.
118-
*/
11994
public static final String SEGMENT_TEMPLATE = "SegmentTemplate";
120-
121-
/**
122-
* Constant which represents the {@code SegmentTimeline} element of DASH manifests.
123-
*/
12495
public static final String SEGMENT_TIMELINE = "SegmentTimeline";
125-
126-
/**
127-
* Constant which represents the {@code SegmentBase} element of DASH manifests.
128-
*/
12996
public static final String BASE_URL = "BaseURL";
130-
131-
/**
132-
* Constant which represents the {@code SegmentBase} element of DASH manifests.
133-
*/
13497
public static final String SEGMENT_BASE = "SegmentBase";
135-
136-
/**
137-
* Constant which represents the {@code Initialization} element of DASH manifests.
138-
*/
13998
public static final String INITIALIZATION = "Initialization";
14099

141100
/**
@@ -665,7 +624,7 @@ public static Response getInitializationResponse(@Nonnull String baseStreamingUr
665624
if (isHtml5StreamingUrl) {
666625
baseStreamingUrl += ALR_YES;
667626
}
668-
baseStreamingUrl = appendRnParamAndSqParamIfNeeded(baseStreamingUrl, deliveryType);
627+
baseStreamingUrl = appendRnSqParamsIfNeeded(baseStreamingUrl, deliveryType);
669628

670629
final Downloader downloader = NewPipe.getDownloader();
671630
if (isHtml5StreamingUrl) {
@@ -701,16 +660,16 @@ public static Response getInitializationResponse(@Nonnull String baseStreamingUr
701660
* {@link XMLConstants#ACCESS_EXTERNAL_SCHEMA} in {@link DocumentBuilderFactory} instances.
702661
*
703662
* @return an instance of {@link Document} secured against XXE attacks on supported platforms,
704-
* that should then be convertible to an XML string without security problems
663+
* that should then be convertible to an XML string without security problems
705664
*/
706665
private static Document newDocument() throws ParserConfigurationException {
707666
final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
708667
try {
709668
documentBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
710669
documentBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
711670
} catch (final Exception ignored) {
712-
// Ignore exceptions as setting these attributes to secure XML generation is supported
713-
// by all platforms (like the Android implementation)
671+
// Ignore exceptions as setting these attributes to secure XML generation is not
672+
// supported by all platforms (like the Android implementation)
714673
}
715674

716675
final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
@@ -736,8 +695,8 @@ private static String documentToXml(@Nonnull final Document document)
736695
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
737696
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
738697
} catch (final Exception ignored) {
739-
// Ignore exceptions as setting these attributes to secure XML generation is supported
740-
// by all platforms (like the Android implementation)
698+
// Ignore exceptions as setting these attributes to secure XML generation is not
699+
// supported by all platforms (like the Android implementation)
741700
}
742701

743702
final Transformer transformer = transformerFactory.newTransformer();
@@ -759,16 +718,10 @@ private static String documentToXml(@Nonnull final Document document)
759718
* @return the base streaming URL to which the param(s) are appended, depending on the
760719
* {@link DeliveryType} of the stream
761720
*/
762-
@SuppressWarnings({"checkstyle:FinalParameters", "checkstyle:FinalLocalVariable"})
763721
@Nonnull
764-
private static String appendRnParamAndSqParamIfNeeded(
765-
@Nonnull String baseStreamingUrl,
766-
@Nonnull final DeliveryType deliveryType) {
767-
if (deliveryType != DeliveryType.PROGRESSIVE) {
768-
baseStreamingUrl += SQ_0;
769-
}
770-
771-
return baseStreamingUrl + RN_0;
722+
private static String appendRnSqParamsIfNeeded(@Nonnull final String baseStreamingUrl,
723+
@Nonnull final DeliveryType deliveryType) {
724+
return baseStreamingUrl + (deliveryType == DeliveryType.PROGRESSIVE ? "" : SQ_0) + RN_0;
772725
}
773726

774727
/**

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java

Lines changed: 47 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,17 +1140,11 @@ private JsonObject getVideoSecondaryInfoRenderer() throws ParsingException {
11401140
return videoSecondaryInfoRenderer;
11411141
}
11421142

1143-
@FunctionalInterface
1144-
private interface StreamBuilderHelper<T extends Stream> {
1145-
@Nonnull
1146-
T buildStream(ItagInfo itagInfo);
1147-
}
1148-
11491143
@Nonnull
11501144
private <T extends Stream> List<T> getItags(
11511145
final String streamingDataKey,
11521146
final ItagItem.ItagType itagTypeWanted,
1153-
final StreamBuilderHelper<T> streamBuilderHelper,
1147+
final java.util.function.Function<ItagInfo, T> streamBuilderHelper,
11541148
final String streamTypeExceptionMessage) throws ParsingException {
11551149
try {
11561150
final List<ItagInfo> itagInfos = new ArrayList<>();
@@ -1176,7 +1170,7 @@ private <T extends Stream> List<T> getItags(
11761170

11771171
final List<T> streamList = new ArrayList<>();
11781172
for (final ItagInfo itagInfo : itagInfos) {
1179-
final T stream = streamBuilderHelper.buildStream(itagInfo);
1173+
final T stream = streamBuilderHelper.apply(itagInfo);
11801174
if (!Stream.containSimilarStream(stream, streamList)) {
11811175
streamList.add(stream);
11821176
}
@@ -1190,8 +1184,8 @@ private <T extends Stream> List<T> getItags(
11901184
}
11911185

11921186
/**
1193-
* Get the {@link StreamBuilderHelper} which will be used to build {@link AudioStream}s in
1194-
* {@link #getItags(String, ItagItem.ItagType, StreamBuilderHelper, String)}
1187+
* Get the stream builder helper which will be used to build {@link AudioStream}s in
1188+
* {@link #getItags(String, ItagItem.ItagType, java.util.function.Function, String)}
11951189
*
11961190
* <p>
11971191
* The {@code StreamBuilderHelper} will set the following attributes in the
@@ -1213,38 +1207,34 @@ private <T extends Stream> List<T> getItags(
12131207
* Note that the {@link ItagItem} comes from an {@link ItagInfo} instance.
12141208
* </p>
12151209
*
1216-
* @return a {@link StreamBuilderHelper} to build {@link AudioStream}s
1210+
* @return a stream builder helper to build {@link AudioStream}s
12171211
*/
12181212
@Nonnull
1219-
private StreamBuilderHelper<AudioStream> getAudioStreamBuilderHelper() {
1220-
return new StreamBuilderHelper<AudioStream>() {
1221-
@Nonnull
1222-
@Override
1223-
public AudioStream buildStream(@Nonnull final ItagInfo itagInfo) {
1224-
final ItagItem itagItem = itagInfo.getItagItem();
1225-
final AudioStream.Builder builder = new AudioStream.Builder()
1226-
.setId(String.valueOf(itagItem.id))
1227-
.setContent(itagInfo.getContent(), itagInfo.getIsUrl())
1228-
.setMediaFormat(itagItem.getMediaFormat())
1229-
.setAverageBitrate(itagItem.getAverageBitrate())
1230-
.setItagItem(itagItem);
1231-
1232-
if (streamType == StreamType.LIVE_STREAM
1233-
|| streamType == StreamType.POST_LIVE_STREAM
1234-
|| !itagInfo.getIsUrl()) {
1235-
// For YouTube videos on OTF streams and for all streams of post-live streams
1236-
// and live streams, only the DASH delivery method can be used.
1237-
builder.setDeliveryMethod(DeliveryMethod.DASH);
1238-
}
1239-
1240-
return builder.build();
1213+
private java.util.function.Function<ItagInfo, AudioStream> getAudioStreamBuilderHelper() {
1214+
return (itagInfo) -> {
1215+
final ItagItem itagItem = itagInfo.getItagItem();
1216+
final AudioStream.Builder builder = new AudioStream.Builder()
1217+
.setId(String.valueOf(itagItem.id))
1218+
.setContent(itagInfo.getContent(), itagInfo.getIsUrl())
1219+
.setMediaFormat(itagItem.getMediaFormat())
1220+
.setAverageBitrate(itagItem.getAverageBitrate())
1221+
.setItagItem(itagItem);
1222+
1223+
if (streamType == StreamType.LIVE_STREAM
1224+
|| streamType == StreamType.POST_LIVE_STREAM
1225+
|| !itagInfo.getIsUrl()) {
1226+
// For YouTube videos on OTF streams and for all streams of post-live streams
1227+
// and live streams, only the DASH delivery method can be used.
1228+
builder.setDeliveryMethod(DeliveryMethod.DASH);
12411229
}
1230+
1231+
return builder.build();
12421232
};
12431233
}
12441234

12451235
/**
1246-
* Get the {@link StreamBuilderHelper} which will be used to build {@link VideoStream}s in
1247-
* {@link #getItags(String, ItagItem.ItagType, StreamBuilderHelper, String)}
1236+
* Get the stream builder helper which will be used to build {@link VideoStream}s in
1237+
* {@link #getItags(String, ItagItem.ItagType, java.util.function.Function, String)}
12481238
*
12491239
* <p>
12501240
* The {@code StreamBuilderHelper} will set the following attributes in the
@@ -1272,37 +1262,33 @@ public AudioStream buildStream(@Nonnull final ItagInfo itagInfo) {
12721262
* Note that the {@link ItagItem} comes from an {@link ItagInfo} instance.
12731263
* </p>
12741264
*
1275-
* @param areStreamsVideoOnly whether the {@link StreamBuilderHelper} will set the video
1265+
* @param areStreamsVideoOnly whether the stream builder helper will set the video
12761266
* streams as video-only streams
1277-
* @return a {@link StreamBuilderHelper} to build {@link VideoStream}s
1267+
* @return a stream builder helper to build {@link VideoStream}s
12781268
*/
12791269
@Nonnull
1280-
private StreamBuilderHelper<VideoStream> getVideoStreamBuilderHelper(
1270+
private java.util.function.Function<ItagInfo, VideoStream> getVideoStreamBuilderHelper(
12811271
final boolean areStreamsVideoOnly) {
1282-
return new StreamBuilderHelper<VideoStream>() {
1283-
@Nonnull
1284-
@Override
1285-
public VideoStream buildStream(@Nonnull final ItagInfo itagInfo) {
1286-
final ItagItem itagItem = itagInfo.getItagItem();
1287-
final VideoStream.Builder builder = new VideoStream.Builder()
1288-
.setId(String.valueOf(itagItem.id))
1289-
.setContent(itagInfo.getContent(), itagInfo.getIsUrl())
1290-
.setMediaFormat(itagItem.getMediaFormat())
1291-
.setIsVideoOnly(areStreamsVideoOnly)
1292-
.setItagItem(itagItem);
1293-
1294-
final String resolutionString = itagItem.getResolutionString();
1295-
builder.setResolution(resolutionString != null ? resolutionString
1296-
: EMPTY_STRING);
1297-
1298-
if (streamType != StreamType.VIDEO_STREAM || !itagInfo.getIsUrl()) {
1299-
// For YouTube videos on OTF streams and for all streams of post-live streams
1300-
// and live streams, only the DASH delivery method can be used.
1301-
builder.setDeliveryMethod(DeliveryMethod.DASH);
1302-
}
1303-
1304-
return builder.build();
1272+
return (itagInfo) -> {
1273+
final ItagItem itagItem = itagInfo.getItagItem();
1274+
final VideoStream.Builder builder = new VideoStream.Builder()
1275+
.setId(String.valueOf(itagItem.id))
1276+
.setContent(itagInfo.getContent(), itagInfo.getIsUrl())
1277+
.setMediaFormat(itagItem.getMediaFormat())
1278+
.setIsVideoOnly(areStreamsVideoOnly)
1279+
.setItagItem(itagItem);
1280+
1281+
final String resolutionString = itagItem.getResolutionString();
1282+
builder.setResolution(resolutionString != null ? resolutionString
1283+
: EMPTY_STRING);
1284+
1285+
if (streamType != StreamType.VIDEO_STREAM || !itagInfo.getIsUrl()) {
1286+
// For YouTube videos on OTF streams and for all streams of post-live streams
1287+
// and live streams, only the DASH delivery method can be used.
1288+
builder.setDeliveryMethod(DeliveryMethod.DASH);
13051289
}
1290+
1291+
return builder.build();
13061292
};
13071293
}
13081294

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static StreamInfo extractImportantData(@Nonnull final StreamExtractor ex
114114
final String name = extractor.getName();
115115
final int ageLimit = extractor.getAgeLimit();
116116

117-
// Suppress always-non-null warning as here we double-check it is really not null
117+
// Suppress always-non-null warning as here we double-check it really is not null
118118
//noinspection ConstantConditions
119119
if (streamType == StreamType.NONE
120120
|| isNullOrEmpty(url)

0 commit comments

Comments
 (0)