Skip to content

Commit 81459e2

Browse files
committed
Fix age restricted YouTube videos
1 parent 186193d commit 81459e2

1 file changed

Lines changed: 48 additions & 17 deletions

File tree

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

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
2222
import org.schabi.newpipe.extractor.localization.TimeAgoPatternsManager;
2323
import org.schabi.newpipe.extractor.services.youtube.ItagItem;
24-
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
2524
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
25+
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
2626
import org.schabi.newpipe.extractor.stream.AudioStream;
2727
import org.schabi.newpipe.extractor.stream.Description;
2828
import org.schabi.newpipe.extractor.stream.Frameset;
@@ -52,7 +52,10 @@
5252
import javax.annotation.Nonnull;
5353
import javax.annotation.Nullable;
5454

55-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*;
55+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.fixThumbnailUrl;
56+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse;
57+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
58+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
5659
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
5760
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
5861

@@ -115,7 +118,11 @@ public YoutubeStreamExtractor(StreamingService service, LinkHandler linkHandler)
115118
@Override
116119
public String getName() throws ParsingException {
117120
assertPageFetched();
118-
String title = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("title"));
121+
String title = null;
122+
123+
try {
124+
title = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("title"));
125+
} catch (ParsingException ignored) { }
119126

120127
if (isNullOrEmpty(title)) {
121128
title = playerResponse.getObject("videoDetails").getString("title");
@@ -193,11 +200,13 @@ public String getThumbnailUrl() throws ParsingException {
193200

194201
@Nonnull
195202
@Override
196-
public Description getDescription() throws ParsingException {
203+
public Description getDescription() {
197204
assertPageFetched();
198205
// description with more info on links
199-
String description = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("description"), true);
200-
if (description != null && !description.isEmpty()) return new Description(description, Description.HTML);
206+
try {
207+
String description = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("description"), true);
208+
if (description != null && !description.isEmpty()) return new Description(description, Description.HTML);
209+
} catch (ParsingException ignored) { }
201210

202211
// raw non-html description
203212
return new Description(playerResponse.getObject("videoDetails").getString("shortDescription"), Description.PLAIN_TEXT);
@@ -246,8 +255,12 @@ public long getTimeStamp() throws ParsingException {
246255
@Override
247256
public long getViewCount() throws ParsingException {
248257
assertPageFetched();
249-
String views = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("viewCount")
258+
String views = null;
259+
260+
try {
261+
views = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("viewCount")
250262
.getObject("videoViewCountRenderer").getObject("viewCount"));
263+
} catch (ParsingException ignored) {}
251264

252265
if (isNullOrEmpty(views)) {
253266
views = playerResponse.getObject("videoDetails").getString("viewCount");
@@ -279,6 +292,7 @@ public long getLikeCount() throws ParsingException {
279292
} catch (NumberFormatException nfe) {
280293
throw new ParsingException("Could not parse \"" + likesString + "\" as an Integer", nfe);
281294
} catch (Exception e) {
295+
if (ageLimit == 18) return -1;
282296
throw new ParsingException("Could not get like count", e);
283297
}
284298
}
@@ -302,6 +316,7 @@ public long getDislikeCount() throws ParsingException {
302316
} catch (NumberFormatException nfe) {
303317
throw new ParsingException("Could not parse \"" + dislikesString + "\" as an Integer", nfe);
304318
} catch (Exception e) {
319+
if (ageLimit == 18) return -1;
305320
throw new ParsingException("Could not get dislike count", e);
306321
}
307322
}
@@ -311,14 +326,18 @@ public long getDislikeCount() throws ParsingException {
311326
public String getUploaderUrl() throws ParsingException {
312327
assertPageFetched();
313328

329+
try {
314330
String uploaderUrl = getUrlFromNavigationEndpoint(getVideoSecondaryInfoRenderer()
315331
.getObject("owner").getObject("videoOwnerRenderer").getObject("navigationEndpoint"));
316-
if (uploaderUrl != null && !uploaderUrl.isEmpty()) return uploaderUrl;
317-
332+
if (!isNullOrEmpty(uploaderUrl)) {
333+
return uploaderUrl;
334+
}
335+
} catch (ParsingException ignored) { }
318336

319337
String uploaderId = playerResponse.getObject("videoDetails").getString("channelId");
320-
if (uploaderId != null && !uploaderId.isEmpty())
338+
if (!isNullOrEmpty(uploaderId)) {
321339
return YoutubeChannelLinkHandlerFactory.getInstance().getUrl("channel/" + uploaderId);
340+
}
322341

323342
throw new ParsingException("Could not get uploader url");
324343
}
@@ -327,8 +346,13 @@ public String getUploaderUrl() throws ParsingException {
327346
@Override
328347
public String getUploaderName() throws ParsingException {
329348
assertPageFetched();
330-
String uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner")
349+
350+
String uploaderName = null;
351+
352+
try {
353+
uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner")
331354
.getObject("videoOwnerRenderer").getObject("title"));
355+
} catch (ParsingException ignored) { }
332356

333357
if (isNullOrEmpty(uploaderName)) {
334358
uploaderName = playerResponse.getObject("videoDetails").getString("author");
@@ -343,14 +367,20 @@ public String getUploaderName() throws ParsingException {
343367
@Override
344368
public String getUploaderAvatarUrl() throws ParsingException {
345369
assertPageFetched();
370+
371+
String url = null;
372+
346373
try {
347-
String url = getVideoSecondaryInfoRenderer().getObject("owner").getObject("videoOwnerRenderer")
374+
url = getVideoSecondaryInfoRenderer().getObject("owner").getObject("videoOwnerRenderer")
348375
.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");
376+
} catch (ParsingException ignored) { }
349377

350-
return fixThumbnailUrl(url);
351-
} catch (Exception e) {
352-
throw new ParsingException("Could not get uploader avatar url", e);
378+
if (isNullOrEmpty(url)) {
379+
if (ageLimit == 18) return "";
380+
throw new ParsingException("Could not get uploader avatar URL");
353381
}
382+
383+
return fixThumbnailUrl(url);
354384
}
355385

356386
@Nonnull
@@ -872,7 +902,7 @@ private JsonObject getVideoPrimaryInfoRenderer() throws ParsingException {
872902
}
873903
}
874904

875-
if (videoPrimaryInfoRenderer == null) {
905+
if (isNullOrEmpty(videoPrimaryInfoRenderer)) {
876906
throw new ParsingException("Could not find videoPrimaryInfoRenderer");
877907
}
878908

@@ -894,7 +924,7 @@ private JsonObject getVideoSecondaryInfoRenderer() throws ParsingException {
894924
}
895925
}
896926

897-
if (videoSecondaryInfoRenderer == null) {
927+
if (isNullOrEmpty(videoSecondaryInfoRenderer)) {
898928
throw new ParsingException("Could not find videoSecondaryInfoRenderer");
899929
}
900930

@@ -904,6 +934,7 @@ private JsonObject getVideoSecondaryInfoRenderer() throws ParsingException {
904934

905935
@Nonnull
906936
private static String getVideoInfoUrl(final String id, final String sts) {
937+
// TODO: Try parsing embedded_player_response first
907938
return "https://www.youtube.com/get_video_info?" + "video_id=" + id +
908939
"&eurl=https://youtube.googleapis.com/v/" + id +
909940
"&sts=" + sts + "&ps=default&gl=US&hl=en";

0 commit comments

Comments
 (0)