Skip to content

Commit ceb5563

Browse files
committed
Refactor collectors
* Define a common iterface * Use generic types * Remove some duplicated code * Simplify InfoItemSearchCollector and remove unused methods * SearchResult: Make fields final
1 parent bc44557 commit ceb5563

22 files changed

Lines changed: 223 additions & 144 deletions
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.schabi.newpipe.extractor;
2+
3+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Collectors are used to simplify the collection of information
9+
* from extractors
10+
* @param <I> the item type
11+
* @param <E> the extractor type
12+
*/
13+
public interface Collector<I, E> {
14+
15+
/**
16+
* Try to add an extractor to the collection
17+
* @param extractor the extractor to add
18+
*/
19+
void commit(E extractor);
20+
21+
/**
22+
* Try to extract the item from an extractor without adding it to the collection
23+
* @param extractor the extractor to use
24+
* @return the item
25+
* @throws ParsingException thrown if there is an error extracting the
26+
* <b>required</b> fields of the item.
27+
*/
28+
I extract(E extractor) throws ParsingException;
29+
30+
/**
31+
* Get all items
32+
* @return the items
33+
*/
34+
List<I> getItemList();
35+
36+
/**
37+
* Get all errors
38+
* @return the errors
39+
*/
40+
List<Throwable> getErrors();
41+
42+
/**
43+
* Reset all collected items and errors
44+
*/
45+
void reset();
46+
}

