|
private static class HopperBotPlaylistSong { |
|
@NotNull final String filename; |
|
@NotNull final String title; |
|
@NotNull final List<String> authors = new ArrayList<>(); |
|
@NotNull final List<String> singers = new ArrayList<>(); |
|
@NotNull final String url; |
|
@Nullable final String note; |
|
@Nullable final String lyrics; |
|
@Nullable final List<MessageEmbed> lyricEmbeds; |
|
final MessageEmbed songInfoEmbed; |
|
|
|
HopperBotPlaylistSong(@NotNull String filename, @NotNull Map<String,JsonNode> songJsonData, @NotNull PlaylistFeature playlistFeature) { |
|
this.filename = filename; |
|
title = songJsonData.get("Title").textValue(); |
|
songJsonData.get("Authors").elements().forEachRemaining(author -> authors.add(author.textValue())); |
|
songJsonData.get("Singers").elements().forEachRemaining(singer -> singers.add(singer.textValue())); |
|
url = songJsonData.get("URL").textValue(); |
|
final String note = songJsonData.get("Note").textValue(); |
|
this.note = note.equals("") ? null : note; |
|
|
|
final Iterator<JsonNode> lyricsIterator = songJsonData.get("Lyrics").elements(); |
|
if (lyricsIterator.hasNext()) { |
|
final List<String> lyricsLines = new ArrayList<>(); |
|
lyricsIterator.forEachRemaining(lyric -> lyricsLines.add(lyric.textValue())); |
|
lyrics = String.join("\n",lyricsLines); |
|
|
|
lyricEmbeds = new ArrayList<>(); |
|
final List<String> lyricPages = new ArrayList<>(); |
|
for (String verse : lyrics.split("\n\n")) { |
|
if (verse.length() >= 1024) { |
|
String lastItem = ""; |
|
lyricPages.add(lastItem); |
|
for (String line : verse.split("\n")) { |
|
if (lastItem.length() + line.length() <= 1024) { |
|
lyricPages.set(lyricPages.size()-1,lastItem+"\n"+line); |
|
} else { |
|
lyricPages.add(line); |
|
lastItem = line; |
|
} |
|
} |
|
} else { |
|
lyricPages.add(verse); |
|
} |
|
} |
|
int index = 0; |
|
for (String lyricPage : lyricPages) { |
|
final EmbedBuilder embedBuilder = playlistFeature.pagedEmbedBase("Lyrics",index++,lyricPages.size()); |
|
lyricEmbeds.add(embedBuilder.addField(strippedFilename(),lyricPage,false).build()); |
|
} |
|
} else { |
|
lyrics = null; |
|
lyricEmbeds = null; |
|
} |
|
|
|
songInfoEmbed = addBooleanField( |
|
addNullableField( |
|
addListField( |
|
addListField( |
|
getEmbedBase() |
|
.setTitle("Song info") |
|
.setAuthor(strippedFilename() + " (Click for YouTube video)",fullURL()) |
|
.setImage(thumbnail()) |
|
.addField("Title",title,false), |
|
"Author", authors, true |
|
), "Singer", singers, true |
|
), "Note", note, false |
|
), "Lyrics available (/lyrics [song])", lyrics != null, false |
|
).build(); |
|
} |
|
|
|
@NotNull |
|
public String strippedFilename() { |
|
return removeExtension(filename); |
|
} |
|
|
|
@NotNull |
|
public String thumbnail() { |
|
return "https://i.ytimg.com/vi/"+url+"/hqdefault.jpg"; |
|
} |
|
|
|
@NotNull |
|
public String fullURL() { |
|
return "https://www.youtube.com/watch?v="+url; |
|
} |
|
|
|
@NotNull |
|
private EmbedBuilder addListField(@NotNull EmbedBuilder embedBuilder, @NotNull String name, @NotNull List<String> values, boolean inline) { |
|
if (values.isEmpty()) { |
|
return embedBuilder; |
|
} |
|
if (values.size() == 1) { |
|
return embedBuilder.addField(name,values.get(0),true); |
|
} |
|
return embedBuilder.addField(name+"s",String.join(", ",values),inline); |
|
} |
|
@NotNull |
|
private EmbedBuilder addNullableField(@NotNull EmbedBuilder embedBuilder, @NotNull String name, @Nullable String value, boolean inline) { |
|
if (value == null || value.equals("")) { |
|
return embedBuilder; |
|
} |
|
return embedBuilder.addField(name,value,inline); |
|
} |
|
@NotNull |
|
private EmbedBuilder addBooleanField(@NotNull EmbedBuilder embedBuilder, @NotNull String name, boolean value, boolean inline) { |
|
return embedBuilder.addField(name+"?",value ? "Yes" : "No",inline); |
|
} |
|
} |