2017-06-29 20:12:55 +02:00
|
|
|
package org.schabi.newpipe.extractor;
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2018-07-13 18:02:40 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
2017-08-06 22:20:15 +02:00
|
|
|
import java.io.IOException;
|
2018-09-03 00:46:34 +02:00
|
|
|
import java.io.Serializable;
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2018-09-03 00:46:34 +02:00
|
|
|
public abstract class Extractor implements Serializable{
|
2017-08-06 22:20:15 +02:00
|
|
|
/**
|
2018-03-18 20:17:22 +01:00
|
|
|
* {@link StreamingService} currently related to this extractor.<br>
|
2017-08-06 22:20:15 +02:00
|
|
|
* Useful for getting other things from a service (like the url handlers for cleaning/accepting/get id from urls).
|
|
|
|
*/
|
|
|
|
private final StreamingService service;
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2018-07-13 18:02:40 +02:00
|
|
|
private final LinkHandler uIHandler;
|
2017-08-06 22:20:15 +02:00
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nullable
|
2017-11-28 13:37:01 +01:00
|
|
|
private boolean pageFetched = false;
|
|
|
|
private final Downloader downloader;
|
2017-08-06 22:20:15 +02:00
|
|
|
|
2018-07-13 18:02:40 +02:00
|
|
|
public Extractor(final StreamingService service, final LinkHandler uIHandler) {
|
2017-11-11 12:17:14 +01:00
|
|
|
if(service == null) throw new NullPointerException("service is null");
|
2018-07-13 18:02:40 +02:00
|
|
|
if(uIHandler == null) throw new NullPointerException("LinkHandler is null");
|
2017-08-06 22:20:15 +02:00
|
|
|
this.service = service;
|
2018-07-01 16:21:40 +02:00
|
|
|
this.uIHandler = uIHandler;
|
2017-11-28 13:37:01 +01:00
|
|
|
this.downloader = NewPipe.getDownloader();
|
|
|
|
if(downloader == null) throw new NullPointerException("downloader is null");
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
/**
|
2018-07-13 18:02:40 +02:00
|
|
|
* @return The {@link LinkHandler} of the current extractor object (e.g. a ChannelExtractor should return a channel url handler).
|
2017-08-06 22:20:15 +02:00
|
|
|
*/
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2018-07-13 18:02:40 +02:00
|
|
|
public LinkHandler getUIHandler() {
|
2018-07-01 16:21:40 +02:00
|
|
|
return uIHandler;
|
2018-05-06 14:08:50 +02:00
|
|
|
}
|
2017-08-06 22:20:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the current page.
|
2017-11-28 13:37:01 +01:00
|
|
|
* @throws IOException if the page can not be loaded
|
|
|
|
* @throws ExtractionException if the pages content is not understood
|
2017-08-06 22:20:15 +02:00
|
|
|
*/
|
2017-11-28 13:37:01 +01:00
|
|
|
public void fetchPage() throws IOException, ExtractionException {
|
|
|
|
if(pageFetched) return;
|
|
|
|
onFetchPage(downloader);
|
|
|
|
pageFetched = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void assertPageFetched() {
|
|
|
|
if(!pageFetched) throw new IllegalStateException("Page is not fetched. Make sure you call fetchPage()");
|
|
|
|
}
|
|
|
|
|
2018-01-13 20:09:31 +01:00
|
|
|
protected boolean isPageFetched() {
|
|
|
|
return pageFetched;
|
|
|
|
}
|
|
|
|
|
2017-11-28 13:37:01 +01:00
|
|
|
/**
|
|
|
|
* Fetch the current page.
|
|
|
|
* @param downloader the download to use
|
|
|
|
* @throws IOException if the page can not be loaded
|
|
|
|
* @throws ExtractionException if the pages content is not understood
|
|
|
|
*/
|
|
|
|
public abstract void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException;
|
2017-08-06 22:20:15 +02:00
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2018-05-06 14:08:50 +02:00
|
|
|
public String getId() throws ParsingException {
|
2018-07-01 16:21:40 +02:00
|
|
|
return uIHandler.getId();
|
2018-05-06 14:08:50 +02:00
|
|
|
}
|
2017-11-25 01:10:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name
|
|
|
|
* @return the name
|
|
|
|
* @throws ParsingException if the name cannot be extracted
|
|
|
|
*/
|
|
|
|
@Nonnull
|
2017-08-11 03:23:09 +02:00
|
|
|
public abstract String getName() throws ParsingException;
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2018-05-06 14:08:50 +02:00
|
|
|
public String getOriginalUrl() throws ParsingException {
|
2018-07-01 16:21:40 +02:00
|
|
|
return uIHandler.getOriginalUrl();
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2018-05-06 14:08:50 +02:00
|
|
|
public String getUrl() throws ParsingException {
|
2018-07-01 16:21:40 +02:00
|
|
|
return uIHandler.getUrl();
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2017-08-06 22:20:15 +02:00
|
|
|
public StreamingService getService() {
|
|
|
|
return service;
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
public int getServiceId() {
|
|
|
|
return service.getServiceId();
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
2018-05-26 19:15:45 +02:00
|
|
|
|
|
|
|
public Downloader getDownloader() {
|
|
|
|
return downloader;
|
|
|
|
}
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|