Skip to content

Commit 58a2479

Browse files
committed
Apply changes in all playlist extractors except YoutubePlaylistExtractor
Also fix some issues in the extractors, remove uneeded overrides, use the Java 8 Stream API where possible and replace usages of Utils.UTF_8 with StandardCharsets.UTF_8 in these classes.
1 parent fc6b45e commit 58a2479

4 files changed

Lines changed: 54 additions & 121 deletions

File tree

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

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919

2020
import javax.annotation.Nonnull;
2121
import java.io.IOException;
22+
import java.util.Objects;
2223

2324
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImageUrl;
2425
import static org.schabi.newpipe.extractor.utils.JsonUtils.getJsonData;
2526
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamExtractor.getAlbumInfoJson;
27+
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
28+
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
2629

2730
public class BandcampPlaylistExtractor extends PlaylistExtractor {
2831

@@ -57,33 +60,27 @@ public void onFetchPage(@Nonnull final Downloader downloader) throws IOException
5760
throw new ParsingException("JSON does not exist", e);
5861
}
5962

60-
61-
62-
if (trackInfo.size() <= 0) {
63+
if (trackInfo.isEmpty()) {
6364
// Albums without trackInfo need to be purchased before they can be played
6465
throw new ContentNotAvailableException("Album needs to be purchased");
6566
}
6667
}
6768

69+
@Nonnull
6870
@Override
6971
public String getThumbnailUrl() throws ParsingException {
7072
if (albumJson.isNull("art_id")) {
71-
return "";
73+
return EMPTY_STRING;
7274
} else {
7375
return getImageUrl(albumJson.getLong("art_id"), true);
7476
}
7577
}
7678

77-
@Override
78-
public String getBannerUrl() {
79-
return "";
80-
}
81-
8279
@Override
8380
public String getUploaderUrl() throws ParsingException {
8481
final String[] parts = getUrl().split("/");
8582
// https: (/) (/) * .bandcamp.com (/) and leave out the rest
86-
return "https://" + parts[2] + "/";
83+
return HTTPS + parts[2] + "/";
8784
}
8885

8986
@Override
@@ -94,9 +91,10 @@ public String getUploaderName() {
9491
@Override
9592
public String getUploaderAvatarUrl() {
9693
try {
97-
return document.getElementsByClass("band-photo").first().attr("src");
98-
} catch (NullPointerException e) {
99-
return "";
94+
return Objects.requireNonNull(document.getElementsByClass("band-photo").first())
95+
.attr("src");
96+
} catch (final NullPointerException e) {
97+
return EMPTY_STRING;
10098
}
10199
}
102100

@@ -110,24 +108,6 @@ public long getStreamCount() {
110108
return trackInfo.size();
111109
}
112110

113-
@Nonnull
114-
@Override
115-
public String getSubChannelName() {
116-
return "";
117-
}
118-
119-
@Nonnull
120-
@Override
121-
public String getSubChannelUrl() {
122-
return "";
123-
}
124-
125-
@Nonnull
126-
@Override
127-
public String getSubChannelAvatarUrl() {
128-
return "";
129-
}
130-
131111
@Nonnull
132112
@Override
133113
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
@@ -146,14 +126,13 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException
146126
collector.commit(new BandcampPlaylistStreamInfoItemExtractor(
147127
track, getUploaderUrl(), getThumbnailUrl()));
148128
}
149-
150129
}
151130

152131
return new InfoItemsPage<>(collector, null);
153132
}
154133

