Skip to content

Commit bda65e8

Browse files
committed
Refactor Extractor
- Renaming and removal of duplicate code - New base class for list extractors
1 parent 7299308 commit bda65e8

7 files changed

Lines changed: 47 additions & 26 deletions

File tree

ListExtractor.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.schabi.newpipe.extractor;
2+
3+
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
4+
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
5+
6+
import java.io.IOException;
7+
8+
/**
9+
* Base class to extractors that have a list (e.g. playlists, channels).
10+
*/
11+
public abstract class ListExtractor extends Extractor {
12+
protected String nextStreamsUrl;
13+
14+
public ListExtractor(UrlIdHandler urlIdHandler, int serviceId, String url) {
15+
super(urlIdHandler, serviceId, url);
16+
}
17+
18+
public boolean hasMoreStreams(){
19+
return nextStreamsUrl != null && !nextStreamsUrl.isEmpty();
20+
}
21+
22+
public abstract StreamInfoItemCollector getNextStreams() throws ExtractionException, IOException;
23+
24+
public String getNextStreamsUrl() {
25+
return nextStreamsUrl;
26+
}
27+
28+
public void setNextStreamsUrl(String nextStreamsUrl) {
29+
this.nextStreamsUrl = nextStreamsUrl;
30+
}
31+
32+
}

StreamingService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ public StreamingService(int id) {
2727
}
2828

2929
public abstract ServiceInfo getServiceInfo();
30-
public abstract StreamExtractor getExtractorInstance(String url) throws IOException, ExtractionException;
31-
public abstract SearchEngine getSearchEngineInstance();
30+
3231
public abstract UrlIdHandler getStreamUrlIdHandlerInstance();
3332
public abstract UrlIdHandler getChannelUrlIdHandlerInstance();
3433
public abstract UrlIdHandler getPlaylistUrlIdHandlerInstance();
34+
public abstract SearchEngine getSearchEngineInstance();
35+
public abstract SuggestionExtractor getSuggestionExtractorInstance();
36+
public abstract StreamExtractor getStreamExtractorInstance(String url) throws IOException, ExtractionException;
3537
public abstract ChannelExtractor getChannelExtractorInstance(String url) throws ExtractionException, IOException;
3638
public abstract PlaylistExtractor getPlaylistExtractorInstance(String url) throws ExtractionException, IOException;
37-
public abstract SuggestionExtractor getSuggestionExtractorInstance();
3839

3940

