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.
This commit is contained in:
TiA4f8R 2022-03-12 13:16:00 +01:00
parent fc6b45ee36
commit 58a247907e
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
4 changed files with 54 additions and 121 deletions

View File

@ -19,10 +19,13 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; 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.services.bandcamp.extractors.BandcampExtractorHelper.getImageUrl;
import static org.schabi.newpipe.extractor.utils.JsonUtils.getJsonData; 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.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 { public class BandcampPlaylistExtractor extends PlaylistExtractor {
@ -57,33 +60,27 @@ public class BandcampPlaylistExtractor extends PlaylistExtractor {
throw new ParsingException("JSON does not exist", e); throw new ParsingException("JSON does not exist", e);
} }
if (trackInfo.isEmpty()) {
if (trackInfo.size() <= 0) {
// Albums without trackInfo need to be purchased before they can be played // Albums without trackInfo need to be purchased before they can be played
throw new ContentNotAvailableException("Album needs to be purchased"); throw new ContentNotAvailableException("Album needs to be purchased");
} }
} }
@Nonnull
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
if (albumJson.isNull("art_id")) { if (albumJson.isNull("art_id")) {
return ""; return EMPTY_STRING;
} else { } else {
return getImageUrl(albumJson.getLong("art_id"), true); return getImageUrl(albumJson.getLong("art_id"), true);
} }
} }
@Override
public String getBannerUrl() {
return "";
}
@Override @Override
public String getUploaderUrl() throws ParsingException { public String getUploaderUrl() throws ParsingException {
final String[] parts = getUrl().split("/"); final String[] parts = getUrl().split("/");
// https: (/) (/) * .bandcamp.com (/) and leave out the rest // https: (/) (/) * .bandcamp.com (/) and leave out the rest
return "https://" + parts[2] + "/"; return HTTPS + parts[2] + "/";
} }
@Override @Override
@ -94,9 +91,10 @@ public class BandcampPlaylistExtractor extends PlaylistExtractor {
@Override @Override
public String getUploaderAvatarUrl() { public String getUploaderAvatarUrl() {
try { try {
return document.getElementsByClass("band-photo").first().attr("src"); return Objects.requireNonNull(document.getElementsByClass("band-photo").first())
} catch (NullPointerException e) { .attr("src");
return ""; } catch (final NullPointerException e) {
return EMPTY_STRING;
} }
} }
@ -110,24 +108,6 @@ public class BandcampPlaylistExtractor extends PlaylistExtractor {
return trackInfo.size(); return trackInfo.size();
} }
@Nonnull
@Override
public String getSubChannelName() {
return "";
}
@Nonnull
@Override
public String getSubChannelUrl() {
return "";
}
@Nonnull
@Override
public String getSubChannelAvatarUrl() {
return "";
}
@Nonnull @Nonnull
@Override @Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException { public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
@ -146,14 +126,13 @@ public class BandcampPlaylistExtractor extends PlaylistExtractor {
collector.commit(new BandcampPlaylistStreamInfoItemExtractor( collector.commit(new BandcampPlaylistStreamInfoItemExtractor(
track, getUploaderUrl(), getThumbnailUrl())); track, getUploaderUrl(), getThumbnailUrl()));
} }
} }
return new InfoItemsPage<>(collector, null); return new InfoItemsPage<>(collector, null);
} }
@Override @Override
public InfoItemsPage<StreamInfoItem> getPage(Page page) { public InfoItemsPage<StreamInfoItem> getPage(final Page page) {
return null; return null;
} }

View File