155134
@Override
156-
public InfoItemsPage<StreamInfoItem> getPage(Page page) {
135+
public InfoItemsPage<StreamInfoItem> getPage(final Page page) {
157136
return null;
158137
}
159138

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubePlaylistExtractor.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,12 @@ public PeertubePlaylistExtractor(final StreamingService service, final ListLinkH
2929
super(service, linkHandler);
3030
}
3131

32+
@Nonnull
3233
@Override
3334
public String getThumbnailUrl() throws ParsingException {
3435
return getBaseUrl() + playlistInfo.getString("thumbnailPath");
3536
}
3637

37-
@Override
38-
public String getBannerUrl() {
39-
return null;
40-
}
41-
4238
@Override
4339
public String getUploaderUrl() {
4440
return playlistInfo.getObject("ownerAccount").getString("url");

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

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import java.util.List;
2424

2525
import javax.annotation.Nonnull;
26-
import javax.annotation.Nullable;
2726

2827
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
28+
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
2929
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
3030

3131
public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
@@ -67,7 +67,7 @@ public String getName() {
6767
return playlist.getString("title");
6868
}
6969

70-
@Nullable
70+
@Nonnull
7171
@Override
7272
public String getThumbnailUrl() {
7373
String artworkUrl = playlist.getString("artwork_url");
@@ -80,24 +80,21 @@ public String getThumbnailUrl() {
8080

8181
for (final StreamInfoItem item : infoItems.getItems()) {
8282
artworkUrl = item.getThumbnailUrl();
83-
if (!isNullOrEmpty(artworkUrl)) break;
83+
if (!isNullOrEmpty(artworkUrl)) {
84+
break;
85+
}
8486
}
8587
} catch (final Exception ignored) {
8688
}
8789

8890
if (artworkUrl == null) {
89-
return null;
91+
return EMPTY_STRING;
9092
}
9193
}
9294

9395
return artworkUrl.replace("large.jpg", "crop.jpg");
9496
}
9597

96-
@Override
97-
public String getBannerUrl() {
98-
return null;
99-
}
100-
10198
@Override
10299
public String getUploaderUrl() {
103100
return SoundcloudParsingHelper.getUploaderUrl(playlist);
@@ -123,24 +120,6 @@ public long getStreamCount() {
123120
return playlist.getLong("track_count");
124121
}
125122

126-
@Nonnull
127-
@Override
128-
public String getSubChannelName() {
129-
return "";
130-
}
131-
132-
@Nonnull
133-
@Override
134-
public String getSubChannelUrl() {
135-
return "";
136-
}
137-
138-
@Nonnull
139-
@Override
140-
public String getSubChannelAvatarUrl() {
141-
return "";
142-
}
143-
144123
@Nonnull
145124
@Override
146125
public InfoItemsPage<StreamInfoItem> getInitialPage() {
@@ -149,18 +128,18 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() {
149128
final List<String> ids = new ArrayList<>();
150129

151130
final JsonArray tracks = playlist.getArray("tracks");
152-
for (final Object o : tracks) {
153-
if (o instanceof JsonObject) {
154-
final JsonObject track = (JsonObject) o;
155-
if (track.has("title")) { // i.e. if full info is available
156-
streamInfoItemsCollector.commit(new SoundcloudStreamInfoItemExtractor(track));
157-
} else {
158-
// %09d would be enough, but a 0 before the number does not create problems, so
159-
// let's be sure
160-
ids.add(String.format("%010d", track.getInt("id")));
161-
}
162-
}
163-
}
131+
tracks.stream().filter(JsonObject.class::isInstance)
132+
.map(JsonObject.class::cast)
133+
.forEachOrdered(track -> {
134+
if (track.has("title")) { // i.e. if full info is available
135+
streamInfoItemsCollector.commit(
136+
new SoundcloudStreamInfoItemExtractor(track));
137+
} else {
138+
// %09d would be enough, but a 0 before the number does not create
139+
// problems, so let's be sure
140+
ids.add(String.format("%010d", track.getInt("id")));
141+
}
142+
});
164143

165144
return new InfoItemsPage<>(streamInfoItemsCollector, new Page(ids));
166145
}

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

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.IOException;
2525
import java.net.URL;
26+
import java.nio.charset.StandardCharsets;
2627
import java.util.*;
2728

2829
import javax.annotation.Nonnull;
@@ -71,7 +72,7 @@ public void onFetchPage(@Nonnull final Downloader downloader)
7172
jsonBody.value("playlistIndex", Integer.parseInt(playlistIndexString));
7273
}
7374

74-
final byte[] body = JsonWriter.string(jsonBody.done()).getBytes(UTF_8);
75+
final byte[] body = JsonWriter.string(jsonBody.done()).getBytes(StandardCharsets.UTF_8);
7576

7677
final Map<String, List<String>> headers = new HashMap<>();
7778
addClientInfoHeaders(headers);
@@ -97,6 +98,7 @@ public String getName() throws ParsingException {
9798
return name;
9899
}
99100

101+
@Nonnull
100102
@Override
101103
public String getThumbnailUrl() throws ParsingException {
102104
try {
@@ -108,19 +110,15 @@ public String getThumbnailUrl() throws ParsingException {
108110
.getObject("watchEndpoint").getString("videoId"));
109111
} catch (final Exception ignored) {
110112
}
113+
111114
throw new ParsingException("Could not get playlist thumbnail", e);
112115
}
113116
}
114117

115-
@Override
116-
public String getBannerUrl() {
117-
return "";
118-
}
119-
120118
@Override
121119
public String getUploaderUrl() {
122120
// YouTube mixes are auto-generated by YouTube
123-
return "";
121+
return EMPTY_STRING;
124122
}
125123

126124
@Override
@@ -132,7 +130,7 @@ public String getUploaderName() {
132130
@Override
133131
public String getUploaderAvatarUrl() {
134132
// YouTube mixes are auto-generated by YouTube
135-
return "";
133+
return EMPTY_STRING;
136134
}
137135

138136
@Override
@@ -148,8 +146,8 @@ public long getStreamCount() {
148146

149147
@Nonnull
150148
@Override
151-
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException,
152-
ExtractionException {
149+
public InfoItemsPage<StreamInfoItem> getInitialPage()
150+
throws IOException, ExtractionException {
153151
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
154152
collectStreamsFrom(collector, playlistData.getArray("contents"));
155153

@@ -159,9 +157,10 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException,
159157
return new InfoItemsPage<>(collector, getNextPageFrom(playlistData, cookies));
160158
}
161159

162-
private Page getNextPageFrom(final JsonObject playlistJson,
163-
final Map<String, String> cookies) throws IOException,
164-
ExtractionException {
160+
@Nonnull
161+
private Page getNextPageFrom(@Nonnull final JsonObject playlistJson,
162+
final Map<String, String> cookies)
163+
throws IOException, ExtractionException {
165164
final JsonObject lastStream = ((JsonObject) playlistJson.getArray("contents")
166165
.get(playlistJson.getArray("contents").size() - 1));
167166
if (lastStream == null || lastStream.getObject("playlistPanelVideoRenderer") == null) {
@@ -181,7 +180,7 @@ private Page getNextPageFrom(final JsonObject playlistJson,
181180
.value("playlistIndex", index)
182181
.value("params", params)
183182
.done())
184-
.getBytes(UTF_8);
183+
.getBytes(StandardCharsets.UTF_8);
185184

186185
return new Page(YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, cookies, body);
187186
}
@@ -217,26 +216,23 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException
217216

218217
private void collectStreamsFrom(@Nonnull final StreamInfoItemsCollector collector,
219218
@Nullable final List<Object> streams) {
220-
221219
if (streams == null) {
222220
return;
223221
}
224222

225223
final TimeAgoParser timeAgoParser = getTimeAgoParser();
226224

227-
for (final Object stream : streams) {
228-
if (stream instanceof JsonObject) {
229-
final JsonObject streamInfo = ((JsonObject) stream)
230-
.getObject("playlistPanelVideoRenderer");
231-
if (streamInfo != null) {
232-
collector.commit(new YoutubeStreamInfoItemExtractor(streamInfo,
233-
timeAgoParser));
234-
}
235-
}
236-
}
225+
streams.stream()
226+
.filter(JsonObject.class::isInstance)
227+
.map(stream -> ((JsonObject) stream)
228+
.getObject("playlistPanelVideoRenderer"))
229+
.filter(Objects::nonNull)
230+
.map(streamInfo -> new YoutubeStreamInfoItemExtractor(streamInfo, timeAgoParser))
231+
.forEachOrdered(collector::commit);
237232
}
238233

239-
private String getThumbnailUrlFromPlaylistId(final String playlistId) throws ParsingException {
234+
@Nonnull
235+
private String getThumbnailUrlFromPlaylistId(@Nonnull final String playlistId) throws ParsingException {
240236
final String videoId;
241237
if (playlistId.startsWith("RDMM")) {
242238
videoId = playlistId.substring(4);
@@ -251,25 +247,8 @@ private String getThumbnailUrlFromPlaylistId(final String playlistId) throws Par
251247
return getThumbnailUrlFromVideoId(videoId);
252248
}
253249

250+
@Nonnull
254251
private String getThumbnailUrlFromVideoId(final String videoId) {
255252
return "https://i.ytimg.com/vi/" + videoId + "/hqdefault.jpg";
256253
}
257-
258-
@Nonnull
259-
@Override
260-
public String getSubChannelName() {
261-
return "";
262-
}
263-
264-
@Nonnull
265-
@Override
266-
public String getSubChannelUrl() {
267-
return "";
268-
}
269-
270-
@Nonnull
271-
@Override
272-
public String getSubChannelAvatarUrl() {
273-
return "";
274-
}
275254
}

0 commit comments

Comments
 (0)