Improve code for YouTube Music search

This commit is contained in:
wb9688 2020-03-20 19:14:53 +01:00
parent dc29d87962
commit d58c0f230d
2 changed files with 38 additions and 12 deletions

View File

@ -308,7 +308,9 @@ public class YoutubeSearchExtractor extends SearchExtractor {
throw new ParsingException("Could not parse JSON", e); throw new ParsingException("Could not parse JSON", e);
} }
if (ajaxJson.getObject("continuationContents") == null) return new InfoItemsPage<>(collector, null); if (ajaxJson.getObject("continuationContents") == null) {
return InfoItemsPage.emptyPage();
}
JsonObject musicShelfContinuation = ajaxJson.getObject("continuationContents").getObject("musicShelfContinuation"); JsonObject musicShelfContinuation = ajaxJson.getObject("continuationContents").getObject("musicShelfContinuation");
@ -329,8 +331,7 @@ public class YoutubeSearchExtractor extends SearchExtractor {
private boolean isMusicSearch() { private boolean isMusicSearch() {
final List<String> contentFilters = getLinkHandler().getContentFilters(); final List<String> contentFilters = getLinkHandler().getContentFilters();
if (contentFilters.size() > 0 && contentFilters.get(0).startsWith("music_")) return true; return contentFilters.size() > 0 && contentFilters.get(0).startsWith("music_");
return false;
} }
private void collectStreamsFrom(InfoItemsSearchCollector collector, JsonArray videos) throws NothingFoundException, ParsingException { private void collectStreamsFrom(InfoItemsSearchCollector collector, JsonArray videos) throws NothingFoundException, ParsingException {
@ -391,6 +392,16 @@ public class YoutubeSearchExtractor extends SearchExtractor {
throw new ParsingException("Could not get uploader name"); throw new ParsingException("Could not get uploader name");
} }
@Override
public String getUploaderUrl() throws ParsingException {
if (searchType.equals(MUSIC_VIDEOS)) return null;
String url = getUrlFromNavigationEndpoint(info.getArray("flexColumns")
.getObject(1).getObject("musicResponsiveListItemFlexColumnRenderer")
.getObject("text").getArray("runs").getObject(0).getObject("navigationEndpoint"));
if (url != null && !url.isEmpty()) return url;
throw new ParsingException("Could not get uploader url");
}
@Override @Override
public String getTextualUploadDate() { public String getTextualUploadDate() {
return null; return null;
@ -413,9 +424,12 @@ public class YoutubeSearchExtractor extends SearchExtractor {
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
try { try {
// TODO: Don't simply get the first item, but look at all thumbnails and their resolution JsonArray thumbnails = info.getObject("thumbnail").getObject("musicThumbnailRenderer")
return fixThumbnailUrl(info.getObject("thumbnail").getObject("musicThumbnailRenderer") .getObject("thumbnail").getArray("thumbnails");
.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url")); // the last thumbnail is the one with the highest resolution
String url = thumbnails.getObject(thumbnails.size() - 1).getString("url");
return fixThumbnailUrl(url);
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Could not get thumbnail url", e); throw new ParsingException("Could not get thumbnail url", e);
} }
@ -426,9 +440,12 @@ public class YoutubeSearchExtractor extends SearchExtractor {
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
try { try {
// TODO: Don't simply get the first item, but look at all thumbnails and their resolution JsonArray thumbnails = info.getObject("thumbnail").getObject("musicThumbnailRenderer")
return fixThumbnailUrl(info.getObject("thumbnail").getObject("musicThumbnailRenderer") .getObject("thumbnail").getArray("thumbnails");
.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url")); // the last thumbnail is the one with the highest resolution
String url = thumbnails.getObject(thumbnails.size() - 1).getString("url");
return fixThumbnailUrl(url);
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Could not get thumbnail url", e); throw new ParsingException("Could not get thumbnail url", e);
} }
@ -472,9 +489,12 @@ public class YoutubeSearchExtractor extends SearchExtractor {
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
try { try {
// TODO: Don't simply get the first item, but look at all thumbnails and their resolution JsonArray thumbnails = info.getObject("thumbnail").getObject("musicThumbnailRenderer")
return fixThumbnailUrl(info.getObject("thumbnail").getObject("musicThumbnailRenderer") .getObject("thumbnail").getArray("thumbnails");
.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url")); // the last thumbnail is the one with the highest resolution
String url = thumbnails.getObject(thumbnails.size() - 1).getString("url");
return fixThumbnailUrl(url);
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Could not get thumbnail url", e); throw new ParsingException("Could not get thumbnail url", e);
} }

View File

@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; 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 extends YoutubeSearchExtractorBaseTest {
@BeforeClass @BeforeClass
@ -30,6 +31,11 @@ public class YoutubeSearchExtractorMusicTest extends YoutubeSearchExtractorBaseT
itemsPage = extractor.getInitialPage(); itemsPage = extractor.getInitialPage();
} }
@Test
public void testRelatedItems() throws Exception {
defaultTestRelatedItems(extractor);
}
@Test @Test
public void testGetSecondPage() throws Exception { public void testGetSecondPage() throws Exception {
YoutubeSearchExtractor secondExtractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("mocromaniac", YoutubeSearchExtractor secondExtractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("mocromaniac",