2018-07-13 18:02:40 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.linkHandler;
|
2017-03-12 16:15:51 +01:00
|
|
|
|
2020-09-26 11:22:24 +02:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.List;
|
2020-03-30 11:48:00 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException;
|
2017-03-12 16:15:51 +01:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2020-02-02 14:19:48 +01:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
|
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
2019-01-13 12:52:07 +01:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
|
2020-04-10 10:51:05 +02:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
|
2019-01-13 12:52:07 +01:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2017-03-12 16:15:51 +01:00
|
|
|
|
2022-03-18 15:09:06 +01:00
|
|
|
public final class YoutubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory {
|
2017-03-12 16:15:51 +01:00
|
|
|
|
2020-04-16 19:28:27 +02:00
|
|
|
private static final YoutubePlaylistLinkHandlerFactory INSTANCE =
|
|
|
|
new YoutubePlaylistLinkHandlerFactory();
|
2017-03-12 16:15:51 +01:00
|
|
|
|
2022-03-18 15:09:06 +01:00
|
|
|
private YoutubePlaylistLinkHandlerFactory() {
|
|
|
|
}
|
|
|
|
|
2018-07-13 18:02:40 +02:00
|
|
|
public static YoutubePlaylistLinkHandlerFactory getInstance() {
|
2020-04-16 19:28:27 +02:00
|
|
|
return INSTANCE;
|
2017-07-11 05:08:03 +02:00
|
|
|
}
|
|
|
|
|
2017-03-12 16:15:51 +01:00
|
|
|
@Override
|
2020-04-16 19:28:27 +02:00
|
|
|
public String getUrl(final String id, final List<String> contentFilters,
|
|
|
|
final String sortFilter) {
|
2017-08-07 18:12:51 +02:00
|
|
|
return "https://www.youtube.com/playlist?list=" + id;
|
2017-03-12 16:15:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-16 19:28:27 +02:00
|
|
|
public String getId(final String url) throws ParsingException {
|
2017-03-12 16:15:51 +01:00
|
|
|
try {
|
2020-04-16 19:28:27 +02:00
|
|
|
final URL urlObj = Utils.stringToURL(url);
|
2019-01-13 12:52:07 +01:00
|
|
|
|
2019-01-27 01:44:46 +01:00
|
|
|
if (!Utils.isHTTP(urlObj) || !(YoutubeParsingHelper.isYoutubeURL(urlObj)
|
|
|
|
|| YoutubeParsingHelper.isInvidioURL(urlObj))) {
|
2020-12-24 10:33:26 +01:00
|
|
|
throw new ParsingException("the url given is not a YouTube-URL");
|
2019-01-13 12:52:07 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 19:28:27 +02:00
|
|
|
final String path = urlObj.getPath();
|
2020-02-08 23:58:46 +01:00
|
|
|
if (!path.equals("/watch") && !path.equals("/playlist")) {
|
2019-01-27 12:05:36 +01:00
|
|
|
throw new ParsingException("the url given is neither a video nor a playlist URL");
|
|
|
|
}
|
|
|
|
|
2020-04-16 19:28:27 +02:00
|
|
|
final String listID = Utils.getQueryValue(urlObj, "list");
|
2019-01-13 12:52:07 +01:00
|
|
|
|
|
|
|
if (listID == null) {
|
2020-12-24 10:33:26 +01:00
|
|
|
throw new ParsingException("the URL given does not include a playlist");
|
2019-01-13 12:52:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!listID.matches("[a-zA-Z0-9_-]{10,}")) {
|
2020-04-16 19:28:27 +02:00
|
|
|
throw new ParsingException(
|
|
|
|
"the list-ID given in the URL does not match the list pattern");
|
2019-01-13 12:52:07 +01:00
|
|
|
}
|
|
|
|
|
2020-09-26 11:22:24 +02:00
|
|
|
if (YoutubeParsingHelper.isYoutubeChannelMixId(listID)
|
|
|
|
&& Utils.getQueryValue(urlObj, "v") == null) {
|
2022-03-18 15:09:06 +01:00
|
|
|
// Video id can't be determined from the channel mix id.
|
|
|
|
// See YoutubeParsingHelper#extractVideoIdFromMixId
|
|
|
|
throw new ContentNotSupportedException(
|
|
|
|
"Channel Mix without a video id are not supported");
|
2020-09-26 11:22:24 +02:00
|
|
|
}
|
|
|
|
|
2019-01-13 12:52:07 +01:00
|
|
|
return listID;
|
2017-03-12 16:15:51 +01:00
|
|
|
} catch (final Exception exception) {
|
2020-12-24 10:33:26 +01:00
|
|
|
throw new ParsingException("Error could not parse URL: " + exception.getMessage(),
|
2020-04-16 19:28:27 +02:00
|
|
|
exception);
|
2017-03-12 16:15:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-05-06 14:08:50 +02:00
|
|
|
public boolean onAcceptUrl(final String url) {
|
2019-01-13 12:52:07 +01:00
|
|
|
try {
|
2020-04-16 19:28:27 +02:00
|
|
|
getId(url);
|
2022-03-18 15:09:06 +01:00
|
|
|
} catch (final ParsingException e) {
|
2019-01-13 12:52:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-16 19:28:27 +02:00
|
|
|
return true;
|
2017-03-12 16:15:51 +01:00
|
|
|
}
|
2020-02-02 14:19:48 +01:00
|
|
|
|
2020-02-02 18:15:47 +01:00
|
|
|
/**
|
2022-03-18 15:09:06 +01:00
|
|
|
* If it is a mix (auto-generated playlist) URL, return a {@link LinkHandler} where the URL is
|
|
|
|
* like {@code https://youtube.com/watch?v=videoId&list=playlistId}
|
2020-02-02 18:15:47 +01:00
|
|
|
* <p>Otherwise use super</p>
|
|
|
|
*/
|
2020-02-02 14:19:48 +01:00
|
|
|
@Override
|
2020-04-16 19:28:27 +02:00
|
|
|
public ListLinkHandler fromUrl(final String url) throws ParsingException {
|
2020-02-02 14:19:48 +01:00
|
|
|
try {
|
2020-04-16 19:28:27 +02:00
|
|
|
final URL urlObj = Utils.stringToURL(url);
|
|
|
|
final String listID = Utils.getQueryValue(urlObj, "list");
|
2020-02-02 14:19:48 +01:00
|
|
|
if (listID != null && YoutubeParsingHelper.isYoutubeMixId(listID)) {
|
|
|
|
String videoID = Utils.getQueryValue(urlObj, "v");
|
2020-02-04 13:49:43 +01:00
|
|
|
if (videoID == null) {
|
2020-09-26 11:22:24 +02:00
|
|
|
videoID = YoutubeParsingHelper.extractVideoIdFromMixId(listID);
|
2020-02-04 13:49:43 +01:00
|
|
|
}
|
2020-04-16 19:28:27 +02:00
|
|
|
final String newUrl = "https://www.youtube.com/watch?v=" + videoID
|
|
|
|
+ "&list=" + listID;
|
|
|
|
return new ListLinkHandler(new LinkHandler(url, newUrl, listID),
|
|
|
|
getContentFilter(url),
|
|
|
|
getSortFilter(url));
|
2020-02-02 14:19:48 +01:00
|
|
|
}
|
2022-03-18 15:09:06 +01:00
|
|
|
} catch (final MalformedURLException exception) {
|
2020-12-24 10:33:26 +01:00
|
|
|
throw new ParsingException("Error could not parse URL: " + exception.getMessage(),
|
2020-02-02 14:19:48 +01:00
|
|
|
exception);
|
|
|
|
}
|
|
|
|
return super.fromUrl(url);
|
|
|
|
}
|
2017-03-12 16:15:51 +01:00
|
|
|
}
|