mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2025-04-29 00:10:35 +05:30
[YouTube] Fix bug when url isn't present in the browseEndpoint object
This commit is contained in:
parent
342bdbb852
commit
408f042127
@ -63,7 +63,7 @@ public class YoutubeSearchExtractor extends SearchExtractor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSearchSuggestion() {
|
public String getSearchSuggestion() throws ParsingException {
|
||||||
JsonObject showingResultsForRenderer = initialData.getObject("contents")
|
JsonObject showingResultsForRenderer = initialData.getObject("contents")
|
||||||
.getObject("twoColumnSearchResultsRenderer").getObject("primaryContents")
|
.getObject("twoColumnSearchResultsRenderer").getObject("primaryContents")
|
||||||
.getObject("sectionListRenderer").getArray("contents").getObject(0)
|
.getObject("sectionListRenderer").getArray("contents").getObject(0)
|
||||||
@ -114,7 +114,7 @@ public class YoutubeSearchExtractor extends SearchExtractor {
|
|||||||
return new InfoItemsPage<>(collector, getNextPageUrlFrom(itemSectionRenderer.getArray("continuations")));
|
return new InfoItemsPage<>(collector, getNextPageUrlFrom(itemSectionRenderer.getArray("continuations")));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void collectStreamsFrom(InfoItemsSearchCollector collector, JsonArray videos) throws NothingFoundException {
|
private void collectStreamsFrom(InfoItemsSearchCollector collector, JsonArray videos) throws NothingFoundException, ParsingException {
|
||||||
collector.reset();
|
collector.reset();
|
||||||
|
|
||||||
final TimeAgoParser timeAgoParser = getTimeAgoParser();
|
final TimeAgoParser timeAgoParser = getTimeAgoParser();
|
||||||
|
@ -564,8 +564,12 @@ public class YoutubeStreamExtractor extends StreamExtractor {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String getErrorMessage() {
|
public String getErrorMessage() {
|
||||||
return getTextFromObject(initialAjaxJson.getObject(2).getObject("playerResponse").getObject("playabilityStatus")
|
try {
|
||||||
.getObject("errorScreen").getObject("playerErrorMessageRenderer").getObject("reason"));
|
return getTextFromObject(initialAjaxJson.getObject(2).getObject("playerResponse").getObject("playabilityStatus")
|
||||||
|
.getObject("errorScreen").getObject("playerErrorMessageRenderer").getObject("reason"));
|
||||||
|
} catch (ParsingException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -253,7 +253,7 @@ public class YoutubeParsingHelper {
|
|||||||
throw new ParsingException("Could not get client version");
|
throw new ParsingException("Could not get client version");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint) {
|
public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint) throws ParsingException {
|
||||||
if (navigationEndpoint.getObject("urlEndpoint") != null) {
|
if (navigationEndpoint.getObject("urlEndpoint") != null) {
|
||||||
String internUrl = navigationEndpoint.getObject("urlEndpoint").getString("url");
|
String internUrl = navigationEndpoint.getObject("urlEndpoint").getString("url");
|
||||||
if (internUrl.startsWith("/redirect?")) {
|
if (internUrl.startsWith("/redirect?")) {
|
||||||
@ -275,7 +275,20 @@ public class YoutubeParsingHelper {
|
|||||||
return internUrl;
|
return internUrl;
|
||||||
}
|
}
|
||||||
} else if (navigationEndpoint.getObject("browseEndpoint") != null) {
|
} else if (navigationEndpoint.getObject("browseEndpoint") != null) {
|
||||||
return "https://www.youtube.com" + navigationEndpoint.getObject("browseEndpoint").getString("canonicalBaseUrl");
|
final JsonObject browseEndpoint = navigationEndpoint.getObject("browseEndpoint");
|
||||||
|
final String canonicalBaseUrl = browseEndpoint.getString("canonicalBaseUrl");
|
||||||
|
final String browseId = browseEndpoint.getString("browseId");
|
||||||
|
|
||||||
|
// All channel ids are prefixed with UC
|
||||||
|
if (browseId != null && browseId.startsWith("UC")) {
|
||||||
|
return "https://www.youtube.com/channel/" + browseId;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (canonicalBaseUrl != null && !canonicalBaseUrl.isEmpty()) {
|
||||||
|
return "https://www.youtube.com" + canonicalBaseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new ParsingException("canonicalBaseUrl is null and browseId is not a channel (\"" + browseEndpoint + "\")");
|
||||||
} else if (navigationEndpoint.getObject("watchEndpoint") != null) {
|
} else if (navigationEndpoint.getObject("watchEndpoint") != null) {
|
||||||
StringBuilder url = new StringBuilder();
|
StringBuilder url = new StringBuilder();
|
||||||
url.append("https://www.youtube.com/watch?v=").append(navigationEndpoint.getObject("watchEndpoint").getString("videoId"));
|
url.append("https://www.youtube.com/watch?v=").append(navigationEndpoint.getObject("watchEndpoint").getString("videoId"));
|
||||||
@ -288,7 +301,7 @@ public class YoutubeParsingHelper {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getTextFromObject(JsonObject textObject, boolean html) {
|
public static String getTextFromObject(JsonObject textObject, boolean html) throws ParsingException {
|
||||||
if (textObject.has("simpleText")) return textObject.getString("simpleText");
|
if (textObject.has("simpleText")) return textObject.getString("simpleText");
|
||||||
|
|
||||||
StringBuilder textBuilder = new StringBuilder();
|
StringBuilder textBuilder = new StringBuilder();
|
||||||
@ -314,7 +327,7 @@ public class YoutubeParsingHelper {
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getTextFromObject(JsonObject textObject) {
|
public static String getTextFromObject(JsonObject textObject) throws ParsingException {
|
||||||
return getTextFromObject(textObject, false);
|
return getTextFromObject(textObject, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ public class YoutubePlaylistExtractorTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUploaderUrl() throws Exception {
|
public void testUploaderUrl() throws Exception {
|
||||||
assertEquals("https://www.youtube.com/user/andre0y0you", extractor.getUploaderUrl());
|
assertEquals("https://www.youtube.com/channel/UCs72iRpTEuwV3y6pdWYLgiw", extractor.getUploaderUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -124,7 +124,7 @@ public class YoutubeSearchExtractorDefaultTest extends YoutubeSearchExtractorBas
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSuggestionNotNull() {
|
public void testSuggestionNotNull() throws Exception {
|
||||||
//todo write a real test
|
//todo write a real test
|
||||||
assertNotNull(extractor.getSearchSuggestion());
|
assertNotNull(extractor.getSearchSuggestion());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user