2022-02-02 20:23:11 +01:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
|
|
|
|
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
|
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getThumbnailUrlFromInfoItem;
|
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isYoutubeChannelMixId;
|
2022-02-17 17:19:54 +01:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isYoutubeGenreMixId;
|
2022-02-02 20:23:11 +01:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isYoutubeMusicMixId;
|
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
|
|
|
|
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.extractor.ListExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
|
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
|
|
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
|
|
|
public class YoutubeMixPlaylistInfoItemExtractor implements PlaylistInfoItemExtractor {
|
|
|
|
private final JsonObject mixInfoItem;
|
|
|
|
|
|
|
|
public YoutubeMixPlaylistInfoItemExtractor(final JsonObject mixInfoItem) {
|
|
|
|
this.mixInfoItem = mixInfoItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() throws ParsingException {
|
|
|
|
final String name = getTextFromObject(mixInfoItem.getObject("title"));
|
|
|
|
if (isNullOrEmpty(name)) {
|
|
|
|
throw new ParsingException("Could not get name");
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUrl() throws ParsingException {
|
|
|
|
final String url = mixInfoItem.getString("shareUrl");
|
|
|
|
if (isNullOrEmpty(url)) {
|
|
|
|
throw new ParsingException("Could not get url");
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getThumbnailUrl() throws ParsingException {
|
|
|
|
return getThumbnailUrlFromInfoItem(mixInfoItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderName() throws ParsingException {
|
|
|
|
// YouTube mixes are auto-generated by YouTube
|
|
|
|
return "YouTube";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getStreamCount() throws ParsingException {
|
|
|
|
// Auto-generated playlists always start with 25 videos and are endless
|
|
|
|
return ListExtractor.ITEM_COUNT_INFINITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public PlaylistInfo.PlaylistType getPlaylistType() throws ParsingException {
|
|
|
|
try {
|
|
|
|
final String url = getUrl();
|
|
|
|
final String mixPlaylistId = Utils.getQueryValue(Utils.stringToURL(url), "list");
|
|
|
|
if (isNullOrEmpty(mixPlaylistId)) {
|
|
|
|
throw new ParsingException("Mix playlist id was null or empty for url " + url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isYoutubeMusicMixId(mixPlaylistId)) {
|
|
|
|
return PlaylistInfo.PlaylistType.MIX_MUSIC;
|
|
|
|
} else if (isYoutubeChannelMixId(mixPlaylistId)) {
|
|
|
|
return PlaylistInfo.PlaylistType.MIX_CHANNEL;
|
2022-02-17 17:19:54 +01:00
|
|
|
} else if (isYoutubeGenreMixId(mixPlaylistId)) {
|
|
|
|
return PlaylistInfo.PlaylistType.MIX_GENRE;
|
2022-02-02 20:23:11 +01:00
|
|
|
} else {
|
|
|
|
// either a normal mix based on a stream, or a "my mix" (still based on a stream)
|
2022-02-17 17:19:54 +01:00
|
|
|
// note: if YouTube introduces even more types of mixes, they will default to this
|
2022-02-02 20:23:11 +01:00
|
|
|
return PlaylistInfo.PlaylistType.MIX_STREAM;
|
|
|
|
}
|
|
|
|
} catch (final MalformedURLException e) {
|
|
|
|
throw new ParsingException("Could not obtain mix playlist id", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|