Skip to content

Commit 401082a

Browse files
committed
[YouTube] Extract playlist type in playlist extractor
1 parent 63ed06a commit 401082a

4 files changed

Lines changed: 61 additions & 27 deletions

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,51 @@ public static String extractVideoIdFromMixId(final String playlistId)
339339
}
340340
}
341341

342+
/**
343+
* @param playlistId the playlist id to parse
344+
* @return the {@link PlaylistInfo.PlaylistType} extracted from the playlistId (mix playlist
345+
* types included)
346+
* @throws ParsingException if the playlistId is null or empty
347+
*/
348+
@Nonnull
349+
public static PlaylistInfo.PlaylistType extractPlaylistTypeFromPlaylistId(
350+
final String playlistId) throws ParsingException {
351+
if (isNullOrEmpty(playlistId)) {
352+
throw new ParsingException("Could not extract playlist type from empty playlist id");
353+
} else if (isYoutubeMusicMixId(playlistId)) {
354+
return PlaylistInfo.PlaylistType.MIX_MUSIC;
355+
} else if (isYoutubeChannelMixId(playlistId)) {
356+
return PlaylistInfo.PlaylistType.MIX_CHANNEL;
357+
} else if (isYoutubeGenreMixId(playlistId)) {
358+
return PlaylistInfo.PlaylistType.MIX_GENRE;
359+
} else if (isYoutubeMixId(playlistId)) { // normal mix
360+
// Either a normal mix based on a stream, or a "my mix" (still based on a stream).
361+
// NOTE: if YouTube introduces even more types of mixes that still start with RD,
362+
// they will default to this, even though they might not be based on a stream.
363+
return PlaylistInfo.PlaylistType.MIX_STREAM;
364+
} else {
365+
// not a known type of mix: just consider it a normal playlist
366+
return PlaylistInfo.PlaylistType.NORMAL;
367+
}
368+
}
369+
370+
/**
371+
* @param playlistUrl the playlist url to parse
372+
* @return the {@link PlaylistInfo.PlaylistType} extracted from the playlistUrl's list param
373+
* (mix playlist types included)
374+
* @throws ParsingException if the playlistUrl is malformed, if has no list param or if the list
375+
* param is empty
376+
*/
377+
public static PlaylistInfo.PlaylistType extractPlaylistTypeFromPlaylistUrl(
378+
final String playlistUrl) throws ParsingException {
379+
try {
380+
return extractPlaylistTypeFromPlaylistId(
381+
Utils.getQueryValue(Utils.stringToURL(playlistUrl), "list"));
382+
} catch (final MalformedURLException e) {
383+
throw new ParsingException("Could not extract playlist type from malformed url", e);
384+
}
385+
}
386+
342387
public static JsonObject getInitialData(final String html) throws ParsingException {
343388
try {
344389
try {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.schabi.newpipe.extractor.localization.Localization;
1717
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
1818
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
19+
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
1920
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
2021
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
2122
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
@@ -241,4 +242,10 @@ private String getThumbnailUrlFromPlaylistId(@Nonnull final String playlistId)
241242
private String getThumbnailUrlFromVideoId(final String videoId) {
242243
return "https://i.ytimg.com/vi/" + videoId + "/hqdefault.jpg";
243244
}
245+
246+
@Nonnull
247+
@Override
248+
public PlaylistInfo.PlaylistType getPlaylistType() throws ParsingException {
249+
return extractPlaylistTypeFromPlaylistId(playlistData.getString("playlistId"));
250+
}
244251
}

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

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.schabi.newpipe.extractor.services.youtube.extractors;
22

3+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractPlaylistTypeFromPlaylistUrl;
34
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
45
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getThumbnailUrlFromInfoItem;
5-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isYoutubeChannelMixId;
6-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isYoutubeGenreMixId;
7-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isYoutubeMusicMixId;
86
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
97

108
import com.grack.nanojson.JsonObject;
@@ -13,9 +11,6 @@
1311
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1412
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
1513
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor;
16-
import org.schabi.newpipe.extractor.utils.Utils;
17-
18-
import java.net.MalformedURLException;
1914

2015
import javax.annotation.Nonnull;
2116

@@ -64,26 +59,6 @@ public long getStreamCount() throws ParsingException {
6459
@Nonnull
6560
@Override
6661
public PlaylistInfo.PlaylistType getPlaylistType() throws ParsingException {
67-
try {
68-
final String url = getUrl();
69-
final String mixPlaylistId = Utils.getQueryValue(Utils.stringToURL(url), "list");
70-
if (isNullOrEmpty(mixPlaylistId)) {
71-
throw new ParsingException("Mix playlist id was null or empty for url " + url);
72-
}
73-
74-
if (isYoutubeMusicMixId(mixPlaylistId)) {
75-
return PlaylistInfo.PlaylistType.MIX_MUSIC;
76-
} else if (isYoutubeChannelMixId(mixPlaylistId)) {
77-
return PlaylistInfo.PlaylistType.MIX_CHANNEL;
78-
} else if (isYoutubeGenreMixId(mixPlaylistId)) {
79-
return PlaylistInfo.PlaylistType.MIX_GENRE;
80-
} else {
81-
// either a normal mix based on a stream, or a "my mix" (still based on a stream)
82-
// note: if YouTube introduces even more types of mixes, they will default to this
83-
return PlaylistInfo.PlaylistType.MIX_STREAM;
84-
}
85-
} catch (final MalformedURLException e) {
86-
throw new ParsingException("Could not obtain mix playlist id", e);
87-
}
62+
return extractPlaylistTypeFromPlaylistUrl(getUrl());
8863
}
8964
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.schabi.newpipe.extractor.localization.Localization;
1515
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
1616
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
17+
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
1718
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
1819
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1920
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
@@ -329,4 +330,10 @@ public long getViewCount() {
329330
})
330331
.forEachOrdered(collector::commit);
331332
}
333+
334+
@Nonnull
335+
@Override
336+
public PlaylistInfo.PlaylistType getPlaylistType() throws ParsingException {
337+
return extractPlaylistTypeFromPlaylistUrl(getUrl());
338+
}
332339
}

0 commit comments

Comments
 (0)