Skip to content

Commit 408f042

Browse files
mauriciocolliTobiGr
authored andcommitted
[YouTube] Fix bug when url isn't present in the browseEndpoint object
1 parent 342bdbb commit 408f042

5 files changed

Lines changed: 27 additions & 10 deletions

File tree

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
@@ -63,7 +63,7 @@ public String getUrl() throws ParsingException {
6363
}
6464

6565
@Override
66-
public String getSearchSuggestion() {
66+
public String getSearchSuggestion() throws ParsingException {
6767
JsonObject showingResultsForRenderer = initialData.getObject("contents")
6868
.getObject("twoColumnSearchResultsRenderer").getObject("primaryContents")
6969
.getObject("sectionListRenderer").getArray("contents").getObject(0)
@@ -114,7 +114,7 @@ public InfoItemsPage<InfoItem> getPage(String pageUrl) throws IOException, Extra
114114
return new InfoItemsPage<>(collector, getNextPageUrlFrom(itemSectionRenderer.getArray("continuations")));
115115
}
116116

117-
private void collectStreamsFrom(InfoItemsSearchCollector collector, JsonArray videos) throws NothingFoundException {
117+
private void collectStreamsFrom(InfoItemsSearchCollector collector, JsonArray videos) throws NothingFoundException, ParsingException {
118118
collector.reset();
119119

120120
final TimeAgoParser timeAgoParser = getTimeAgoParser();

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,12 @@ public StreamInfoItemsCollector getRelatedStreams() throws ExtractionException {
564564
*/
565565
@Override
566566
public String getErrorMessage() {
567-
return getTextFromObject(initialAjaxJson.getObject(2).getObject("playerResponse").getObject("playabilityStatus")
568-
.getObject("errorScreen").getObject("playerErrorMessageRenderer").getObject("reason"));
567+
try {
568+
return getTextFromObject(initialAjaxJson.getObject(2).getObject("playerResponse").getObject("playabilityStatus")
569+
.getObject("errorScreen").getObject("playerErrorMessageRenderer").getObject("reason"));
570+
} catch (ParsingException e) {
571+
return null;
572+
}
569573
}
570574

571575
/*//////////////////////////////////////////////////////////////////////////

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeParsingHelper.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public static String getClientVersion() throws IOException, ExtractionException
253253
throw new ParsingException("Could not get client version");
254254
}
255255

256-
public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint) {
256+
public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint) throws ParsingException {
257257
if (navigationEndpoint.getObject("urlEndpoint") != null) {
258258
String internUrl = navigationEndpoint.getObject("urlEndpoint").getString("url");
259259
if (internUrl.startsWith("/redirect?")) {
@@ -275,7 +275,20 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
275275
return internUrl;
276276
}
277277
} else if (navigationEndpoint.getObject("browseEndpoint") != null) {
278-
return "https://www.youtube.com" + navigationEndpoint.getObject("browseEndpoint").getString("canonicalBaseUrl");
278+
final JsonObject browseEndpoint = navigationEndpoint.getObject("browseEndpoint");
279+
final String canonicalBaseUrl = browseEndpoint.getString("canonicalBaseUrl");
280+
final String browseId = browseEndpoint.getString("browseId");
281+
282+
// All channel ids are prefixed with UC
283+
if (browseId != null && browseId.startsWith("UC")) {
284+
return "https://www.youtube.com/channel/" + browseId;
285+
}
286+
287+
if (canonicalBaseUrl != null && !canonicalBaseUrl.isEmpty()) {
288+
return "https://www.youtube.com" + canonicalBaseUrl;
289+
}
290+
291+
throw new ParsingException("canonicalBaseUrl is null and browseId is not a channel (\"" + browseEndpoint + "\")");
279292
} else if (navigationEndpoint.getObject("watchEndpoint") != null) {
280293
StringBuilder url = new StringBuilder();
281294
url.append("https://www.youtube.com/watch?v=").append(navigationEndpoint.getObject("watchEndpoint").getString("videoId"));
@@ -288,7 +301,7 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
288301
return null;
289302
}
290303

291-
public static String getTextFromObject(JsonObject textObject, boolean html) {
304+
public static String getTextFromObject(JsonObject textObject, boolean html) throws ParsingException {
292305
if (textObject.has("simpleText")) return textObject.getString("simpleText");
293306

294307
StringBuilder textBuilder = new StringBuilder();
@@ -314,7 +327,7 @@ public static String getTextFromObject(JsonObject textObject, boolean html) {
314327
return text;
315328
}
316329

317-
public static String getTextFromObject(JsonObject textObject) {
330+
public static String getTextFromObject(JsonObject textObject) throws ParsingException {
318331
return getTextFromObject(textObject, false);
319332
}
320333

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubePlaylistExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void testBannerUrl() throws Exception {
9999

100100
@Test
101101
public void testUploaderUrl() throws Exception {
102-
assertEquals("https://www.youtube.com/user/andre0y0you", extractor.getUploaderUrl());
102+
assertEquals("https://www.youtube.com/channel/UCs72iRpTEuwV3y6pdWYLgiw", extractor.getUploaderUrl());
103103
}
104104

105105
@Test

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorDefaultTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void testGetSecondPage() throws Exception {
124124
}
125125

126126
@Test
127-
public void testSuggestionNotNull() {
127+
public void testSuggestionNotNull() throws Exception {
128128
//todo write a real test
129129
assertNotNull(extractor.getSearchSuggestion());
130130
}

0 commit comments

Comments
 (0)