NewPipeExtractor/extractor/src/main/java/org/schabi/newpipe/extractor/StreamingService.java

332 lines
13 KiB
Java
Raw Normal View History

2017-03-01 18:47:52 +01:00
package org.schabi.newpipe.extractor;
2018-08-20 00:52:19 +02:00
import java.util.Collections;
import java.util.List;
2017-08-11 20:21:49 +02:00
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
2018-08-20 00:52:19 +02:00
import org.schabi.newpipe.extractor.comments.CommentsExtractor;
2017-03-01 18:47:52 +01:00
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
2017-08-12 17:29:28 +02:00
import org.schabi.newpipe.extractor.kiosk.KioskList;
2018-08-20 00:52:19 +02:00
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
2018-05-13 21:28:51 +02:00
import org.schabi.newpipe.extractor.search.SearchExtractor;
import org.schabi.newpipe.extractor.stream.StreamExtractor;
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
2018-09-15 21:47:53 +02:00
import org.schabi.newpipe.extractor.utils.Localization;
2017-03-01 18:47:52 +01:00
2018-11-10 10:50:13 +01:00
/*
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
* StreamingService.java is part of NewPipe.
*
* NewPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
2017-03-01 18:47:52 +01:00
public abstract class StreamingService {
2018-11-10 10:50:13 +01:00
/**
* This class holds meta information about the service implementation.
*/
public static class ServiceInfo {
2018-10-11 21:10:22 +02:00
private String name;
private final List<MediaCapability> mediaCapabilities;
2017-08-06 22:20:15 +02:00
2018-11-10 10:50:13 +01:00
/**
* Creates a new instance of a ServiceInfo
* @param name the name of the service
* @param mediaCapabilities the type of media this service can handle
*/
public ServiceInfo(String name, List<MediaCapability> mediaCapabilities) {
2017-08-06 22:20:15 +02:00
this.name = name;
this.mediaCapabilities = Collections.unmodifiableList(mediaCapabilities);
}
public String getName() {
return name;
}
2018-10-11 21:10:22 +02:00
public void setName(String name) {
this.name = name;
}
public List<MediaCapability> getMediaCapabilities() {
return mediaCapabilities;
}
public enum MediaCapability {
AUDIO, VIDEO, LIVE
2017-08-06 22:20:15 +02:00
}
2017-03-01 18:47:52 +01:00
}
2018-11-10 10:50:13 +01:00
/**
* LinkType will be used to determine which type of URL you are handling, and therefore which part
* of NewPipe should handle a certain URL.
*/
2017-03-01 18:47:52 +01:00
public enum LinkType {
NONE,
STREAM,
2017-08-11 03:23:09 +02:00
CHANNEL,
2017-03-01 18:47:52 +01:00
PLAYLIST
}
2017-08-06 22:20:15 +02:00
private final int serviceId;
private final ServiceInfo serviceInfo;
2017-03-01 18:47:52 +01:00
2018-11-10 10:50:13 +01:00
/**
* Creates a new Streaming service.
* If you Implement one do not set id within your implementation of this extractor, instead
* set the id when you put the extractor into
* <a href="https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/ServiceList.html">ServiceList</a>.
* All other parameters can be set directly from the overriding constructor.
* @param id the number of the service to identify him within the NewPipe frontend
* @param name the name of the service
* @param capabilities the type of media this service can handle
*/
public StreamingService(int id, String name, List<ServiceInfo.MediaCapability> capabilities) {
2017-08-06 22:20:15 +02:00
this.serviceId = id;
this.serviceInfo = new ServiceInfo(name, capabilities);
2017-08-06 22:20:15 +02:00
}
public final int getServiceId() {
return serviceId;
2017-03-01 18:47:52 +01:00
}
2017-08-06 22:20:15 +02:00
public ServiceInfo getServiceInfo() {
return serviceInfo;
}
@Override
public String toString() {
return serviceId + ":" + serviceInfo.getName();
}
////////////////////////////////////////////
// Url Id handler
////////////////////////////////////////////
2018-11-10 10:50:13 +01:00
/**
* Must return a new instance of an implementation of LinkHandlerFactory for streams.
* @return an instance of a LinkHandlerFactory for streams
*/
2018-08-05 14:14:36 +02:00
public abstract LinkHandlerFactory getStreamLHFactory();
2018-11-10 10:50:13 +01:00
/**
* Must return a new instance of an implementation of ListLinkHandlerFactory for channels.
* If support for channels is not given null must be returned.
* @return an instance of a ListLinkHandlerFactory for channels or null
*/
2018-08-05 14:14:36 +02:00
public abstract ListLinkHandlerFactory getChannelLHFactory();
2018-11-10 10:50:13 +01:00
/**
* Must return a new instance of an implementation of ListLinkHandlerFactory for playlists.
* If support for playlists is not given null must be returned.
* @return an instance of a ListLinkHandlerFactory for playlists or null
*/
2018-08-05 14:14:36 +02:00
public abstract ListLinkHandlerFactory getPlaylistLHFactory();
2018-11-10 10:50:13 +01:00
/**
* Must return an instance of an implementation of SearchQueryHandlerFactory.
* @return an instance of a SearchQueryHandlerFactory
*/
2018-08-05 14:14:36 +02:00
public abstract SearchQueryHandlerFactory getSearchQHFactory();
2018-08-20 00:52:19 +02:00
public abstract ListLinkHandlerFactory getCommentsLHFactory();
2018-02-26 16:19:58 +01:00
////////////////////////////////////////////
// Extractor
////////////////////////////////////////////
2018-11-10 10:50:13 +01:00
/**
* Must create a new instance of a SearchExtractor implementation.
* @param queryHandler specifies the keyword lock for, and the filters which should be applied.
* @param localization specifies the language/country for the extractor.
* @return a new SearchExtractor instance
*/
2018-09-15 21:47:53 +02:00
public abstract SearchExtractor getSearchExtractor(SearchQueryHandler queryHandler, Localization localization);
2018-11-10 10:50:13 +01:00
/**
* Must create a new instance of a SuggestionExtractor implementation.
* @param localization specifies the language/country for the extractor.
* @return a new SuggestionExtractor instance
*/
2018-09-15 21:47:53 +02:00
public abstract SuggestionExtractor getSuggestionExtractor(Localization localization);
2018-11-10 10:50:13 +01:00
/**
* Outdated or obsolete. null can be returned.
* @return just null
*/
public abstract SubscriptionExtractor getSubscriptionExtractor();
2018-09-15 21:47:53 +02:00
2018-11-10 10:50:13 +01:00
/**
* Must create a new instance of a KioskList implementation.
* @return a new KioskList instance
* @throws ExtractionException
*/
public abstract KioskList getKioskList() throws ExtractionException;
2018-09-15 21:47:53 +02:00
2018-11-10 10:50:13 +01:00
/**
* Must create a new instance of a ChannelExtractor implementation.
* @param linkHandler is pointing to the channel which should be handled by this new instance.
* @param localization specifies the language used for the request.
* @return a new ChannelExtractor
* @throws ExtractionException
*/
2018-09-15 21:47:53 +02:00
public abstract ChannelExtractor getChannelExtractor(ListLinkHandler linkHandler,
Localization localization) throws ExtractionException;
2018-11-10 10:50:13 +01:00
/**
* Must crete a new instance of a PlaylistExtractor implementation.
* @param linkHandler is pointing to the playlist which should be handled by this new instance.
* @param localization specifies the language used for the request.
* @return a new PlaylistExtractor
* @throws ExtractionException
*/
2018-09-15 21:47:53 +02:00
public abstract PlaylistExtractor getPlaylistExtractor(ListLinkHandler linkHandler,
Localization localization) throws ExtractionException;
2018-11-10 10:50:13 +01:00
/**
* Must create a new instance of a StreamExtractor implementation.
* @param linkHandler is pointing to the stream which should be handled by this new instance.
* @param localization specifies the language used for the request.
* @return a new StreamExtractor
* @throws ExtractionException
*/
2018-09-15 21:47:53 +02:00
public abstract StreamExtractor getStreamExtractor(LinkHandler linkHandler,
Localization localization) throws ExtractionException;
2018-09-29 09:49:00 +02:00
public abstract CommentsExtractor getCommentsExtractor(ListLinkHandler linkHandler,
Localization localization) throws ExtractionException;
2018-09-15 21:47:53 +02:00
////////////////////////////////////////////
// Extractor with default localization
////////////////////////////////////////////
2018-09-15 21:47:53 +02:00
public SearchExtractor getSearchExtractor(SearchQueryHandler queryHandler) {
return getSearchExtractor(queryHandler, NewPipe.getPreferredLocalization());
2018-09-15 21:47:53 +02:00
}
public SuggestionExtractor getSuggestionExtractor() {
return getSuggestionExtractor(NewPipe.getPreferredLocalization());
2018-09-15 21:47:53 +02:00
}
public ChannelExtractor getChannelExtractor(ListLinkHandler linkHandler) throws ExtractionException {
return getChannelExtractor(linkHandler, NewPipe.getPreferredLocalization());
2018-09-15 21:47:53 +02:00
}
2018-12-28 08:32:00 +01:00
2018-09-15 21:47:53 +02:00
public PlaylistExtractor getPlaylistExtractor(ListLinkHandler linkHandler) throws ExtractionException {
return getPlaylistExtractor(linkHandler, NewPipe.getPreferredLocalization());
2018-09-15 21:47:53 +02:00
}
public StreamExtractor getStreamExtractor(LinkHandler linkHandler) throws ExtractionException {
return getStreamExtractor(linkHandler, NewPipe.getPreferredLocalization());
2018-09-15 21:47:53 +02:00
}
2018-09-22 21:42:11 +02:00
2018-09-29 09:49:00 +02:00
public CommentsExtractor getCommentsExtractor(ListLinkHandler urlIdHandler) throws ExtractionException {
2018-10-19 16:47:26 +02:00
return getCommentsExtractor(urlIdHandler, NewPipe.getPreferredLocalization());
2018-09-29 09:49:00 +02:00
}
2018-09-15 21:47:53 +02:00
////////////////////////////////////////////
// Extractor without link handler
////////////////////////////////////////////
2018-09-15 21:47:53 +02:00
public SearchExtractor getSearchExtractor(String query,
List<String> contentFilter,
String sortFilter,
Localization localization) throws ExtractionException {
return getSearchExtractor(getSearchQHFactory()
.fromQuery(query,
contentFilter,
sortFilter),
localization);
}
2018-09-15 21:47:53 +02:00
public ChannelExtractor getChannelExtractor(String id,
List<String> contentFilter,
String sortFilter,
Localization localization) throws ExtractionException {
return getChannelExtractor(getChannelLHFactory().fromQuery(id, contentFilter, sortFilter), localization);
}
2018-09-15 21:47:53 +02:00
public PlaylistExtractor getPlaylistExtractor(String id,
List<String> contentFilter,
String sortFilter,
Localization localization) throws ExtractionException {
return getPlaylistExtractor(getPlaylistLHFactory()
.fromQuery(id,
contentFilter,
sortFilter),
localization);
}
2018-09-15 21:47:53 +02:00
////////////////////////////////////////////
// Short extractor without localization
////////////////////////////////////////////
public SearchExtractor getSearchExtractor(String query) throws ExtractionException {
return getSearchExtractor(getSearchQHFactory().fromQuery(query), NewPipe.getPreferredLocalization());
}
public ChannelExtractor getChannelExtractor(String url) throws ExtractionException {
return getChannelExtractor(getChannelLHFactory().fromUrl(url), NewPipe.getPreferredLocalization());
}
public PlaylistExtractor getPlaylistExtractor(String url) throws ExtractionException {
return getPlaylistExtractor(getPlaylistLHFactory().fromUrl(url), NewPipe.getPreferredLocalization());
}
public StreamExtractor getStreamExtractor(String url) throws ExtractionException {
return getStreamExtractor(getStreamLHFactory().fromUrl(url), NewPipe.getPreferredLocalization());
}
2018-08-20 00:52:19 +02:00
public CommentsExtractor getCommentsExtractor(String url) throws ExtractionException {
ListLinkHandlerFactory llhf = getCommentsLHFactory();
if(null == llhf) {
return null;
}
2018-10-19 16:47:26 +02:00
return getCommentsExtractor(llhf.fromUrl(url), NewPipe.getPreferredLocalization());
2018-08-20 00:52:19 +02:00
}
2018-09-29 09:49:00 +02:00
public abstract boolean isCommentsSupported();
2018-10-11 21:10:22 +02:00
public abstract String getBaseUrl();
2017-03-01 18:47:52 +01:00
/**
2018-11-10 10:50:13 +01:00
* Figures out where the link is pointing to (a channel, a video, a playlist, etc.)
* @param url the url on which it should be decided of which link type it is
* @return the link type of url
* @throws ParsingException
2017-03-01 18:47:52 +01:00
*/
public final LinkType getLinkTypeByUrl(String url) throws ParsingException {
2018-08-05 14:14:36 +02:00
LinkHandlerFactory sH = getStreamLHFactory();
LinkHandlerFactory cH = getChannelLHFactory();
LinkHandlerFactory pH = getPlaylistLHFactory();
2017-03-01 18:47:52 +01:00
if (sH.acceptUrl(url)) {
2017-03-01 18:47:52 +01:00
return LinkType.STREAM;
} else if (cH.acceptUrl(url)) {
2017-08-11 03:23:09 +02:00
return LinkType.CHANNEL;
} else if (pH.acceptUrl(url)) {
return LinkType.PLAYLIST;
2017-03-01 18:47:52 +01:00
} else {
return LinkType.NONE;
}
}
}