mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2025-04-28 16:00:33 +05:30
Refactor YouTube Music search tests
This commit is contained in:
parent
dd434cca01
commit
aa8cea47f3
@ -395,9 +395,11 @@ public class YoutubeSearchExtractor extends SearchExtractor {
|
|||||||
@Override
|
@Override
|
||||||
public String getUploaderUrl() throws ParsingException {
|
public String getUploaderUrl() throws ParsingException {
|
||||||
if (searchType.equals(MUSIC_VIDEOS)) return null;
|
if (searchType.equals(MUSIC_VIDEOS)) return null;
|
||||||
String url = getUrlFromNavigationEndpoint(info.getArray("flexColumns")
|
JsonObject navigationEndpoint = info.getArray("flexColumns")
|
||||||
.getObject(1).getObject("musicResponsiveListItemFlexColumnRenderer")
|
.getObject(1).getObject("musicResponsiveListItemFlexColumnRenderer")
|
||||||
.getObject("text").getArray("runs").getObject(0).getObject("navigationEndpoint"));
|
.getObject("text").getArray("runs").getObject(0).getObject("navigationEndpoint");
|
||||||
|
if (navigationEndpoint == null) return null;
|
||||||
|
String url = getUrlFromNavigationEndpoint(navigationEndpoint);
|
||||||
if (url != null && !url.isEmpty()) return url;
|
if (url != null && !url.isEmpty()) return url;
|
||||||
throw new ParsingException("Could not get uploader url");
|
throw new ParsingException("Could not get uploader url");
|
||||||
}
|
}
|
||||||
|
@ -37,11 +37,14 @@ public final class DefaultTests {
|
|||||||
if (item instanceof StreamInfoItem) {
|
if (item instanceof StreamInfoItem) {
|
||||||
StreamInfoItem streamInfoItem = (StreamInfoItem) item;
|
StreamInfoItem streamInfoItem = (StreamInfoItem) item;
|
||||||
assertNotEmpty("Uploader name not set: " + item, streamInfoItem.getUploaderName());
|
assertNotEmpty("Uploader name not set: " + item, streamInfoItem.getUploaderName());
|
||||||
assertNotEmpty("Uploader url not set: " + item, streamInfoItem.getUploaderUrl());
|
|
||||||
|
// assertNotEmpty("Uploader url not set: " + item, streamInfoItem.getUploaderUrl());
|
||||||
|
if (streamInfoItem.getUploaderUrl() != null && !streamInfoItem.getUploaderUrl().isEmpty()) {
|
||||||
assertIsSecureUrl(streamInfoItem.getUploaderUrl());
|
assertIsSecureUrl(streamInfoItem.getUploaderUrl());
|
||||||
|
assertExpectedLinkType(expectedService, streamInfoItem.getUploaderUrl(), LinkType.CHANNEL);
|
||||||
|
}
|
||||||
|
|
||||||
assertExpectedLinkType(expectedService, streamInfoItem.getUrl(), LinkType.STREAM);
|
assertExpectedLinkType(expectedService, streamInfoItem.getUrl(), LinkType.STREAM);
|
||||||
assertExpectedLinkType(expectedService, streamInfoItem.getUploaderUrl(), LinkType.CHANNEL);
|
|
||||||
|
|
||||||
final String textualUploadDate = streamInfoItem.getTextualUploadDate();
|
final String textualUploadDate = streamInfoItem.getTextualUploadDate();
|
||||||
if (textualUploadDate != null && !textualUploadDate.isEmpty()) {
|
if (textualUploadDate != null && !textualUploadDate.isEmpty()) {
|
||||||
|
@ -1,95 +1,63 @@
|
|||||||
package org.schabi.newpipe.extractor.services.youtube.search;
|
package org.schabi.newpipe.extractor.services.youtube.search;
|
||||||
|
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
|
||||||
import org.schabi.newpipe.DownloaderTestImpl;
|
import org.schabi.newpipe.DownloaderTestImpl;
|
||||||
import org.schabi.newpipe.extractor.InfoItem;
|
import org.schabi.newpipe.extractor.InfoItem;
|
||||||
import org.schabi.newpipe.extractor.ListExtractor;
|
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
import org.schabi.newpipe.extractor.NewPipe;
|
||||||
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeSearchExtractor;
|
import org.schabi.newpipe.extractor.StreamingService;
|
||||||
|
import org.schabi.newpipe.extractor.search.SearchExtractor;
|
||||||
|
import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest;
|
||||||
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory;
|
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URLEncoder;
|
||||||
import java.net.URLDecoder;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import javax.annotation.Nullable;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static java.util.Collections.singletonList;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
|
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
|
||||||
import static org.schabi.newpipe.extractor.services.DefaultTests.defaultTestRelatedItems;
|
|
||||||
|
|
||||||
public class YoutubeSearchExtractorMusicTest extends YoutubeSearchExtractorBaseTest {
|
public class YoutubeSearchExtractorMusicTest {
|
||||||
|
public static class MusicSongs extends DefaultSearchExtractorTest {
|
||||||
|
private static SearchExtractor extractor;
|
||||||
|
private static final String QUERY = "mocromaniac";
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUpClass() throws Exception {
|
public static void setUp() throws Exception {
|
||||||
NewPipe.init(DownloaderTestImpl.getInstance());
|
NewPipe.init(DownloaderTestImpl.getInstance());
|
||||||
extractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("mocromaniac",
|
extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), "");
|
||||||
asList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), null);
|
|
||||||
extractor.fetchPage();
|
extractor.fetchPage();
|
||||||
itemsPage = extractor.getInitialPage();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Override public SearchExtractor extractor() { return extractor; }
|
||||||
public void testRelatedItems() throws Exception {
|
@Override public StreamingService expectedService() { return YouTube; }
|
||||||
defaultTestRelatedItems(extractor);
|
@Override public String expectedName() { return QUERY; }
|
||||||
|
@Override public String expectedId() { return QUERY; }
|
||||||
|
@Override public String expectedUrlContains() { return "music.youtube.com/search?q=" + QUERY; }
|
||||||
|
@Override public String expectedOriginalUrlContains() { return "music.youtube.com/search?q=" + QUERY; }
|
||||||
|
@Override public String expectedSearchString() { return QUERY; }
|
||||||
|
@Nullable @Override public String expectedSearchSuggestion() { return null; }
|
||||||
|
@Override public InfoItem.InfoType expectedInfoItemType() { return InfoItem.InfoType.STREAM; }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
public static class Suggestion extends DefaultSearchExtractorTest {
|
||||||
public void testGetSecondPage() throws Exception {
|
private static SearchExtractor extractor;
|
||||||
YoutubeSearchExtractor secondExtractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("mocromaniac",
|
private static final String QUERY = "megaman x3";
|
||||||
asList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), null);
|
|
||||||
ListExtractor.InfoItemsPage<InfoItem> secondPage = secondExtractor.getPage(itemsPage.getNextPageUrl());
|
|
||||||
assertTrue(Integer.toString(secondPage.getItems().size()),
|
|
||||||
secondPage.getItems().size() > 10);
|
|
||||||
|
|
||||||
// check if its the same result
|
@BeforeClass
|
||||||
boolean equals = true;
|
public static void setUp() throws Exception {
|
||||||
for (int i = 0; i < secondPage.getItems().size()
|
NewPipe.init(DownloaderTestImpl.getInstance());
|
||||||
&& i < itemsPage.getItems().size(); i++) {
|
extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), "");
|
||||||
if (!secondPage.getItems().get(i).getUrl().equals(
|
extractor.fetchPage();
|
||||||
itemsPage.getItems().get(i).getUrl())) {
|
|
||||||
equals = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assertFalse("First and second page are equal", equals);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override public SearchExtractor extractor() { return extractor; }
|
||||||
@Test
|
@Override public StreamingService expectedService() { return YouTube; }
|
||||||
public void testUrl() throws Exception {
|
@Override public String expectedName() { return QUERY; }
|
||||||
assertTrue(extractor.getUrl(), extractor.getUrl().startsWith("https://music.youtube.com/search?q="));
|
@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); }
|
||||||
@Test
|
@Override public String expectedSearchString() { return QUERY; }
|
||||||
public void testGetSecondPageUrl() throws Exception {
|
@Nullable @Override public String expectedSearchSuggestion() { return "mega man x3"; }
|
||||||
URL url = new URL(extractor.getNextPageUrl());
|
@Override public InfoItem.InfoType expectedInfoItemType() { return InfoItem.InfoType.STREAM; }
|
||||||
|
|
||||||
assertEquals(url.getHost(), "music.youtube.com");
|
|
||||||
assertEquals(url.getPath(), "/youtubei/v1/search");
|
|
||||||
|
|
||||||
Map<String, String> queryPairs = new LinkedHashMap<>();
|
|
||||||
for (String queryPair : url.getQuery().split("&")) {
|
|
||||||
int index = queryPair.indexOf("=");
|
|
||||||
queryPairs.put(URLDecoder.decode(queryPair.substring(0, index), "UTF-8"),
|
|
||||||
URLDecoder.decode(queryPair.substring(index + 1), "UTF-8"));
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(queryPairs.get("ctoken"), queryPairs.get("continuation"));
|
|
||||||
assertTrue(queryPairs.get("continuation").length() > 5);
|
|
||||||
assertTrue(queryPairs.get("itct").length() > 5);
|
|
||||||
assertEquals("json", queryPairs.get("alt"));
|
|
||||||
assertTrue(queryPairs.get("key").length() > 5);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSuggestions() throws Exception {
|
|
||||||
YoutubeSearchExtractor newExtractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("megaman x3",
|
|
||||||
asList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), null);
|
|
||||||
newExtractor.fetchPage();
|
|
||||||
|
|
||||||
assertTrue(newExtractor.getInitialPage().getItems().size() > 10);
|
|
||||||
assertEquals("mega man x3", newExtractor.getSearchSuggestion());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user