Skip to content

Commit 52bcf56

Browse files
authored
Merge pull request #1323 from absurdlylongusername/minor-code-cleanup
2 parents dd2f24f + 25113b2 commit 52bcf56

File tree

13 files changed

+175
-112
lines changed

13 files changed

+175
-112
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,12 @@ public OffsetDateTime offsetDateTime() {
6969
public boolean isApproximation() {
7070
return isApproximation;
7171
}
72+
73+
@Override
74+
public String toString() {
75+
return "DateWrapper{"
76+
+ "offsetDateTime=" + offsetDateTime
77+
+ ", isApproximation=" + isApproximation
78+
+ '}';
79+
}
7280
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ public long getLength() {
121121

122122
@Override
123123
public long getTimeStamp() throws ParsingException {
124-
return getTimestampSeconds("(#t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)");
124+
final var timestamp = getTimestampSeconds("(#t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)");
125+
return timestamp == -2 ? 0 : timestamp;
125126
}
126127

127128
@Override
@@ -170,7 +171,7 @@ public List<AudioStream> getAudioStreams() throws ExtractionException {
170171

171172
try {
172173
final JsonArray transcodings = track.getObject("media")
173-
.getArray("transcodings");
174+
.getArray("transcodings");
174175
if (!isNullOrEmpty(transcodings)) {
175176
// Get information about what stream formats are available
176177
extractAudioStreams(transcodings, audioStreams);

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudStreamLinkHandlerFactory.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.schabi.newpipe.extractor.services.soundcloud.linkHandler;
22

3+
import java.util.regex.Pattern;
4+
35
import org.schabi.newpipe.extractor.exceptions.ParsingException;
46
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
57
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper;
@@ -9,11 +11,18 @@
911
public final class SoundcloudStreamLinkHandlerFactory extends LinkHandlerFactory {
1012
private static final SoundcloudStreamLinkHandlerFactory INSTANCE
1113
= new SoundcloudStreamLinkHandlerFactory();
12-
private static final String URL_PATTERN = "^https?://(www\\.|m\\.|on\\.)?"
13-
+ "soundcloud.com/[0-9a-z_-]+"
14-
+ "/(?!(tracks|albums|sets|reposts|followers|following)/?$)[0-9a-z_-]+/?([#?].*)?$";
15-
private static final String API_URL_PATTERN = "^https?://api-v2\\.soundcloud.com"
16-
+ "/(tracks|albums|sets|reposts|followers|following)/([0-9a-z_-]+)/";
14+
15+
private static final Pattern URL_PATTERN = Pattern.compile(
16+
"^https?://(?:www\\.|m\\.|on\\.)?"
17+
+ "soundcloud.com/[0-9a-z_-]+"
18+
+ "/(?!(?:tracks|albums|sets|reposts|followers|following)/?$)[0-9a-z_-]+/?(?:[#?].*)?$"
19+
);
20+
21+
private static final Pattern API_URL_PATTERN = Pattern.compile(
22+
"^https?://api-v2\\.soundcloud.com"
23+
+ "/(tracks|albums|sets|reposts|followers|following)/([0-9a-z_-]+)/"
24+
);
25+
1726
private SoundcloudStreamLinkHandlerFactory() {
1827
}
1928

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

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public Builder() {
8888
}
8989

9090
/**
91-
* Set the identifier of the {@link AudioStream}.
91+
* Set the identifier of the {@link AudioStream} which uniquely identifies the stream,
92+
* e.g. for YouTube this would be the itag
9293
*
9394
* <p>
9495
* It <b>must not be null</b> and should be non empty.
@@ -108,14 +109,14 @@ public Builder setId(@Nonnull final String id) {
108109
}
109110

110111
/**
111-
* Set the content of the {@link AudioStream}.
112-
*
112+
* Set the content or the URL of the {@link AudioStream}, depending on whether isUrl is
113+
* true
113114
* <p>
114115
* It must not be null, and should be non empty.
115116
* </p>
116117
*
117118
* @param content the content of the {@link AudioStream}
118-
* @param isUrl whether the content is a URL
119+
* @param isUrl whether content is the URL or the actual content of e.g. a DASH manifest
119120
* @return this {@link Builder} instance
120121
*/
121122
public Builder setContent(@Nonnull final String content,
@@ -126,7 +127,7 @@ public Builder setContent(@Nonnull final String content,
126127
}
127128

128129
/**
129-
* Set the {@link MediaFormat} used by the {@link AudioStream}.
130+
* Set the {@link MediaFormat} used by the {@link AudioStream}, which can be null
130131
*
131132
* <p>
132133
* It should be one of the audio {@link MediaFormat}s ({@link MediaFormat#M4A M4A},
@@ -278,16 +279,22 @@ public Builder setItagItem(@Nullable final ItagItem itagItem) {
278279
* Build an {@link AudioStream} using the builder's current values.
279280
*
280281
* <p>
281-
* The identifier and the content (and so the {@code isUrl} boolean) properties must have
282+
* The identifier and the content (and thus {@code isUrl}) properties must have
282283
* been set.
283284
* </p>
284285
*
285286
* @return a new {@link AudioStream} using the builder's current values
286-
* @throws IllegalStateException if {@code id}, {@code content} (and so {@code isUrl}) or
287+
* @throws IllegalStateException if {@code id}, {@code content} (and thus {@code isUrl}) or
287288
* {@code deliveryMethod} have been not set, or have been set as {@code null}
288289
*/
289290
@Nonnull
290291
public AudioStream build() {
292+
validateBuild();
293+
294+
return new AudioStream(this);
295+
}
296+
297+
void validateBuild() {
291298
if (id == null) {
292299
throw new IllegalStateException(
293300
"The identifier of the audio stream has been not set or is null. If you "
@@ -305,64 +312,39 @@ public AudioStream build() {
305312
"The delivery method of the audio stream has been set as null, which is "
306313
+ "not allowed. Pass a valid one instead with setDeliveryMethod.");
307314
}
308-
309-
return new AudioStream(id, content, isUrl, mediaFormat, deliveryMethod, averageBitrate,
310-
manifestUrl, audioTrackId, audioTrackName, audioLocale, audioTrackType,
311-
itagItem);
312315
}
313316
}
314317

315318

316319
/**
317-
* Create a new audio stream.
320+
* Create a new audio stream using the given {@link Builder}.
318321
*
319-
* @param id the identifier which uniquely identifies the stream, e.g. for YouTube
320-
* this would be the itag
321-
* @param content the content or the URL of the stream, depending on whether isUrl is
322-
* true
323-
* @param isUrl whether content is the URL or the actual content of e.g. a DASH
324-
* manifest
325-
* @param format the {@link MediaFormat} used by the stream, which can be null
326-
* @param deliveryMethod the {@link DeliveryMethod} of the stream
327-
* @param averageBitrate the average bitrate of the stream (which can be unknown, see
328-
* {@link #UNKNOWN_BITRATE})
329-
* @param audioTrackId the id of the audio track
330-
* @param audioTrackName the name of the audio track
331-
* @param audioLocale the {@link Locale} of the audio stream, representing its language
332-
* @param itagItem the {@link ItagItem} corresponding to the stream, which cannot be null
333-
* @param manifestUrl the URL of the manifest this stream comes from (if applicable,
334-
* otherwise null)
322+
* @param builder The {@link Builder} to use to create the audio stream
335323
*/
336324
@SuppressWarnings("checkstyle:ParameterNumber")
337-
private AudioStream(@Nonnull final String id,
338-
@Nonnull final String content,
339-
final boolean isUrl,
340-
@Nullable final MediaFormat format,
341-
@Nonnull final DeliveryMethod deliveryMethod,
342-
final int averageBitrate,
343-
@Nullable final String manifestUrl,
344-
@Nullable final String audioTrackId,
345-
@Nullable final String audioTrackName,
346-
@Nullable final Locale audioLocale,
347-
@Nullable final AudioTrackType audioTrackType,
348-
@Nullable final ItagItem itagItem) {
349-
super(id, content, isUrl, format, deliveryMethod, manifestUrl);
350-
if (itagItem != null) {
351-
this.itagItem = itagItem;
352-
this.itag = itagItem.id;
353-
this.quality = itagItem.getQuality();
354-
this.bitrate = itagItem.getBitrate();
355-
this.initStart = itagItem.getInitStart();
356-
this.initEnd = itagItem.getInitEnd();
357-
this.indexStart = itagItem.getIndexStart();
358-
this.indexEnd = itagItem.getIndexEnd();
359-
this.codec = itagItem.getCodec();
325+
AudioStream(final Builder builder) {
326+
super(builder.id,
327+
builder.content,
328+
builder.isUrl,
329+
builder.mediaFormat,
330+
builder.deliveryMethod,
331+
builder.manifestUrl);
332+
if (builder.itagItem != null) {
333+
this.itagItem = builder.itagItem;
334+
this.itag = builder.itagItem.id;
335+
this.quality = builder.itagItem.getQuality();
336+
this.bitrate = builder.itagItem.getBitrate();
337+
this.initStart = builder.itagItem.getInitStart();
338+
this.initEnd = builder.itagItem.getInitEnd();
339+
this.indexStart = builder.itagItem.getIndexStart();
340+
this.indexEnd = builder.itagItem.getIndexEnd();
341+
this.codec = builder.itagItem.getCodec();
360342
}
361-
this.averageBitrate = averageBitrate;
362-
this.audioTrackId = audioTrackId;
363-
this.audioTrackName = audioTrackName;
364-
this.audioLocale = audioLocale;
365-
this.audioTrackType = audioTrackType;
343+
this.averageBitrate = builder.averageBitrate;
344+
this.audioTrackId = builder.audioTrackId;
345+
this.audioTrackName = builder.audioTrackName;
346+
this.audioLocale = builder.audioLocale;
347+
this.audioTrackType = builder.audioTrackType;
366348
}
367349

368350
/**

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ public class Description implements Serializable {
1717

1818
public Description(@Nullable final String content, final int type) {
1919
this.type = type;
20-
if (content == null) {
21-
this.content = "";
22-
} else {
23-
this.content = content;
24-
}
20+
this.content = Objects.requireNonNullElse(content, "");
2521
}
2622

2723
public String getContent() {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public Stream(final String id,
6868
* @param streamList the list of {@link Stream}s which will be compared
6969
* @return whether the list already contains one stream with equals stats
7070
*/
71-
public static boolean containSimilarStream(final Stream stream,
71+
public static boolean containSimilarStream(@Nonnull final Stream stream,
7272
final List<? extends Stream> streamList) {
7373
if (isNullOrEmpty(streamList)) {
7474
return false;
@@ -98,11 +98,9 @@ public static boolean containSimilarStream(final Stream stream,
9898
* @return whether the stream have the same stats or not, based on the criteria above
9999
*/
100100
public boolean equalStats(@Nullable final Stream other) {
101-
if (other == null || mediaFormat == null || other.mediaFormat == null) {
102-
return false;
103-
}
104-
return mediaFormat.id == other.mediaFormat.id && deliveryMethod == other.deliveryMethod
105-
&& isUrl == other.isUrl;
101+
return other != null && mediaFormat != null && other.mediaFormat != null
102+
&& mediaFormat.id == other.mediaFormat.id && deliveryMethod == other.deliveryMethod
103+
&& isUrl == other.isUrl;
106104
}
107105

108106
/**
@@ -137,6 +135,7 @@ public String getUrl() {
137135
*
138136
* @return the content or URL
139137
*/
138+
@Nonnull
140139
public String getContent() {
141140
return content;
142141
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ private static void extractOptionalData(final StreamInfo streamInfo,
352352
private String uploaderUrl = "";
353353
@Nonnull
354354
private List<Image> uploaderAvatars = List.of();
355-
private boolean uploaderVerified = false;
355+
private boolean uploaderVerified;
356356
private long uploaderSubscriberCount = -1;
357357

358358
private String subChannelName = "";
@@ -368,19 +368,19 @@ private static void extractOptionalData(final StreamInfo streamInfo,
368368
private String hlsUrl = "";
369369
private List<InfoItem> relatedItems = List.of();
370370

371-
private long startPosition = 0;
371+
private long startPosition;
372372
private List<SubtitlesStream> subtitles = List.of();
373373

374374
private String host = "";
375375
private StreamExtractor.Privacy privacy;
376376
private String category = "";
377377
private String licence = "";
378378
private String supportInfo = "";
379-
private Locale language = null;
379+
private Locale language;
380380
private List<String> tags = List.of();
381381
private List<StreamSegment> streamSegments = List.of();
382382
private List<MetaInfo> metaInfo = List.of();
383-
private boolean shortFormContent = false;
383+
private boolean shortFormContent;
384384

385385
/**
386386
* Preview frames, e.g. for the storyboard / seekbar thumbnail preview

extractor/src/main/java/org/schabi/newpipe/extractor/utils/ImageSuffix.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* <p>
1515
* This class is used to construct {@link org.schabi.newpipe.extractor.Image Image}
1616
* instances from a single base URL/path, in order to get all or most image resolutions provided,
17-
* depending of the service and the resolutions provided.
17+
* depending on the service and the resolutions provided.
1818
* </p>
1919
*
2020
* <p>

0 commit comments

Comments
 (0)