|
13 | 13 | import org.schabi.newpipe.extractor.exceptions.*; |
14 | 14 | import org.schabi.newpipe.extractor.localization.ContentCountry; |
15 | 15 | import org.schabi.newpipe.extractor.localization.Localization; |
| 16 | +import org.schabi.newpipe.extractor.playlist.PlaylistInfo; |
16 | 17 | import org.schabi.newpipe.extractor.stream.Description; |
17 | 18 | import org.schabi.newpipe.extractor.utils.JsonUtils; |
18 | 19 | import org.schabi.newpipe.extractor.utils.Parser; |
@@ -246,7 +247,8 @@ public static OffsetDateTime parseDateFrom(final String textualUploadDate) |
246 | 247 | * @return Whether given id belongs to a YouTube Mix |
247 | 248 | */ |
248 | 249 | public static boolean isYoutubeMixId(@Nonnull final String playlistId) { |
249 | | - return playlistId.startsWith("RD") && !isYoutubeMusicMixId(playlistId); |
| 250 | + return playlistId.startsWith("RD") |
| 251 | + && !isYoutubeMusicMixId(playlistId); |
250 | 252 | } |
251 | 253 |
|
252 | 254 | /** |
@@ -282,28 +284,57 @@ public static boolean isYoutubeChannelMixId(@Nonnull final String playlistId) { |
282 | 284 | } |
283 | 285 |
|
284 | 286 | /** |
285 | | - * @return the video id extracted from the playlist id for Mixes |
286 | | - * @throws ParsingException If the playlistId is a Channel Mix or not a mix. |
| 287 | + * Checks if the given playlist id is a YouTube Genre Mix (auto-generated playlist) |
| 288 | + * Ids from a YouTube Genre Mix start with "RDGMEM" |
| 289 | + * |
| 290 | + * @return Whether given id belongs to a YouTube Genre Mix |
| 291 | + */ |
| 292 | + public static boolean isYoutubeGenreMixId(@Nonnull final String playlistId) { |
| 293 | + return playlistId.startsWith("RDGMEM"); |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * @param playlistId the playlist id to parse |
| 298 | + * @return the {@link PlaylistInfo.PlaylistType} extracted from the playlistId (mix playlist |
| 299 | + * types included) |
| 300 | + * @throws ParsingException if the playlistId is null or empty, if the playlistId is not a mix, |
| 301 | + * if it is a mix but it's not based on a specific stream (this is the |
| 302 | + * case for channel or genre mixes) |
287 | 303 | */ |
288 | 304 | @Nonnull |
289 | | - public static String extractVideoIdFromMixId(@Nonnull final String playlistId) |
| 305 | + public static String extractVideoIdFromMixId(final String playlistId) |
290 | 306 | throws ParsingException { |
291 | | - if (isYoutubeMyMixId(playlistId)) { |
| 307 | + if (isNullOrEmpty(playlistId)) { |
| 308 | + throw new ParsingException("Video id could not be determined from empty playlist id"); |
| 309 | + |
| 310 | + } else if (isYoutubeMyMixId(playlistId)) { |
292 | 311 | return playlistId.substring(4); |
293 | 312 |
|
294 | 313 | } else if (isYoutubeMusicMixId(playlistId)) { |
295 | 314 | return playlistId.substring(6); |
296 | 315 |
|
297 | 316 | } else if (isYoutubeChannelMixId(playlistId)) { |
298 | | - // Channel mix are build with RMCM{channelId}, so videoId can't be determined |
299 | | - throw new ParsingException("Video id could not be determined from mix id: " |
| 317 | + // Channel mixes are of the form RMCM{channelId}, so videoId can't be determined |
| 318 | + throw new ParsingException("Video id could not be determined from channel mix id: " |
| 319 | + + playlistId); |
| 320 | + |
| 321 | + } else if (isYoutubeGenreMixId(playlistId)) { |
| 322 | + // Genre mixes are of the form RDGMEM{garbage}, so videoId can't be determined |
| 323 | + throw new ParsingException("Video id could not be determined from genre mix id: " |
300 | 324 | + playlistId); |
301 | 325 |
|
302 | 326 | } else if (isYoutubeMixId(playlistId)) { // normal mix |
| 327 | + if (playlistId.length() != 13) { |
| 328 | + // Stream YouTube mixes are of the form RD{videoId}, but if videoId is not exactly |
| 329 | + // 11 characters then it can't be a video id, hence we are dealing with a different |
| 330 | + // type of mix (e.g. genre mixes handled above, of the form RDGMEM{garbage}) |
| 331 | + throw new ParsingException("Video id could not be determined from mix id: " |
| 332 | + + playlistId); |
| 333 | + } |
303 | 334 | return playlistId.substring(2); |
304 | 335 |
|
305 | 336 | } else { // not a mix |
306 | | - throw new ParsingException("Video id could not be determined from mix id: " |
| 337 | + throw new ParsingException("Video id could not be determined from playlist id: " |
307 | 338 | + playlistId); |
308 | 339 | } |
309 | 340 | } |
|
0 commit comments