@ -29,16 +29,12 @@ public class PeertubePlaylistExtractor extends PlaylistExtractor {
super(service, linkHandler); super(service, linkHandler);
} }
@Nonnull
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
return getBaseUrl() + playlistInfo.getString("thumbnailPath"); return getBaseUrl() + playlistInfo.getString("thumbnailPath");
} }
@Override
public String getBannerUrl() {
return null;
}
@Override @Override
public String getUploaderUrl() { public String getUploaderUrl() {
return playlistInfo.getObject("ownerAccount").getString("url"); return playlistInfo.getObject("ownerAccount").getString("url");

View File

@ -23,9 +23,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull; 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.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; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class SoundcloudPlaylistExtractor extends PlaylistExtractor { public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
@ -67,7 +67,7 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
return playlist.getString("title"); return playlist.getString("title");
} }
@Nullable @Nonnull
@Override @Override
public String getThumbnailUrl() { public String getThumbnailUrl() {
String artworkUrl = playlist.getString("artwork_url"); String artworkUrl = playlist.getString("artwork_url");
@ -80,24 +80,21 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
for (final StreamInfoItem item : infoItems.getItems()) { for (final StreamInfoItem item : infoItems.getItems()) {
artworkUrl = item.getThumbnailUrl(); artworkUrl = item.getThumbnailUrl();
if (!isNullOrEmpty(artworkUrl)) break; if (!isNullOrEmpty(artworkUrl)) {
break;
}
} }
} catch (final Exception ignored) { } catch (final Exception ignored) {
} }
if (artworkUrl == null) { if (artworkUrl == null) {
return null; return EMPTY_STRING;
} }
} }
return artworkUrl.replace("large.jpg", "crop.jpg"); return artworkUrl.replace("large.jpg", "crop.jpg");
} }
@Override
public String getBannerUrl() {
return null;
}
@Override @Override
public String getUploaderUrl() { public String getUploaderUrl() {
return SoundcloudParsingHelper.getUploaderUrl(playlist); return SoundcloudParsingHelper.getUploaderUrl(playlist);
@ -123,24 +120,6 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
return playlist.getLong("track_count"); return playlist.getLong("track_count");
} }
@Nonnull
@Override
public String getSubChannelName() {
return "";
}
@Nonnull
@Override
public String getSubChannelUrl() {
return "";
}
@Nonnull
@Override
public String getSubChannelAvatarUrl() {
return "";
}
@Nonnull @Nonnull
@Override @Override
public InfoItemsPage<StreamInfoItem> getInitialPage() { public InfoItemsPage<StreamInfoItem> getInitialPage() {
@ -149,18 +128,18 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
final List<String> ids = new ArrayList<>(); final List<String> ids = new ArrayList<>();
final JsonArray tracks = playlist.getArray("tracks"); final JsonArray tracks = playlist.getArray("tracks");
for (final Object o : tracks) { tracks.stream().filter(JsonObject.class::isInstance)
if (o instanceof JsonObject) { .map(JsonObject.class::cast)
final JsonObject track = (JsonObject) o; .forEachOrdered(track -> {
if (track.has("title")) { // i.e. if full info is available if (track.has("title")) { // i.e. if full info is available
streamInfoItemsCollector.commit(new SoundcloudStreamInfoItemExtractor(track)); streamInfoItemsCollector.commit(
} else { new SoundcloudStreamInfoItemExtractor(track));
// %09d would be enough, but a 0 before the number does not create problems, so } else {
// let's be sure // %09d would be enough, but a 0 before the number does not create
ids.add(String.format("%010d", track.getInt("id"))); // problems, so let's be sure
} ids.add(String.format("%010d", track.getInt("id")));
} }
} });
return new InfoItemsPage<>(streamInfoItemsCollector, new Page(ids)); return new InfoItemsPage<>(streamInfoItemsCollector, new Page(ids));
} }

View File

