Refactored and added a way to list a movie's subtitle languages by imdb id and title 2/2
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
5a85141611
commit
f0b757060a
@ -21,9 +21,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public abstract class Command {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ListLangMovieTitle.class.getCanonicalName());
|
||||
private static final Logger logger = LoggerFactory.getLogger(ListLangMovie.class.getCanonicalName());
|
||||
|
||||
protected final String name;
|
||||
protected final String description;
|
||||
@ -47,7 +48,7 @@ public abstract class Command {
|
||||
public abstract Mono<Void> execute(MessageCreateEvent event);
|
||||
|
||||
public String getUsage() {
|
||||
return this.usage;
|
||||
return "``" + this.usage + "``";
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
@ -68,31 +69,33 @@ public abstract class Command {
|
||||
.then();
|
||||
}
|
||||
|
||||
protected Mono<Void> badArgument(MessageCreateEvent event, String arg) {
|
||||
return event.getMessage().getChannel()
|
||||
.flatMap(channel ->
|
||||
channel.createMessage("Error, this argument isn't recognized: '" + arg + "'.\n" + getUsage()))
|
||||
.then();
|
||||
}
|
||||
|
||||
protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) {
|
||||
try {
|
||||
Movie movie = OMDBClient.getMovie(arg, isId);
|
||||
ListResponse<SubtitleInfo> subtitles =
|
||||
OpenSubtitles.searchSubtitles("",
|
||||
String.valueOf(movie.getNumericImdbId()));
|
||||
if (subtitles.getStatus().equals(ResponseStatus.OK)) {
|
||||
embed.setColor(Color.BISMARK);
|
||||
embed.setDescription("You requested a list of the languages available for a movie with this " +
|
||||
(isId ? "IMDB Identifier" : "title") + " '" + arg + "'. " + subtitles.getData().size()
|
||||
+ " subtitles were found.");
|
||||
embed.addField("Top 20 distinct languages (based on downloads)", subtitles.getData().stream()
|
||||
.sorted(Comparator.comparingInt(SubtitleInfo::getDownloadsNo))
|
||||
.filter(distinctByKey(SubtitleInfo::getLanguage))
|
||||
.map(subtitleInfo -> subtitleInfo.getSubtitleFileId()
|
||||
+ " - " + subtitleInfo.getLanguage())
|
||||
.limit(20)
|
||||
.collect(Collectors.joining("\n")), false);
|
||||
} else if (subtitles.getStatus().equals(ResponseStatus.INVALID_IMDB_ID)) {
|
||||
embed.setColor(Color.ORANGE).setDescription("It looks like this IMDB Identifier either isn't valid " +
|
||||
"or is not in the database.");
|
||||
if (movie != null) {
|
||||
ListResponse<SubtitleInfo> subtitles =
|
||||
OpenSubtitles.searchSubtitles("",
|
||||
String.valueOf(movie.getNumericImdbId()));
|
||||
if (subtitles.getStatus().equals(ResponseStatus.OK)) {
|
||||
handleOk(arg, embed, isId, movie, subtitles);
|
||||
} else if (subtitles.getStatus().equals(ResponseStatus.INVALID_IMDB_ID)) {
|
||||
embed.setColor(Color.ORANGE).setDescription("It looks like this IMDB Identifier either isn't valid " +
|
||||
"or is not in the database.");
|
||||
} else {
|
||||
logger.error("Cannot get subtitle list. Code returned: {}", subtitles.getStatus());
|
||||
embed.setDescription("I cannot get the subtitles right now, sorry! " +
|
||||
"Try again later or contact my administrator.");
|
||||
}
|
||||
} else {
|
||||
logger.error("Cannot get subtitle list. Code returned: {}", subtitles.getStatus());
|
||||
embed.setDescription("An error occurred. " +
|
||||
"Try again later or contact my administrator.");
|
||||
embed.setDescription("I didn't find any correspondence, sorry! If you know this is an error, please " +
|
||||
"contact my administrator!");
|
||||
}
|
||||
} catch (IOException | XmlRpcException e) {
|
||||
logger.error("A network error happened: {}", e.getMessage());
|
||||
@ -102,4 +105,29 @@ public abstract class Command {
|
||||
"Try again later or contact my administrator.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleOk(String arg, EmbedCreateSpec embed, boolean isId, Movie movie, ListResponse<SubtitleInfo> subtitles) {
|
||||
embed.setColor(Color.BISMARK);
|
||||
Stream<String> subtitlesStream = getSubtitleStream(subtitles);
|
||||
String formattedSubtitles = subtitlesStream.collect(Collectors.joining("\n"));
|
||||
embed.setDescription("You requested a list of the languages available for a movie with the " +
|
||||
(isId ? "IMDB Identifier" : "title") + " '" + arg + "'. ");
|
||||
embed.addField("Movie", movie.toString(), true);
|
||||
embed.addField("IMDB Link", "https://www.imdb.com/title/" + movie.getImdbID(), true);
|
||||
if (movie.getPoster() != null) embed.setThumbnail(movie.getPoster());
|
||||
embed.addField("Top 20 distinct languages (based on downloads)",
|
||||
formattedSubtitles.isBlank() ? "None." : formattedSubtitles, false);
|
||||
}
|
||||
|
||||
private Stream<String> getSubtitleStream(ListResponse<SubtitleInfo> subtitles) {
|
||||
return subtitles.getData().stream()
|
||||
.sorted(Comparator.comparingInt(SubtitleInfo::getDownloadsNo))
|
||||
.filter(distinctByKey(SubtitleInfo::getLanguage))
|
||||
.filter(subtitleInfo ->
|
||||
(subtitleInfo.getEncoding().equalsIgnoreCase("ascii")
|
||||
|| subtitleInfo.getEncoding().equalsIgnoreCase("cp1252")))
|
||||
.map(subtitleInfo -> subtitleInfo.getSubtitleFileId()
|
||||
+ " - " + subtitleInfo.getLanguage())
|
||||
.limit(20);
|
||||
}
|
||||
}
|
||||
|
@ -8,21 +8,27 @@ import org.slf4j.LoggerFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
import xyz.vallat.louis.subtitles.OpenSubtitles;
|
||||
|
||||
public class ListLangMovieTitle extends Command {
|
||||
import java.util.List;
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ListLangMovieTitle.class.getCanonicalName());
|
||||
public class ListLangMovie extends Command {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ListLangMovie.class.getCanonicalName());
|
||||
private static final ReactionEmoji WAITING = ReactionEmoji.unicode("⌛");
|
||||
|
||||
public ListLangMovieTitle(String name) {
|
||||
public ListLangMovie(String name) {
|
||||
super(name, "List all languages attached to a film title or an IMDB Identifier.",
|
||||
name + " imdb|title value", 3, 3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> execute(MessageCreateEvent event) {
|
||||
if (event.getMessage().getContent().split(" ").length < minArgs) return notEnoughArguments(event);
|
||||
boolean isId = event.getMessage().getContent().split(" ")
|
||||
String arg = event.getMessage().getContent().substring(name.length() + 1);
|
||||
List<String> arguments = List.of(event.getMessage().getContent().split(" "));
|
||||
if (arguments.size() < minArgs) return notEnoughArguments(event);
|
||||
else if (!arguments.get(1).equalsIgnoreCase("imdb") &&
|
||||
!arguments.get(1).equalsIgnoreCase("title"))
|
||||
return badArgument(event, arguments.get(1));
|
||||
boolean isId = arguments.get(1).equalsIgnoreCase("imdb");
|
||||
String arg = String.join(" ", arguments.subList(2, arguments.size()));
|
||||
if (arg.isBlank()) return notEnoughArguments(event);
|
||||
logger.debug("Executing command with argument '{}'.", arg);
|
||||
return event.getMessage().getChannel()
|
||||
@ -30,7 +36,7 @@ public class ListLangMovieTitle extends Command {
|
||||
.flatMap(messageChannel -> messageChannel.createEmbed(embed -> {
|
||||
embed.setTitle("Subtitle languages").setColor(Color.RED);
|
||||
if (OpenSubtitles.isLoggedIn()) {
|
||||
createEmbedListLang(arg, embed, false);
|
||||
createEmbedListLang(arg, embed, isId);
|
||||
} else {
|
||||
logger.error("Not logged in on OpenSubtitles!");
|
||||
logger.warn("It may be due to a website being down. If it's not" +
|
||||
|
@ -26,8 +26,7 @@ public final class DiscordManager {
|
||||
static {
|
||||
commands.put("ping", new Ping(PREFIX + "ping"));
|
||||
commands.put("version", new Version(PREFIX + "version"));
|
||||
commands.put("listLangMovieImdb", new ListLangMovieImdb(PREFIX + "listLangMovieImdb"));
|
||||
commands.put("listLangMovie", new ListLangMovieTitle(PREFIX + "listLangMovie"));
|
||||
commands.put("listLangMovie", new ListLangMovie(PREFIX + "listLangMovie"));
|
||||
commands.put("download", new Download(PREFIX + "download"));
|
||||
}
|
||||
|
||||
|
@ -54,4 +54,11 @@ public class Movie {
|
||||
public String getPoster() {
|
||||
return poster;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder movie = new StringBuilder(title);
|
||||
if (year != null) movie.append(" (").append(year).append(")");
|
||||
return movie.toString();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user