From 58a247907e66252483ddd33a55f13301d909867f Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sat, 12 Mar 2022 13:16:00 +0100 Subject: [PATCH] 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. --- .../extractors/BandcampPlaylistExtractor.java | 45 ++++--------- .../extractors/PeertubePlaylistExtractor.java | 6 +- .../SoundcloudPlaylistExtractor.java | 57 +++++----------- .../YoutubeMixPlaylistExtractor.java | 67 +++++++------------ 4 files changed, 54 insertions(+), 121 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistExtractor.java index 8cc21a6e7..7bd98b863 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistExtractor.java @@ -19,10 +19,13 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; import javax.annotation.Nonnull; import java.io.IOException; +import java.util.Objects; import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImageUrl; import static org.schabi.newpipe.extractor.utils.JsonUtils.getJsonData; import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamExtractor.getAlbumInfoJson; +import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.HTTPS; public class BandcampPlaylistExtractor extends PlaylistExtractor { @@ -57,33 +60,27 @@ public class BandcampPlaylistExtractor extends PlaylistExtractor { throw new ParsingException("JSON does not exist", e); } - - - if (trackInfo.size() <= 0) { + if (trackInfo.isEmpty()) { // Albums without trackInfo need to be purchased before they can be played throw new ContentNotAvailableException("Album needs to be purchased"); } } + @Nonnull @Override public String getThumbnailUrl() throws ParsingException { if (albumJson.isNull("art_id")) { - return ""; + return EMPTY_STRING; } else { return getImageUrl(albumJson.getLong("art_id"), true); } } - @Override - public String getBannerUrl() { - return ""; - } - @Override public String getUploaderUrl() throws ParsingException { final String[] parts = getUrl().split("/"); // https: (/) (/) * .bandcamp.com (/) and leave out the rest - return "https://" + parts[2] + "/"; + return HTTPS + parts[2] + "/"; } @Override @@ -94,9 +91,10 @@ public class BandcampPlaylistExtractor extends PlaylistExtractor { @Override public String getUploaderAvatarUrl() { try { - return document.getElementsByClass("band-photo").first().attr("src"); - } catch (NullPointerException e) { - return ""; + return Objects.requireNonNull(document.getElementsByClass("band-photo").first()) + .attr("src"); + } catch (final NullPointerException e) { + return EMPTY_STRING; } } @@ -110,24 +108,6 @@ public class BandcampPlaylistExtractor extends PlaylistExtractor { return trackInfo.size(); } - @Nonnull - @Override - public String getSubChannelName() { - return ""; - } - - @Nonnull - @Override - public String getSubChannelUrl() { - return ""; - } - - @Nonnull - @Override - public String getSubChannelAvatarUrl() { - return ""; - } - @Nonnull @Override public InfoItemsPage getInitialPage() throws ExtractionException { @@ -146,14 +126,13 @@ public class BandcampPlaylistExtractor extends PlaylistExtractor { collector.commit(new BandcampPlaylistStreamInfoItemExtractor( track, getUploaderUrl(), getThumbnailUrl())); } - } return new InfoItemsPage<>(collector, null); } @Override - public InfoItemsPage getPage(Page page) { + public InfoItemsPage getPage(final Page page) { return null; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubePlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubePlaylistExtractor.java index 89340df37..f8fd38b1d 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubePlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubePlaylistExtractor.java @@ -29,16 +29,12 @@ public class PeertubePlaylistExtractor extends PlaylistExtractor { super(service, linkHandler); } + @Nonnull @Override public String getThumbnailUrl() throws ParsingException { return getBaseUrl() + playlistInfo.getString("thumbnailPath"); } - @Override - public String getBannerUrl() { - return null; - } - @Override public String getUploaderUrl() { return playlistInfo.getObject("ownerAccount").getString("url"); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java index 2c4ac60fd..ffde3fb72 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java @@ -23,9 +23,9 @@ import java.util.ArrayList; import java.util.List; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL; +import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class SoundcloudPlaylistExtractor extends PlaylistExtractor { @@ -67,7 +67,7 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor { return playlist.getString("title"); } - @Nullable + @Nonnull @Override public String getThumbnailUrl() { String artworkUrl = playlist.getString("artwork_url"); @@ -80,24 +80,21 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor { for (final StreamInfoItem item : infoItems.getItems()) { artworkUrl = item.getThumbnailUrl(); - if (!isNullOrEmpty(artworkUrl)) break; + if (!isNullOrEmpty(artworkUrl)) { + break; + } } } catch (final Exception ignored) { } if (artworkUrl == null) { - return null; + return EMPTY_STRING; } } return artworkUrl.replace("large.jpg", "crop.jpg"); } - @Override - public String getBannerUrl() { - return null; - } - @Override public String getUploaderUrl() { return SoundcloudParsingHelper.getUploaderUrl(playlist); @@ -123,24 +120,6 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor { return playlist.getLong("track_count"); } - @Nonnull - @Override - public String getSubChannelName() { - return ""; - } - - @Nonnull - @Override - public String getSubChannelUrl() { - return ""; - } - - @Nonnull - @Override - public String getSubChannelAvatarUrl() { - return ""; - } - @Nonnull @Override public InfoItemsPage getInitialPage() { @@ -149,18 +128,18 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor { final List ids = new ArrayList<>(); final JsonArray tracks = playlist.getArray("tracks"); - for (final Object o : tracks) { - if (o instanceof JsonObject) { - final JsonObject track = (JsonObject) o; - if (track.has("title")) { // i.e. if full info is available - streamInfoItemsCollector.commit(new SoundcloudStreamInfoItemExtractor(track)); - } else { - // %09d would be enough, but a 0 before the number does not create problems, so - // let's be sure - ids.add(String.format("%010d", track.getInt("id"))); - } - } - } + tracks.stream().filter(JsonObject.class::isInstance) + .map(JsonObject.class::cast) + .forEachOrdered(track -> { + if (track.has("title")) { // i.e. if full info is available + streamInfoItemsCollector.commit( + new SoundcloudStreamInfoItemExtractor(track)); + } else { + // %09d would be enough, but a 0 before the number does not create + // problems, so let's be sure + ids.add(String.format("%010d", track.getInt("id"))); + } + }); return new InfoItemsPage<>(streamInfoItemsCollector, new Page(ids)); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.java index d37320dfc..0a0431392 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.java @@ -23,6 +23,7 @@ import org.schabi.newpipe.extractor.utils.JsonUtils; import java.io.IOException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.*; import javax.annotation.Nonnull; @@ -71,7 +72,7 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor { jsonBody.value("playlistIndex", Integer.parseInt(playlistIndexString)); } - final byte[] body = JsonWriter.string(jsonBody.done()).getBytes(UTF_8); + final byte[] body = JsonWriter.string(jsonBody.done()).getBytes(StandardCharsets.UTF_8); final Map> headers = new HashMap<>(); addClientInfoHeaders(headers); @@ -97,6 +98,7 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor { return name; } + @Nonnull @Override public String getThumbnailUrl() throws ParsingException { try { @@ -108,19 +110,15 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor { .getObject("watchEndpoint").getString("videoId")); } catch (final Exception ignored) { } + throw new ParsingException("Could not get playlist thumbnail", e); } } - @Override - public String getBannerUrl() { - return ""; - } - @Override public String getUploaderUrl() { // YouTube mixes are auto-generated by YouTube - return ""; + return EMPTY_STRING; } @Override @@ -132,7 +130,7 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor { @Override public String getUploaderAvatarUrl() { // YouTube mixes are auto-generated by YouTube - return ""; + return EMPTY_STRING; } @Override @@ -148,8 +146,8 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor { @Nonnull @Override - public InfoItemsPage getInitialPage() throws IOException, - ExtractionException { + public InfoItemsPage getInitialPage() + throws IOException, ExtractionException { final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); collectStreamsFrom(collector, playlistData.getArray("contents")); @@ -159,9 +157,10 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor { return new InfoItemsPage<>(collector, getNextPageFrom(playlistData, cookies)); } - private Page getNextPageFrom(final JsonObject playlistJson, - final Map cookies) throws IOException, - ExtractionException { + @Nonnull + private Page getNextPageFrom(@Nonnull final JsonObject playlistJson, + final Map cookies) + throws IOException, ExtractionException { final JsonObject lastStream = ((JsonObject) playlistJson.getArray("contents") .get(playlistJson.getArray("contents").size() - 1)); if (lastStream == null || lastStream.getObject("playlistPanelVideoRenderer") == null) { @@ -181,7 +180,7 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor { .value("playlistIndex", index) .value("params", params) .done()) - .getBytes(UTF_8); + .getBytes(StandardCharsets.UTF_8); return new Page(YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, cookies, body); } @@ -217,26 +216,23 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor { private void collectStreamsFrom(@Nonnull final StreamInfoItemsCollector collector, @Nullable final List streams) { - if (streams == null) { return; } final TimeAgoParser timeAgoParser = getTimeAgoParser(); - for (final Object stream : streams) { - if (stream instanceof JsonObject) { - final JsonObject streamInfo = ((JsonObject) stream) - .getObject("playlistPanelVideoRenderer"); - if (streamInfo != null) { - collector.commit(new YoutubeStreamInfoItemExtractor(streamInfo, - timeAgoParser)); - } - } - } + streams.stream() + .filter(JsonObject.class::isInstance) + .map(stream -> ((JsonObject) stream) + .getObject("playlistPanelVideoRenderer")) + .filter(Objects::nonNull) + .map(streamInfo -> new YoutubeStreamInfoItemExtractor(streamInfo, timeAgoParser)) + .forEachOrdered(collector::commit); } - private String getThumbnailUrlFromPlaylistId(final String playlistId) throws ParsingException { + @Nonnull + private String getThumbnailUrlFromPlaylistId(@Nonnull final String playlistId) throws ParsingException { final String videoId; if (playlistId.startsWith("RDMM")) { videoId = playlistId.substring(4); @@ -251,25 +247,8 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor { return getThumbnailUrlFromVideoId(videoId); } + @Nonnull private String getThumbnailUrlFromVideoId(final String videoId) { return "https://i.ytimg.com/vi/" + videoId + "/hqdefault.jpg"; } - - @Nonnull - @Override - public String getSubChannelName() { - return ""; - } - - @Nonnull - @Override - public String getSubChannelUrl() { - return ""; - } - - @Nonnull - @Override - public String getSubChannelAvatarUrl() { - return ""; - } }