4041
public final int getServiceId() {

channel/ChannelExtractor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.schabi.newpipe.extractor.channel;
22

33
import org.schabi.newpipe.extractor.Extractor;
4+
import org.schabi.newpipe.extractor.ListExtractor;
45
import org.schabi.newpipe.extractor.UrlIdHandler;
56
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
67
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@@ -28,7 +29,7 @@
2829
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2930
*/
3031

31-
public abstract class ChannelExtractor extends Extractor {
32+
public abstract class ChannelExtractor extends ListExtractor {
3233

3334
public ChannelExtractor(UrlIdHandler urlIdHandler, String url, int serviceId) throws ExtractionException, IOException {
3435
super(urlIdHandler, serviceId, url);
@@ -41,7 +42,5 @@ public ChannelExtractor(UrlIdHandler urlIdHandler, String url, int serviceId) th
4142
public abstract String getFeedUrl() throws ParsingException;
4243
public abstract StreamInfoItemCollector getStreams() throws ParsingException;
4344
public abstract long getSubscriberCount() throws ParsingException;
44-
public abstract boolean hasMoreStreams();
45-
public abstract StreamInfoItemCollector getNextStreams() throws ExtractionException, IOException;
4645

4746
}

playlist/PlaylistExtractor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.schabi.newpipe.extractor.playlist;
22

33
import org.schabi.newpipe.extractor.Extractor;
4+
import org.schabi.newpipe.extractor.ListExtractor;
45
import org.schabi.newpipe.extractor.UrlIdHandler;
56
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
67
import org.schabi.newpipe.extractor.exceptions.ParsingException;
78
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
89

910
import java.io.IOException;
1011

11-
public abstract class PlaylistExtractor extends Extractor {
12+
public abstract class PlaylistExtractor extends ListExtractor {
1213

1314
public PlaylistExtractor(UrlIdHandler urlIdHandler, String url, int serviceId) throws ExtractionException, IOException {
1415
super(urlIdHandler, serviceId, url);
@@ -23,7 +24,4 @@ public PlaylistExtractor(UrlIdHandler urlIdHandler, String url, int serviceId) t
2324
public abstract String getUploaderAvatarUrl() throws ParsingException;
2425
public abstract StreamInfoItemCollector getStreams() throws ParsingException;
2526
public abstract long getStreamsCount() throws ParsingException;
26-
public abstract boolean hasMoreStreams();
27-
public abstract StreamInfoItemCollector getNextStreams() throws ExtractionException, IOException;
28-
2927
}

services/youtube/YoutubeChannelExtractor.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
4949
* It's lazily initialized (when getNextStreams is called)
5050
*/
5151
private Document nextStreamsAjax;
52-
private String nextStreamsUrl = "";
5352

5453
/*//////////////////////////////////////////////////////////////////////////
5554
// Variables for cache purposes (not "select" the current document all over again)
@@ -61,7 +60,6 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
6160
private String feedUrl;
6261
private long subscriberCount = -1;
6362

64-
6563
public YoutubeChannelExtractor(UrlIdHandler urlIdHandler, String url, int serviceId) throws ExtractionException, IOException {
6664
super(urlIdHandler, urlIdHandler.cleanUrl(url), serviceId);
6765
fetchDocument();
@@ -160,14 +158,11 @@ public String getFeedUrl() throws ParsingException {
160158
}
161159
}
162160

163-
@Override
164-
public boolean hasMoreStreams() {
165-
return nextStreamsUrl != null && !nextStreamsUrl.isEmpty();
166-
}
167-
168161
@Override
169162
public StreamInfoItemCollector getNextStreams() throws ExtractionException, IOException {
170-
if (!hasMoreStreams()) throw new ExtractionException("Channel doesn't have more streams");
163+
if (!hasMoreStreams()) {
164+
throw new ExtractionException("Channel doesn't have more streams");
165+
}
171166

172167
StreamInfoItemCollector collector = new StreamInfoItemCollector(getUrlIdHandler(), getServiceId());
173168
setupNextStreamsAjax(NewPipe.getDownloader());

services/youtube/YoutubePlaylistExtractor.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
2727
* It's lazily initialized (when getNextStreams is called)
2828
*/
2929
private Document nextStreamsAjax = null;
30-
private String nextStreamsUrl = "";
3130

3231
/*//////////////////////////////////////////////////////////////////////////
3332
// Variables for cache purposes (not "select" the current document all over again)
@@ -181,14 +180,11 @@ public StreamInfoItemCollector getStreams() throws ParsingException {
181180
return collector;
182181
}
183182

184-
@Override
185-
public boolean hasMoreStreams() {
186-
return nextStreamsUrl != null && !nextStreamsUrl.isEmpty();
187-
}
188-
189183
@Override
190184
public StreamInfoItemCollector getNextStreams() throws ExtractionException, IOException {
191-
if (!hasMoreStreams()) throw new ExtractionException("Playlist doesn't have more streams");
185+
if (!hasMoreStreams()){
186+
throw new ExtractionException("Playlist doesn't have more streams");
187+
}
192188

193189
StreamInfoItemCollector collector = new StreamInfoItemCollector(getUrlIdHandler(), getServiceId());
194190
setupNextStreamsAjax(NewPipe.getDownloader());

services/youtube/YoutubeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public ServiceInfo getServiceInfo() {
4646
}
4747

4848
@Override
49-
public StreamExtractor getExtractorInstance(String url)
49+
public StreamExtractor getStreamExtractorInstance(String url)
5050
throws ExtractionException, IOException {
5151
UrlIdHandler urlIdHandler = YoutubeStreamUrlIdHandler.getInstance();
5252
if (urlIdHandler.acceptUrl(url)) {

0 commit comments

Comments
 (0)