src/main/java/org/schabi/newpipe/extractor/Extractor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public abstract class Extractor {
3232
private String cleanUrl;
3333

3434
public Extractor(StreamingService service, String url) throws ExtractionException {
35+
if(service == null) throw new NullPointerException("service is null");
36+
if(url == null) throw new NullPointerException("url is null");
3537
this.service = service;
3638
this.originalUrl = url;
3739
}
@@ -53,6 +55,10 @@ public String getOriginalUrl() {
5355
return originalUrl;
5456
}
5557

58+
/**
59+
* Get a clean url and as a fallback the original url.
60+
* @return the clean url or the original url
61+
*/
5662
public String getCleanUrl() {
5763
if (cleanUrl != null && !cleanUrl.isEmpty()) return cleanUrl;
5864

src/main/java/org/schabi/newpipe/extractor/InfoItem.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2121
*/
2222

23-
import org.schabi.newpipe.extractor.stream.StreamType;
24-
2523
import java.io.Serializable;
2624

2725
public abstract class InfoItem implements Serializable {
Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package org.schabi.newpipe.extractor;
22

3-
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
3+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
44

55
import java.util.ArrayList;
6+
import java.util.Collections;
67
import java.util.List;
78

89
/*
@@ -25,42 +26,66 @@
2526
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2627
*/
2728

28-
public abstract class InfoItemCollector {
29-
private List<InfoItem> itemList = new ArrayList<>();
30-
private List<Throwable> errors = new ArrayList<>();
31-
private int serviceId = -1;
29+
public abstract class InfoItemCollector<I extends InfoItem, E> implements Collector<I,E> {
3230

31+
private final List<I> itemList = new ArrayList<>();
32+
private final List<Throwable> errors = new ArrayList<>();
33+
private final int serviceId;
34+
35+
/**
36+
* Create a new collector
37+
* @param serviceId the service id
38+
*/
3339
public InfoItemCollector(int serviceId) {
3440
this.serviceId = serviceId;
3541
}
3642

37-
public List<InfoItem> getItemList() {
38-
return itemList;
43+
@Override
44+
public List<I> getItemList() {
45+
return Collections.unmodifiableList(itemList);
3946
}
4047

48+
@Override
4149
public List<Throwable> getErrors() {
42-
return errors;
50+
return Collections.unmodifiableList(errors);
4351
}
4452

45-
protected void addFromCollector(InfoItemCollector otherC) throws ExtractionException {
46-
if (serviceId != otherC.serviceId) {
47-
throw new ExtractionException("Service Id does not equal: "
48-
+ NewPipe.getNameOfService(serviceId)
49-
+ " and " + NewPipe.getNameOfService((otherC.serviceId)));
50-
}
51-
errors.addAll(otherC.errors);
52-
itemList.addAll(otherC.itemList);
53+
@Override
54+
public void reset() {
55+
itemList.clear();
56+
errors.clear();
5357
}
5458

55-
protected void addError(Exception e) {
56-
errors.add(e);
59+
/**
60+
* Add an error
61+
* @param error the error
62+
*/
63+
protected void addError(Exception error) {
64+
errors.add(error);
5765
}
5866

59-
protected void addItem(InfoItem item) {
67+
/**
68+
* Add an item
69+
* @param item the item
70+
*/
71+
protected void addItem(I item) {
6072
itemList.add(item);
6173
}
6274

63-
protected int getServiceId() {
75+
/**
76+
* Get the service id
77+
* @return the service id
78+
*/
79+
public int getServiceId() {
6480
return serviceId;
6581
}
82+
83+
@Override
84+
public void commit(E extractor) {
85+
try {
86+
addItem(extract(extractor));
87+
} catch (ParsingException e) {
88+
addError(e);
89+
}
90+
}
6691
}

src/main/java/org/schabi/newpipe/extractor/ListExtractor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ public NextItemsResult(List<InfoItem> nextItemsList, String nextItemsUrl, List<T
8787
public boolean hasMoreStreams() {
8888
return nextItemsUrl != null && !nextItemsUrl.isEmpty();
8989
}
90+
91+
public List<InfoItem> getNextItemsList() {
92+
return nextItemsList;
93+
}
94+
95+
public String getNextItemsUrl() {
96+
return nextItemsUrl;
97+
}
98+
99+
public List<Throwable> getErrors() {
100+
return errors;
101+
}
90102
}
91103

92104
}

src/main/java/org/schabi/newpipe/extractor/ListInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public ListInfo(int serviceId, String id, String url, String name) {
1111
super(serviceId, id, url, name);
1212
}
1313

14-
public List<InfoItem> getRelated_streams() {
14+
public List<InfoItem> getRelatedStreams() {
1515
return related_streams;
1616
}
1717

1818
public void setRelatedStreams(List<InfoItem> related_streams) {
1919
this.related_streams = related_streams;
2020
}
2121

22-
public boolean isHas_more_streams() {
22+
public boolean hasMoreStreams() {
2323
return has_more_streams;
2424
}
2525

2626
public void setHasMoreStreams(boolean has_more_streams) {
2727
this.has_more_streams = has_more_streams;
2828
}
2929

30-
public String getNext_streams_url() {
30+
public String getNextStreamsUrl() {
3131
return next_streams_url;
3232
}
3333

src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItemCollector.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2424
*/
2525

26-
public class ChannelInfoItemCollector extends InfoItemCollector {
26+
public class ChannelInfoItemCollector extends InfoItemCollector<ChannelInfoItem, ChannelInfoItemExtractor> {
2727
public ChannelInfoItemCollector(int serviceId) {
2828
super(serviceId);
2929
}
3030

31+
@Override
3132
public ChannelInfoItem extract(ChannelInfoItemExtractor extractor) throws ParsingException {
3233
// important information
3334
int serviceId = getServiceId();
@@ -60,12 +61,4 @@ public ChannelInfoItem extract(ChannelInfoItemExtractor extractor) throws Parsin
6061
}
6162
return resultItem;
6263
}
63-
64-
public void commit(ChannelInfoItemExtractor extractor) throws ParsingException {
65-
try {
66-
addItem(extract(extractor));
67-
} catch (Exception e) {
68-
addError(e);
69-
}
70-
}
7164
}

src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfoItemCollector.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import org.schabi.newpipe.extractor.InfoItemCollector;
44
import org.schabi.newpipe.extractor.exceptions.ParsingException;
55

6-
public class PlaylistInfoItemCollector extends InfoItemCollector {
6+
public class PlaylistInfoItemCollector extends InfoItemCollector<PlaylistInfoItem, PlaylistInfoItemExtractor> {
7+
78
public PlaylistInfoItemCollector(int serviceId) {
89
super(serviceId);
910
}
1011

12+
@Override
1113
public PlaylistInfoItem extract(PlaylistInfoItemExtractor extractor) throws ParsingException {
1214

1315
String name = extractor.getName();
@@ -33,12 +35,4 @@ public PlaylistInfoItem extract(PlaylistInfoItemExtractor extractor) throws Pars
3335
}
3436
return resultItem;
3537
}
36-
37-
public void commit(PlaylistInfoItemExtractor extractor) throws ParsingException {
38-
try {
39-
addItem(extract(extractor));
40-
} catch (Exception e) {
41-
addError(e);
42-
}
43-
}
4438
}
Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.schabi.newpipe.extractor.search;
22

3-
import org.schabi.newpipe.extractor.InfoItemCollector;
3+
import org.schabi.newpipe.extractor.*;
44
import org.schabi.newpipe.extractor.channel.ChannelInfoItemCollector;
55
import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor;
66
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
7-
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
7+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
88
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemCollector;
99
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor;
1010
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
@@ -30,13 +30,23 @@
3030
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
3131
*/
3232

33-
public class InfoItemSearchCollector extends InfoItemCollector {
33+
/**
34+
* Collector for search results
35+
*
36+
* This collector can handle the following extractor types:
37+
* <ul>
38+
* <li>{@link StreamInfoItemExtractor}</li>
39+
* <li>{@link ChannelInfoItemExtractor}</li>
40+
* <li>{@link PlaylistInfoItemExtractor}</li>
41+
* </ul>
42+
* Calling {@link #extract(InfoItemExtractor)} or {@link #commit(Object)} with any
43+
* other extractor type will raise an exception.
44+
*/
45+
public class InfoItemSearchCollector extends InfoItemCollector<InfoItem, InfoItemExtractor> {
3446
private String suggestion = "";
35-
private StreamInfoItemCollector streamCollector;
36-
private ChannelInfoItemCollector userCollector;
37-
private PlaylistInfoItemCollector playlistCollector;
38-
39-
private SearchResult result = new SearchResult();
47+
private final StreamInfoItemCollector streamCollector;
48+
private final ChannelInfoItemCollector userCollector;
49+
private final PlaylistInfoItemCollector playlistCollector;
4050

4151
InfoItemSearchCollector(int serviceId) {
4252
super(serviceId);
@@ -50,43 +60,20 @@ public void setSuggestion(String suggestion) {
5060
}
5161

5262
public SearchResult getSearchResult() throws ExtractionException {
53-
54-
addFromCollector(userCollector);
55-
addFromCollector(streamCollector);
56-
addFromCollector(playlistCollector);
57-
58-
result.suggestion = suggestion;
59-
result.errors = getErrors();
60-
return result;
61-
}
62-
63-
public void commit(StreamInfoItemExtractor extractor) {
64-
try {
65-
result.resultList.add(streamCollector.extract(extractor));
66-
} catch (FoundAdException ae) {
67-
System.err.println("Found ad");
68-
} catch (Exception e) {
69-
addError(e);
70-
}
71-
}
72-
73-
public void commit(ChannelInfoItemExtractor extractor) {
74-
try {
75-
result.resultList.add(userCollector.extract(extractor));
76-
} catch (FoundAdException ae) {
77-
System.err.println("Found ad");
78-
} catch (Exception e) {
79-
addError(e);
80-
}
63+
return new SearchResult(getServiceId(), suggestion, getItemList(), getErrors());
8164
}
8265

83-
public void commit(PlaylistInfoItemExtractor extractor) {
84-
try {
85-
result.resultList.add(playlistCollector.extract(extractor));
86-
} catch (FoundAdException ae) {
87-
System.err.println("Found ad");
88-
} catch (Exception e) {
89-
addError(e);
66+
@Override
67+
public InfoItem extract(InfoItemExtractor extractor) throws ParsingException {
68+
// Use the corresponding collector for each item extractor type
69+
if(extractor instanceof StreamInfoItemExtractor) {
70+
return streamCollector.extract((StreamInfoItemExtractor) extractor);
71+
} else if(extractor instanceof ChannelInfoItemExtractor) {
72+
return userCollector.extract((ChannelInfoItemExtractor) extractor);
73+
} else if(extractor instanceof PlaylistInfoItemExtractor) {
74+
return playlistCollector.extract((PlaylistInfoItemExtractor) extractor);
75+
} else {
76+
throw new IllegalArgumentException("Invalid extractor type: " + extractor);
9077
}
9178
}
9279
}

0 commit comments

Comments
 (0)