From bda65e83d64743babe94243b3b93c6ae0c295c44 Mon Sep 17 00:00:00 2001 From: Mauricio Colli Date: Sun, 9 Jul 2017 19:43:04 -0300 Subject: [PATCH] Refactor Extractor - Renaming and removal of duplicate code - New base class for list extractors --- ListExtractor.java | 32 +++++++++++++++++++ StreamingService.java | 7 ++-- channel/ChannelExtractor.java | 5 ++- playlist/PlaylistExtractor.java | 6 ++-- services/youtube/YoutubeChannelExtractor.java | 11 ++----- .../youtube/YoutubePlaylistExtractor.java | 10 ++---- services/youtube/YoutubeService.java | 2 +- 7 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 ListExtractor.java diff --git a/ListExtractor.java b/ListExtractor.java new file mode 100644 index 00000000..318cde7d --- /dev/null +++ b/ListExtractor.java @@ -0,0 +1,32 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector; + +import java.io.IOException; + +/** + * Base class to extractors that have a list (e.g. playlists, channels). + */ +public abstract class ListExtractor extends Extractor { + protected String nextStreamsUrl; + + public ListExtractor(UrlIdHandler urlIdHandler, int serviceId, String url) { + super(urlIdHandler, serviceId, url); + } + + public boolean hasMoreStreams(){ + return nextStreamsUrl != null && !nextStreamsUrl.isEmpty(); + } + + public abstract StreamInfoItemCollector getNextStreams() throws ExtractionException, IOException; + + public String getNextStreamsUrl() { + return nextStreamsUrl; + } + + public void setNextStreamsUrl(String nextStreamsUrl) { + this.nextStreamsUrl = nextStreamsUrl; + } + +} diff --git a/StreamingService.java b/StreamingService.java index a81d6f98..8d5b87ff 100644 --- a/StreamingService.java +++ b/StreamingService.java @@ -27,14 +27,15 @@ public abstract class StreamingService { } public abstract ServiceInfo getServiceInfo(); - public abstract StreamExtractor getExtractorInstance(String url) throws IOException, ExtractionException; - public abstract SearchEngine getSearchEngineInstance(); + public abstract UrlIdHandler getStreamUrlIdHandlerInstance(); public abstract UrlIdHandler getChannelUrlIdHandlerInstance(); public abstract UrlIdHandler getPlaylistUrlIdHandlerInstance(); + public abstract SearchEngine getSearchEngineInstance(); + public abstract SuggestionExtractor getSuggestionExtractorInstance(); + public abstract StreamExtractor getStreamExtractorInstance(String url) throws IOException, ExtractionException; public abstract ChannelExtractor getChannelExtractorInstance(String url) throws ExtractionException, IOException; public abstract PlaylistExtractor getPlaylistExtractorInstance(String url) throws ExtractionException, IOException; - public abstract SuggestionExtractor getSuggestionExtractorInstance(); public final int getServiceId() { diff --git a/channel/ChannelExtractor.java b/channel/ChannelExtractor.java index 3b988a1b..95aa780e 100644 --- a/channel/ChannelExtractor.java +++ b/channel/ChannelExtractor.java @@ -1,6 +1,7 @@ package org.schabi.newpipe.extractor.channel; import org.schabi.newpipe.extractor.Extractor; +import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.UrlIdHandler; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; @@ -28,7 +29,7 @@ import java.io.IOException; * along with NewPipe. If not, see . */ -public abstract class ChannelExtractor extends Extractor { +public abstract class ChannelExtractor extends ListExtractor { public ChannelExtractor(UrlIdHandler urlIdHandler, String url, int serviceId) throws ExtractionException, IOException { super(urlIdHandler, serviceId, url); @@ -41,7 +42,5 @@ public abstract class ChannelExtractor extends Extractor { public abstract String getFeedUrl() throws ParsingException; public abstract StreamInfoItemCollector getStreams() throws ParsingException; public abstract long getSubscriberCount() throws ParsingException; - public abstract boolean hasMoreStreams(); - public abstract StreamInfoItemCollector getNextStreams() throws ExtractionException, IOException; } diff --git a/playlist/PlaylistExtractor.java b/playlist/PlaylistExtractor.java index 9225f79d..57542fcd 100644 --- a/playlist/PlaylistExtractor.java +++ b/playlist/PlaylistExtractor.java @@ -1,6 +1,7 @@ package org.schabi.newpipe.extractor.playlist; import org.schabi.newpipe.extractor.Extractor; +import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.UrlIdHandler; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; @@ -8,7 +9,7 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector; import java.io.IOException; -public abstract class PlaylistExtractor extends Extractor { +public abstract class PlaylistExtractor extends ListExtractor { public PlaylistExtractor(UrlIdHandler urlIdHandler, String url, int serviceId) throws ExtractionException, IOException { super(urlIdHandler, serviceId, url); @@ -23,7 +24,4 @@ public abstract class PlaylistExtractor extends Extractor { public abstract String getUploaderAvatarUrl() throws ParsingException; public abstract StreamInfoItemCollector getStreams() throws ParsingException; public abstract long getStreamsCount() throws ParsingException; - public abstract boolean hasMoreStreams(); - public abstract StreamInfoItemCollector getNextStreams() throws ExtractionException, IOException; - } diff --git a/services/youtube/YoutubeChannelExtractor.java b/services/youtube/YoutubeChannelExtractor.java index 722b40f9..6c0700b5 100644 --- a/services/youtube/YoutubeChannelExtractor.java +++ b/services/youtube/YoutubeChannelExtractor.java @@ -49,7 +49,6 @@ public class YoutubeChannelExtractor extends ChannelExtractor { * It's lazily initialized (when getNextStreams is called) */ private Document nextStreamsAjax; - private String nextStreamsUrl = ""; /*////////////////////////////////////////////////////////////////////////// // Variables for cache purposes (not "select" the current document all over again) @@ -61,7 +60,6 @@ public class YoutubeChannelExtractor extends ChannelExtractor { private String feedUrl; private long subscriberCount = -1; - public YoutubeChannelExtractor(UrlIdHandler urlIdHandler, String url, int serviceId) throws ExtractionException, IOException { super(urlIdHandler, urlIdHandler.cleanUrl(url), serviceId); fetchDocument(); @@ -160,14 +158,11 @@ public class YoutubeChannelExtractor extends ChannelExtractor { } } - @Override - public boolean hasMoreStreams() { - return nextStreamsUrl != null && !nextStreamsUrl.isEmpty(); - } - @Override public StreamInfoItemCollector getNextStreams() throws ExtractionException, IOException { - if (!hasMoreStreams()) throw new ExtractionException("Channel doesn't have more streams"); + if (!hasMoreStreams()) { + throw new ExtractionException("Channel doesn't have more streams"); + } StreamInfoItemCollector collector = new StreamInfoItemCollector(getUrlIdHandler(), getServiceId()); setupNextStreamsAjax(NewPipe.getDownloader()); diff --git a/services/youtube/YoutubePlaylistExtractor.java b/services/youtube/YoutubePlaylistExtractor.java index 75b272dc..0f81efca 100644 --- a/services/youtube/YoutubePlaylistExtractor.java +++ b/services/youtube/YoutubePlaylistExtractor.java @@ -27,7 +27,6 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor { * It's lazily initialized (when getNextStreams is called) */ private Document nextStreamsAjax = null; - private String nextStreamsUrl = ""; /*////////////////////////////////////////////////////////////////////////// // Variables for cache purposes (not "select" the current document all over again) @@ -181,14 +180,11 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor { return collector; } - @Override - public boolean hasMoreStreams() { - return nextStreamsUrl != null && !nextStreamsUrl.isEmpty(); - } - @Override public StreamInfoItemCollector getNextStreams() throws ExtractionException, IOException { - if (!hasMoreStreams()) throw new ExtractionException("Playlist doesn't have more streams"); + if (!hasMoreStreams()){ + throw new ExtractionException("Playlist doesn't have more streams"); + } StreamInfoItemCollector collector = new StreamInfoItemCollector(getUrlIdHandler(), getServiceId()); setupNextStreamsAjax(NewPipe.getDownloader()); diff --git a/services/youtube/YoutubeService.java b/services/youtube/YoutubeService.java index 615056ed..4a0c5979 100644 --- a/services/youtube/YoutubeService.java +++ b/services/youtube/YoutubeService.java @@ -46,7 +46,7 @@ public class YoutubeService extends StreamingService { } @Override - public StreamExtractor getExtractorInstance(String url) + public StreamExtractor getStreamExtractorInstance(String url) throws ExtractionException, IOException { UrlIdHandler urlIdHandler = YoutubeStreamUrlIdHandler.getInstance(); if (urlIdHandler.acceptUrl(url)) {