Search: add isCorrectedSearch() and fix YoutubeSearchExtractor#getSearchSuggestion()
isCorrectedSearch: see the javadoc getSearchSuggestion: in YoutubeSearchExtractor, it was giving the corrected search query. It now gives the suggested query, as it should
This commit is contained in:
parent
665c69b530
commit
29b639b454
|
@ -37,4 +37,15 @@ public abstract class SearchExtractor extends ListExtractor<InfoItem> {
|
|||
public String getName() {
|
||||
return getLinkHandler().getSearchString();
|
||||
}
|
||||
|
||||
/**
|
||||
* When you search on some service, it can give you another and corrected request.
|
||||
* This method says if it's the case.
|
||||
* <p>
|
||||
* Example: on YouTube, if you search for "pewdeipie",
|
||||
* it will give you results for "pewdiepie", then isCorrectedSearch should return true.
|
||||
*
|
||||
* @return whether the results comes from a corrected query or not.
|
||||
*/
|
||||
public abstract boolean isCorrectedSearch() throws ParsingException;
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ public class SearchInfo extends ListInfo<InfoItem> {
|
|||
|
||||
private String searchString;
|
||||
private String searchSuggestion;
|
||||
private boolean isCorrectedSearch;
|
||||
|
||||
public SearchInfo(int serviceId,
|
||||
SearchQueryHandler qIHandler,
|
||||
|
@ -46,6 +47,11 @@ public class SearchInfo extends ListInfo<InfoItem> {
|
|||
} catch (Exception e) {
|
||||
info.addError(e);
|
||||
}
|
||||
try {
|
||||
info.isCorrectedSearch = extractor.isCorrectedSearch();
|
||||
} catch (Exception e) {
|
||||
info.addError(e);
|
||||
}
|
||||
|
||||
ListExtractor.InfoItemsPage<InfoItem> page = ExtractorHelper.getItemsPageOrLogError(info, extractor);
|
||||
info.setRelatedItems(page.getItems());
|
||||
|
@ -70,4 +76,8 @@ public class SearchInfo extends ListInfo<InfoItem> {
|
|||
public String getSearchSuggestion() {
|
||||
return searchSuggestion;
|
||||
}
|
||||
|
||||
public boolean isCorrectedSearch() {
|
||||
return this.isCorrectedSearch;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,6 +47,11 @@ public class MediaCCCSearchExtractor extends SearchExtractor {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCorrectedSearch() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public InfoItemsPage<InfoItem> getInitialPage() {
|
||||
|
|
|
@ -40,6 +40,11 @@ public class PeertubeSearchExtractor extends SearchExtractor {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCorrectedSearch() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
|
||||
super.fetchPage();
|
||||
|
|
|
@ -38,6 +38,11 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCorrectedSearch() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
|
||||
|
|
|
@ -135,6 +135,19 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor {
|
|||
return getTextFromObject(didYouMeanRenderer.getObject("correctedQuery"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCorrectedSearch() {
|
||||
final JsonObject itemSectionRenderer = initialData.getObject("contents").getObject("sectionListRenderer")
|
||||
.getArray("contents").getObject(0).getObject("itemSectionRenderer");
|
||||
if (itemSectionRenderer == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
JsonObject showingResultsForRenderer = itemSectionRenderer.getArray("contents").getObject(0)
|
||||
.getObject("showingResultsForRenderer");
|
||||
return showingResultsForRenderer != null;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public InfoItemsPage<InfoItem> getInitialPage() throws ExtractionException, IOException {
|
||||
|
|
|
@ -2,7 +2,6 @@ package org.schabi.newpipe.extractor.services.youtube.extractors;
|
|||
|
||||
import com.grack.nanojson.JsonArray;
|
||||
import com.grack.nanojson.JsonObject;
|
||||
|
||||
import org.schabi.newpipe.extractor.InfoItem;
|
||||
import org.schabi.newpipe.extractor.StreamingService;
|
||||
import org.schabi.newpipe.extractor.downloader.Downloader;
|
||||
|
@ -12,10 +11,10 @@ import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
|
|||
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
|
||||
import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector;
|
||||
import org.schabi.newpipe.extractor.search.SearchExtractor;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.schabi.newpipe.extractor.utils.JsonUtils;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse;
|
||||
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
|
||||
|
@ -64,15 +63,25 @@ public class YoutubeSearchExtractor extends SearchExtractor {
|
|||
|
||||
@Override
|
||||
public String getSearchSuggestion() throws ParsingException {
|
||||
final JsonObject didYouMeanRenderer = initialData.getObject("contents")
|
||||
.getObject("twoColumnSearchResultsRenderer").getObject("primaryContents")
|
||||
.getObject("sectionListRenderer").getArray("contents").getObject(0)
|
||||
.getObject("itemSectionRenderer").getArray("contents").getObject(0)
|
||||
.getObject("didYouMeanRenderer");
|
||||
if (didYouMeanRenderer == null) {
|
||||
return "";
|
||||
}
|
||||
return JsonUtils.getString(didYouMeanRenderer, "correctedQueryEndpoint.searchEndpoint.query");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCorrectedSearch() {
|
||||
final JsonObject showingResultsForRenderer = initialData.getObject("contents")
|
||||
.getObject("twoColumnSearchResultsRenderer").getObject("primaryContents")
|
||||
.getObject("sectionListRenderer").getArray("contents").getObject(0)
|
||||
.getObject("itemSectionRenderer").getArray("contents").getObject(0)
|
||||
.getObject("showingResultsForRenderer");
|
||||
if (!showingResultsForRenderer.has("correctedQuery")) {
|
||||
return "";
|
||||
}
|
||||
return getTextFromObject(showingResultsForRenderer.getObject("correctedQuery"));
|
||||
return showingResultsForRenderer != null;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
|
|
@ -4,4 +4,5 @@ package org.schabi.newpipe.extractor.services;
|
|||
public interface BaseSearchExtractorTest extends BaseListExtractorTest {
|
||||
void testSearchString() throws Exception;
|
||||
void testSearchSuggestion() throws Exception;
|
||||
void testSearchCorrected() throws Exception;
|
||||
}
|
||||
|
|
|
@ -15,6 +15,10 @@ public abstract class DefaultSearchExtractorTest extends DefaultListExtractorTes
|
|||
public abstract String expectedSearchString();
|
||||
@Nullable public abstract String expectedSearchSuggestion();
|
||||
|
||||
public boolean isCorrectedSearch() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testSearchString() throws Exception {
|
||||
|
@ -31,4 +35,9 @@ public abstract class DefaultSearchExtractorTest extends DefaultListExtractorTes
|
|||
assertEquals(expectedSearchSuggestion, extractor().getSearchSuggestion());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchCorrected() throws Exception {
|
||||
assertEquals(isCorrectedSearch(), extractor().isCorrectedSearch());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -150,4 +150,27 @@ public class YoutubeMusicSearchExtractorTest {
|
|||
@Nullable @Override public String expectedSearchSuggestion() { return "mega man x3"; }
|
||||
@Override public InfoItem.InfoType expectedInfoItemType() { return InfoItem.InfoType.STREAM; }
|
||||
}
|
||||
|
||||
public static class CorrectedSearch extends DefaultSearchExtractorTest {
|
||||
private static SearchExtractor extractor;
|
||||
private static final String QUERY = "duo lipa";
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
NewPipe.init(DownloaderTestImpl.getInstance());
|
||||
extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), "");
|
||||
extractor.fetchPage();
|
||||
}
|
||||
|
||||
@Override public SearchExtractor extractor() { return extractor; }
|
||||
@Override public StreamingService expectedService() { return YouTube; }
|
||||
@Override public String expectedName() { return QUERY; }
|
||||
@Override public String expectedId() { return QUERY; }
|
||||
@Override public String expectedUrlContains() { return "music.youtube.com/search?q=" + URLEncoder.encode(QUERY); }
|
||||
@Override public String expectedOriginalUrlContains() { return "music.youtube.com/search?q=" + URLEncoder.encode(QUERY); }
|
||||
@Override public String expectedSearchString() { return QUERY; }
|
||||
@Nullable @Override public String expectedSearchSuggestion() { return null; }
|
||||
@Override public InfoItem.InfoType expectedInfoItemType() { return InfoItem.InfoType.STREAM; }
|
||||
@Override public boolean isCorrectedSearch() { return true; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -114,8 +114,8 @@ public class YoutubeSearchExtractorTest {
|
|||
|
||||
public static class Suggestion extends DefaultSearchExtractorTest {
|
||||
private static SearchExtractor extractor;
|
||||
private static final String QUERY = "pewdeipie";
|
||||
private static final String EXPECTED_SUGGESTION = "pewdiepie";
|
||||
private static final String QUERY = "newpip";
|
||||
private static final String EXPECTED_SUGGESTION = "newpipe";
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
|
@ -132,10 +132,32 @@ public class YoutubeSearchExtractorTest {
|
|||
@Override public String expectedOriginalUrlContains() { return "youtube.com/results?search_query=" + QUERY; }
|
||||
@Override public String expectedSearchString() { return QUERY; }
|
||||
@Nullable @Override public String expectedSearchSuggestion() { return EXPECTED_SUGGESTION; }
|
||||
|
||||
@Override public InfoItem.InfoType expectedInfoItemType() { return InfoItem.InfoType.STREAM; }
|
||||
}
|
||||
|
||||
public static class CorrectedSearch extends DefaultSearchExtractorTest {
|
||||
private static SearchExtractor extractor;
|
||||
private static final String QUERY = "pewdeipie";
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
NewPipe.init(DownloaderTestImpl.getInstance());
|
||||
extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), "");
|
||||
extractor.fetchPage();
|
||||
}
|
||||
|
||||
@Override public SearchExtractor extractor() { return extractor; }
|
||||
@Override public StreamingService expectedService() { return YouTube; }
|
||||
@Override public String expectedName() { return QUERY; }
|
||||
@Override public String expectedId() { return QUERY; }
|
||||
@Override public String expectedUrlContains() { return "youtube.com/results?search_query=" + QUERY; }
|
||||
@Override public String expectedOriginalUrlContains() { return "youtube.com/results?search_query=" + QUERY; }
|
||||
@Override public String expectedSearchString() { return QUERY; }
|
||||
@Nullable @Override public String expectedSearchSuggestion() { return null; }
|
||||
@Override public InfoItem.InfoType expectedInfoItemType() { return InfoItem.InfoType.STREAM; }
|
||||
@Override public boolean isCorrectedSearch() { return true; }
|
||||
}
|
||||
|
||||
public static class RandomQueryNoMorePages extends DefaultSearchExtractorTest {
|
||||
private static SearchExtractor extractor;
|
||||
private static final String QUERY = "UCO6AK";
|
||||
|
|
Loading…
Reference in New Issue