newpipe-documentation/docs/03_Implement_a_service.md

167 lines
11 KiB
Markdown
Raw Normal View History

2019-02-28 06:57:11 +00:00
# Implementing a Service
2019-02-28 06:57:11 +00:00
Services, or better service connectors, are the parts of NewPipe which communicate with an actual service like YouTube.
This page will describe how you can implement and add your own services to the extractor. Please make sure you read and understand the
[Concept of Extractors](https://teamnewpipe.github.io/documentation/01_Concept_of_the_extractor/)
and the [Concept of LinkHandler](https://teamnewpipe.github.io/documentation/02_Concept_of_LinkHandler/)
2019-02-28 06:57:11 +00:00
before continuing.
2019-02-28 06:57:11 +00:00
### Required and Optional Parts
Your service does not have to implement everything; some parts are optional.
This is because not all services support every feature other services support. For example, it might be that a certain
2018-11-03 13:58:23 +00:00
service does not support channels. If so, you can leave out the implementation of channels, and make the corresponding
factory method of the your __StreamingService__ implementation return __null__. The frontend will handle the lack of
2019-02-28 06:57:11 +00:00
having channels.
2019-02-28 06:57:11 +00:00
However, if you start to implement one of the optional parts of the list below, you will have to implement all of its parts/classes. NewPipe will crash if you only implement the extractor for the list item of a channel, but not the channel extractor itself.
2018-09-11 18:21:29 +00:00
2019-02-28 06:57:11 +00:00
__The Parts of a Service:__
2018-09-11 18:21:29 +00:00
2018-09-21 20:39:17 +00:00
- [Head of Service](#head-of-service)
2018-09-15 18:59:19 +00:00
- [Stream](#stream)
- [Search](#search)
- [Playlist](#playlist) _(optional)_
- [Channel](#channel) _(optional)_
2018-09-15 18:59:19 +00:00
- [Kiosk](#kiosk) _(optional)_
2018-09-11 18:21:29 +00:00
### Allowed Libraries
2019-02-28 06:57:11 +00:00
The NewPipe Extractor already includes a lot of usable tools and external libraries that should make extracting easy.
For some specific (tiny) tasks, Regex is allowed. Here you can take a look at the
[Parser](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/utils/Parser.html),
which will give you a little help with that. __Use Regex with care!!!__ Avoid it as often as possible. It's better to
2020-02-19 11:14:18 +00:00
ask us to introduce a new library than start using Regex too often.
- Html/XML Parsing: [jsoup](https://jsoup.org/apidocs/overview-summary.html)
- JSON Parsing: [nanojson](https://github.com/mmastrac/nanojson#parser-example)
2021-11-28 20:01:59 +00:00
- JavaScript Parsing/Execution: [Mozilla Rhino](https://github.com/mozilla/rhino)
- Link detection in strings: [AutoLink](https://github.com/robinst/autolink-java)
2019-02-28 06:57:11 +00:00
If you need to introduce new libraries, please tell us before you do so.
### Head of Service
2019-02-28 06:57:11 +00:00
First of all, if you want to create a new service, you should create a new package below `org.schabi.newpipe.services`
2018-11-03 13:58:23 +00:00
, with the name of your service as package name.
2019-02-28 06:57:11 +00:00
__Parts Required to be Implemented:__
2018-09-11 18:24:49 +00:00
- [StreamingService](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/StreamingService.html)
- [ServiceInfo](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/StreamingService.ServiceInfo.html)
[StreamingService](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/StreamingService.html)
is a factory class that will return objects of all important parts of your service.
2019-02-28 06:57:11 +00:00
Every extractor, handler, and info type you add and should be part of your implementation, must be instantiated using an
instance of this class. You can see it as a factory for all objects of your implementation.
[ServiceInfo](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/StreamingService.ServiceInfo.html)
2019-02-28 06:57:11 +00:00
will return some metadata about your service such as the name, capabilities, the author's name, and their
email address for further notice and maintenance issues. Remember, after extending this class, you need to return an
instance of it by through your implementation of
[`StreamingService.getServiceInfo()`](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/StreamingService.html#getServiceInfo--).
When these two classes are extended by you, you need to add your `StreamingService` to the
[ServiceList](https://github.com/TeamNewPipe/NewPipeExtractor/blob/49c2eb51859a58e4bb5ead2d9d0771408f7d59d6/extractor/src/main/java/org/schabi/newpipe/extractor/ServiceList.java#L23)
2019-02-28 06:57:11 +00:00
of NewPipe. This way, your service will become an official part of the NewPipe Extractor.
Every service has an ID, which will be set when this list gets created. You need to set this ID by entering it in the constructor.
So when adding your service just give it the ID of the previously last service in the list incremented by one.
2018-09-11 18:21:29 +00:00
### Stream
2019-02-28 06:57:11 +00:00
Streams are considered single entities of video or audio. They have metadata like a title, a description,
next/related videos, a thumbnail and comments. To obtain the URL to the actual stream data, as well as its metadata,
StreamExtractor is used. The LinkHandlerFactory will represent a link to such a stream. StreamInfoItemExtractor will
2019-02-28 06:57:11 +00:00
extract one item in a list of items representing such streams, like a search result or a playlist.
Since every streaming service (obviously) provides streams, this is required to implement. Otherwise, your service was
pretty useless :)
2019-02-28 06:57:11 +00:00
__Parts Required to be Implemented:__
2018-11-03 13:58:23 +00:00
2018-09-11 18:21:29 +00:00
- [StreamExtractor](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/stream/StreamExtractor.html)
- [StreamInfoItemExtractor](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.html)
- [LinkHandlerFactory](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/linkhandler/LinkHandlerFactory.html)
2018-09-15 18:59:19 +00:00
### Search
The SearchExtractor is also required to be implemented. It will take a search query represented as
[SearchQueryHandler](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/linkhandler/SearchQueryHandler.html)
2019-02-28 06:57:11 +00:00
and return a list of search results. Since many services support suggestions as you type, you will also want to implement
a __SuggestionExtractor__. This will make it possible for the frontend to also display a suggestion while typing.
2019-02-28 06:57:11 +00:00
__Parts Required to be Implemented:__
2018-11-03 13:58:23 +00:00
2018-09-15 18:59:19 +00:00
- [SearchExtractor](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/search/SearchExtractor.html)
- [SearchQueryHandlerFactory](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/linkhandler/SearchQueryHandlerFactory.html)
2020-04-19 19:51:25 +00:00
- [SuggestionExtractor](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/suggestion/SuggestionExtractor.html) _(optional)_
2018-09-15 18:59:19 +00:00
2018-09-11 18:21:29 +00:00
### Playlist
2019-02-28 06:57:11 +00:00
Playlists are lists of streams provided by the service (you might not have to be concerned over locally saved playlists, those will be handled by the frontend).
A playlist may only contain __StreamInfoItems__, but no other __InfoItem__ types.
2018-11-03 13:58:23 +00:00
2019-02-28 06:57:11 +00:00
__Parts Required to be Implemented:__
2018-09-11 18:21:29 +00:00
- [PlaylistExtractor](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/playlist/PlaylistExtractor.html)
- [PlayListInfoItemExtractor](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/playlist/PlaylistExtractor.html)
- [ListLinkHandlerFactory](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/linkhandler/ListLinkHandlerFactory.html)
### Channel
2019-02-28 06:57:11 +00:00
A Channel is mostly a [Playlist](#playlist), the only difference is that it does not only represent a simple list of streams, but also a
user, a channel, or any entity that could be represented as a user. This is why the metadata supported by the ChannelExtractor
differs from the one of a playlist.
2019-02-28 06:57:11 +00:00
__Parts Required to be Implemented:__
2018-11-03 13:58:23 +00:00
- [ChannelExtractor](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/channel/ChannelExtractor.html)
- [ChannelInfoItemExtractor](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/channel/ChannelExtractor.html)
- [ListLinkHandlerFactory](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/linkhandler/ListLinkHandlerFactory.html)
2018-09-11 18:21:29 +00:00
### Kiosk
2018-11-03 13:58:23 +00:00
A kiosk is a list of __InfoItems__ which will be displayed on the main page of NewPipe. A kiosk is mostly similar to the content
2019-02-28 06:57:11 +00:00
displayed on the main page of a video platform. A kiosk could be something like "Top 20", "Charts", "News", "Creators Selection" etc.
Kiosks are controversial; many people may not like them. If you also don't like them, please consider your users and refrain from denying support for them.
Your service would look pretty empty if you select it and no video is being displayed. Also, you should not override the preference of the user,
since users of NewPipe can decide by the settings whether they want to see the kiosk page or not.
2018-11-03 13:58:23 +00:00
#### Multiple Kiosks
2019-02-28 06:57:11 +00:00
Most services will implement more than one kiosk, so a service might have a "Top 20" for different categories like "Country Music", "Techno", etc.
2018-11-03 13:58:23 +00:00
This is why the extractor will let you implement multiple __KioskExtractors__. Since different kiosk pages might also differ
2019-02-28 06:57:11 +00:00
with their HTML structure, every page you want to support has to be implemented as its own __KioskExtractor__.
However, if the pages are similar, you can use the same implementation, but set the page type when you instantiate your __KioskExtractor__
2018-11-03 13:58:23 +00:00
through the __KioskList.KioskExtractorFactory__.
2019-02-28 06:57:11 +00:00
Every kiosk you implement needs to be added to your __KioskList__ which you return with your
2018-11-03 13:58:23 +00:00
__StreamingService__ implementation.
It is also important to set the default kiosk. This will be the kiosk that will be shown by the first start of your service.
An example implementation of the
[getKioskList()](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/StreamingService.html)
could look like this:
```java
@Override
public KioskList getKioskList() throws ExtractionException {
KioskList list = new KioskList(getServiceId());
list.addKioskEntry(new KioskList.KioskExtractorFactory() {
@Override
public KioskExtractor createNewKiosk(StreamingService streamingService,
String url,
String id,
Localization local)
throws ExtractionException {
return new YoutubeTrendingExtractor(YoutubeService.this,
new YoutubeTrendingLinkHandlerFactory().fromUrl(url), id, local);
}
}, new YoutubeTrendingLinkHandlerFactory(), "Trending");
list.setDefaultKiosk("Trending");
return list;
}
```
2019-02-28 06:57:11 +00:00
__Parts Required to be Implemented:__
2018-11-03 13:58:23 +00:00
- [KioskList.KioskExtractorFactory](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/kiosk/KioskList.KioskExtractorFactory.html)
2018-09-11 18:21:29 +00:00
- [KioskExtractor](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/kiosk/KioskExtractor.html)
- [ListLinkHandlerFactory](https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/linkhandler/ListLinkHandlerFactory.html)