Skip to content

Commit

Permalink
[YouTube] Simplify Optional chains in channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Aug 12, 2023
1 parent 2f7bfd3 commit 44b664a
Showing 1 changed file with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,24 @@ public String getName() throws ParsingException {
return metadataRendererTitle;
}

return channelHeader.flatMap(header -> {
return channelHeader.map(header -> {
final JsonObject channelJson = header.json;
switch (header.headerType) {
case PAGE:
return Optional.ofNullable(channelJson.getObject("content")
return channelJson.getObject("content")
.getObject("pageHeaderViewModel")
.getObject("title")
.getObject("dynamicTextViewModel")
.getObject("text")
.getString("content", channelJson.getString("pageTitle")));
.getString("content", channelJson.getString("pageTitle"));

case CAROUSEL:
case INTERACTIVE_TABBED:
return Optional.ofNullable(getTextFromObject(channelJson.getObject("title")));
return getTextFromObject(channelJson.getObject("title"));

case C4_TABBED:
default:
return Optional.ofNullable(channelJson.getString("title"));
return channelJson.getString("title");
}
})
// The channel name from a microformatDataRenderer may be different from the one displayed,
Expand All @@ -200,36 +203,34 @@ public String getAvatarUrl() throws ParsingException {
}

return channelHeader.map(header -> {
final HeaderType headerType = header.headerType;
if (headerType == HeaderType.PAGE) {
return Optional.ofNullable(header.json.getObject("content")
.getObject("pageHeaderViewModel")
.getObject("image")
.getObject("contentPreviewImageViewModel")
.getObject("image")
.getArray("sources")
.getObject(0)
.getString("url"))
.map(YoutubeParsingHelper::fixThumbnailUrl)
.orElse(null);
}
switch (header.headerType) {
case PAGE:
return header.json.getObject("content")
.getObject("pageHeaderViewModel")
.getObject("image")
.getObject("contentPreviewImageViewModel")
.getObject("image")
.getArray("sources")
.getObject(0)
.getString("url");

if (headerType == HeaderType.INTERACTIVE_TABBED) {
return Optional.ofNullable(header.json.getObject("boxArt")
.getArray("thumbnails")
.getObject(0)
.getString("url"))
.map(YoutubeParsingHelper::fixThumbnailUrl)
.orElse(null);
}
case INTERACTIVE_TABBED:
return header.json.getObject("boxArt")
.getArray("thumbnails")
.getObject(0)
.getString("url");

return Optional.ofNullable(header.json.getObject("avatar")
.getArray("thumbnails")
.getObject(0)
.getString("url"))
.map(YoutubeParsingHelper::fixThumbnailUrl)
.orElse(null);
}).orElseThrow(() -> new ParsingException("Could not get avatar URL"));
case C4_TABBED:
case CAROUSEL:
default:
return header.json.getObject("avatar")
.getArray("thumbnails")
.getObject(0)
.getString("url");
}
})
.map(YoutubeParsingHelper::fixThumbnailUrl)
.orElseThrow(() -> new ParsingException("Could not get avatar URL"));
}

@Override
Expand Down

0 comments on commit 44b664a

Please sign in to comment.