@ -23,6 +23,7 @@ import org.schabi.newpipe.extractor.utils.JsonUtils;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -71,7 +72,7 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
jsonBody.value("playlistIndex", Integer.parseInt(playlistIndexString)); 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<String, List<String>> headers = new HashMap<>(); final Map<String, List<String>> headers = new HashMap<>();
addClientInfoHeaders(headers); addClientInfoHeaders(headers);
@ -97,6 +98,7 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
return name; return name;
} }
@Nonnull
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
try { try {
@ -108,19 +110,15 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
.getObject("watchEndpoint").getString("videoId")); .getObject("watchEndpoint").getString("videoId"));
} catch (final Exception ignored) { } catch (final Exception ignored) {
} }
throw new ParsingException("Could not get playlist thumbnail", e); throw new ParsingException("Could not get playlist thumbnail", e);
} }
} }
@Override
public String getBannerUrl() {
return "";
}
@Override @Override
public String getUploaderUrl() { public String getUploaderUrl() {
// YouTube mixes are auto-generated by YouTube // YouTube mixes are auto-generated by YouTube
return ""; return EMPTY_STRING;
} }
@Override @Override
@ -132,7 +130,7 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
@Override @Override
public String getUploaderAvatarUrl() { public String getUploaderAvatarUrl() {
// YouTube mixes are auto-generated by YouTube // YouTube mixes are auto-generated by YouTube
return ""; return EMPTY_STRING;
} }
@Override @Override
@ -148,8 +146,8 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
@Nonnull @Nonnull
@Override @Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, public InfoItemsPage<StreamInfoItem> getInitialPage()
ExtractionException { throws IOException, ExtractionException {
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
collectStreamsFrom(collector, playlistData.getArray("contents")); collectStreamsFrom(collector, playlistData.getArray("contents"));
@ -159,9 +157,10 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
return new InfoItemsPage<>(collector, getNextPageFrom(playlistData, cookies)); return new InfoItemsPage<>(collector, getNextPageFrom(playlistData, cookies));
} }
private Page getNextPageFrom(final JsonObject playlistJson, @Nonnull
final Map<String, String> cookies) throws IOException, private Page getNextPageFrom(@Nonnull final JsonObject playlistJson,
ExtractionException { final Map<String, String> cookies)
throws IOException, ExtractionException {
final JsonObject lastStream = ((JsonObject) playlistJson.getArray("contents") final JsonObject lastStream = ((JsonObject) playlistJson.getArray("contents")
.get(playlistJson.getArray("contents").size() - 1)); .get(playlistJson.getArray("contents").size() - 1));
if (lastStream == null || lastStream.getObject("playlistPanelVideoRenderer") == null) { if (lastStream == null || lastStream.getObject("playlistPanelVideoRenderer") == null) {
@ -181,7 +180,7 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
.value("playlistIndex", index) .value("playlistIndex", index)
.value("params", params) .value("params", params)
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
return new Page(YOUTUBEI_V1_URL + "next?key=" + getKey(), null, null, cookies, body); 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, private void collectStreamsFrom(@Nonnull final StreamInfoItemsCollector collector,
@Nullable final List<Object> streams) { @Nullable final List<Object> streams) {
if (streams == null) { if (streams == null) {
return; return;
} }
final TimeAgoParser timeAgoParser = getTimeAgoParser(); final TimeAgoParser timeAgoParser = getTimeAgoParser();
for (final Object stream : streams) { streams.stream()
if (stream instanceof JsonObject) { .filter(JsonObject.class::isInstance)
final JsonObject streamInfo = ((JsonObject) stream) .map(stream -> ((JsonObject) stream)
.getObject("playlistPanelVideoRenderer"); .getObject("playlistPanelVideoRenderer"))
if (streamInfo != null) { .filter(Objects::nonNull)
collector.commit(new YoutubeStreamInfoItemExtractor(streamInfo, .map(streamInfo -> new YoutubeStreamInfoItemExtractor(streamInfo, timeAgoParser))
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; final String videoId;
if (playlistId.startsWith("RDMM")) { if (playlistId.startsWith("RDMM")) {
videoId = playlistId.substring(4); videoId = playlistId.substring(4);
@ -251,25 +247,8 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
return getThumbnailUrlFromVideoId(videoId); return getThumbnailUrlFromVideoId(videoId);
} }
@Nonnull
private String getThumbnailUrlFromVideoId(final String videoId) { private String getThumbnailUrlFromVideoId(final String videoId) {
return "https://i.ytimg.com/vi/" + videoId + "/hqdefault.jpg"; 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 "";
}
} }