Skip to content

Commit e4874d5

Browse files
authored
Merge pull request #296 from mauriciocolli/fix-search-errors-detection
Fix search errors detection and refactor search tests
2 parents 8a9ae32 + 0a20c53 commit e4874d5

29 files changed

Lines changed: 700 additions & 921 deletions

extractor/src/main/java/org/schabi/newpipe/extractor/search/InfoItemsSearchCollector.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor;
1212
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
1313

14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.List;
17+
1418
/*
1519
* Created by Christian Schabesberger on 12.02.17.
1620
*
@@ -48,13 +52,31 @@ public class InfoItemsSearchCollector extends InfoItemsCollector<InfoItem, InfoI
4852
private final ChannelInfoItemsCollector userCollector;
4953
private final PlaylistInfoItemsCollector playlistCollector;
5054

51-
InfoItemsSearchCollector(int serviceId) {
55+
public InfoItemsSearchCollector(int serviceId) {
5256
super(serviceId);
5357
streamCollector = new StreamInfoItemsCollector(serviceId);
5458
userCollector = new ChannelInfoItemsCollector(serviceId);
5559
playlistCollector = new PlaylistInfoItemsCollector(serviceId);
5660
}
5761

62+
@Override
63+
public List<Throwable> getErrors() {
64+
final List<Throwable> errors = new ArrayList<>(super.getErrors());
65+
errors.addAll(streamCollector.getErrors());
66+
errors.addAll(userCollector.getErrors());
67+
errors.addAll(playlistCollector.getErrors());
68+
69+
return Collections.unmodifiableList(errors);
70+
}
71+
72+
@Override
73+
public void reset() {
74+
super.reset();
75+
streamCollector.reset();
76+
userCollector.reset();
77+
playlistCollector.reset();
78+
}
79+
5880
@Override
5981
public InfoItem extract(InfoItemExtractor extractor) throws ParsingException {
6082
// Use the corresponding collector for each item extractor type

extractor/src/main/java/org/schabi/newpipe/extractor/search/SearchExtractor.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ public NothingFoundException(String message) {
1717
}
1818
}
1919

20-
private final InfoItemsSearchCollector collector;
21-
2220
public SearchExtractor(StreamingService service, SearchQueryHandler linkHandler) {
2321
super(service, linkHandler);
24-
collector = new InfoItemsSearchCollector(service.getServiceId());
2522
}
2623

2724
public String getSearchString() {
@@ -30,10 +27,6 @@ public String getSearchString() {
3027

3128
public abstract String getSearchSuggestion() throws ParsingException;
3229

33-
protected InfoItemsSearchCollector getInfoItemSearchCollector() {
34-
return collector;
35-
}
36-
3730
@Override
3831
public SearchQueryHandler getLinkHandler() {
3932
return (SearchQueryHandler) super.getLinkHandler();

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCSearchExtractor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public String getSearchSuggestion() throws ParsingException {
4747
@Nonnull
4848
@Override
4949
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
50-
InfoItemsSearchCollector searchItems = getInfoItemSearchCollector();
51-
searchItems.reset();
50+
final InfoItemsSearchCollector searchItems = new InfoItemsSearchCollector(getServiceId());
5251

5352
if (getLinkHandler().getContentFilters().contains(CONFERENCES)
5453
|| getLinkHandler().getContentFilters().contains(ALL)

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeSearchExtractor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionEx
4747
}
4848

4949
private InfoItemsCollector<InfoItem, InfoItemExtractor> collectStreamsFrom(JsonObject json) throws ParsingException {
50-
51-
final InfoItemsSearchCollector collector = getInfoItemSearchCollector();
50+
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
5251

5352
JsonArray contents;
5453
try {

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudSearchExtractor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ public void onFetchPage(@Nonnull Downloader downloader) throws IOException, Extr
7878
}
7979

8080
private InfoItemsCollector<InfoItem, InfoItemExtractor> collectItems(JsonArray searchCollection) {
81-
final InfoItemsSearchCollector collector = getInfoItemSearchCollector();
82-
collector.reset();
81+
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
8382

8483
for (Object result : searchCollection) {
8584
if (!(result instanceof JsonObject)) continue;

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelInfoItemExtractor.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,14 @@ public String getUrl() throws ParsingException {
7070
@Override
7171
public long getSubscriberCount() throws ParsingException {
7272
try {
73-
String subscribers = getTextFromObject(channelInfoItem.getObject("subscriberCountText"));
74-
return Utils.mixedNumberWordToLong(subscribers);
73+
final JsonObject subscriberCountObject = channelInfoItem.getObject("subscriberCountText");
74+
75+
if (subscriberCountObject == null) {
76+
// Subscription count is not available for this channel item.
77+
return -1;
78+
}
79+
80+
return Utils.mixedNumberWordToLong(getTextFromObject(subscriberCountObject));
7581
} catch (Exception e) {
7682
throw new ParsingException("Could not get subscriber count", e);
7783
}
@@ -80,7 +86,14 @@ public long getSubscriberCount() throws ParsingException {
8086
@Override
8187
public long getStreamCount() throws ParsingException {
8288
try {
83-
return Long.parseLong(Utils.removeNonDigitCharacters(getTextFromObject(channelInfoItem.getObject("videoCountText"))));
89+
final JsonObject videoCountObject = channelInfoItem.getObject("videoCountText");
90+
91+
if (videoCountObject == null) {
92+
// Video count is not available, channel probably has no public uploads.
93+
return -1;
94+
}
95+
96+
return Long.parseLong(Utils.removeNonDigitCharacters(getTextFromObject(videoCountObject)));
8497
} catch (Exception e) {
8598
throw new ParsingException("Could not get stream count", e);
8699
}
@@ -89,7 +102,14 @@ public long getStreamCount() throws ParsingException {
89102
@Override
90103
public String getDescription() throws ParsingException {
91104
try {
92-
return getTextFromObject(channelInfoItem.getObject("descriptionSnippet"));
105+
final JsonObject descriptionObject = channelInfoItem.getObject("descriptionSnippet");
106+
107+
if (descriptionObject == null) {
108+
// Channel have no description.
109+
return null;
110+
}
111+
112+
return getTextFromObject(descriptionObject);
93113
} catch (Exception e) {
94114
throw new ParsingException("Could not get description", e);
95115
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public String getSearchSuggestion() throws ParsingException {
7979
@Nonnull
8080
@Override
8181
public InfoItemsPage<InfoItem> getInitialPage() throws ExtractionException {
82-
InfoItemsSearchCollector collector = getInfoItemSearchCollector();
82+
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
8383
JsonArray sections = initialData.getObject("contents").getObject("twoColumnSearchResultsRenderer")
8484
.getObject("primaryContents").getObject("sectionListRenderer").getArray("contents");
8585

@@ -103,7 +103,7 @@ public InfoItemsPage<InfoItem> getPage(String pageUrl) throws IOException, Extra
103103
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
104104
}
105105

106-
InfoItemsSearchCollector collector = getInfoItemSearchCollector();
106+
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
107107
final JsonArray ajaxJson = getJsonResponse(pageUrl, getExtractorLocalization());
108108

109109
JsonObject itemSectionRenderer = ajaxJson.getObject(1).getObject("response")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
@SuppressWarnings("unused")
4+
public interface BaseSearchExtractorTest extends BaseListExtractorTest {
5+
void testSearchString() throws Exception;
6+
void testSearchSuggestion() throws Exception;
7+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
import org.junit.Test;
4+
import org.schabi.newpipe.extractor.Extractor;
5+
import org.schabi.newpipe.extractor.StreamingService;
6+
7+
import static org.hamcrest.CoreMatchers.*;
8+
import static org.junit.Assert.*;
9+
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
10+
11+
public abstract class DefaultExtractorTest<T extends Extractor> implements BaseExtractorTest {
12+
public abstract T extractor() throws Exception;
13+
14+
public abstract StreamingService expectedService() throws Exception;
15+
public abstract String expectedName() throws Exception;
16+
public abstract String expectedId() throws Exception;
17+
public abstract String expectedUrlContains() throws Exception;
18+
public abstract String expectedOriginalUrlContains() throws Exception;
19+
20+
@Test
21+
@Override
22+
public void testServiceId() throws Exception {
23+
assertEquals(expectedService().getServiceId(), extractor().getServiceId());
24+
}
25+
26+
@Test
27+
@Override
28+
public void testName() throws Exception {
29+
assertEquals(expectedName(), extractor().getName());
30+
}
31+
32+
@Test
33+
@Override
34+
public void testId() throws Exception {
35+
assertEquals(expectedId(), extractor().getId());
36+
}
37+
38+
@Test
39+
@Override
40+
public void testUrl() throws Exception {
41+
final String url = extractor().getUrl();
42+
assertIsSecureUrl(url);
43+
assertThat(url, containsString(expectedUrlContains()));
44+
}
45+
46+
@Test
47+
@Override
48+
public void testOriginalUrl() throws Exception {
49+
final String originalUrl = extractor().getOriginalUrl();
50+
assertIsSecureUrl(originalUrl);
51+
assertThat(originalUrl, containsString(expectedOriginalUrlContains()));
52+
}
53+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
import org.junit.Test;
4+
import org.schabi.newpipe.extractor.InfoItem;
5+
import org.schabi.newpipe.extractor.ListExtractor;
6+
7+
import javax.annotation.Nullable;
8+
9+
import static org.schabi.newpipe.extractor.services.DefaultTests.*;
10+
11+
public abstract class DefaultListExtractorTest<T extends ListExtractor<? extends InfoItem>> extends DefaultExtractorTest<T>
12+
implements BaseListExtractorTest {
13+
14+
@Nullable
15+
public InfoItem.InfoType expectedInfoItemType() {
16+
return null;
17+
}
18+
19+
public boolean expectedHasMoreItems() {
20+
return true;
21+
}
22+
23+
@Test
24+
@Override
25+
public void testRelatedItems() throws Exception {
26+
final ListExtractor<? extends InfoItem> extractor = extractor();
27+
28+
final InfoItem.InfoType expectedType = expectedInfoItemType();
29+
final ListExtractor.InfoItemsPage<? extends InfoItem> items = defaultTestRelatedItems(extractor);
30+
if (expectedType != null) {
31+
assertOnlyContainsType(items, expectedType);
32+
}
33+
}
34+
35+
@Test
36+
@Override
37+
public void testMoreRelatedItems() throws Exception {
38+
final ListExtractor<? extends InfoItem> extractor = extractor();
39+
40+
if (expectedHasMoreItems()) {
41+
final InfoItem.InfoType expectedType = expectedInfoItemType();
42+
final ListExtractor.InfoItemsPage<? extends InfoItem> items = defaultTestMoreItems(extractor);
43+
if (expectedType != null) {
44+
assertOnlyContainsType(items, expectedType);
45+
}
46+
} else {
47+
assertNoMoreItems(extractor);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)