Added the number of lines the importer just added to our database

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-29 11:57:35 +01:00
parent 2f5195ad0a
commit af35537353

View File

@ -5,6 +5,7 @@ import com.github.wtekiela.opensub4j.response.ResponseStatus;
import com.github.wtekiela.opensub4j.response.SubtitleFile; import com.github.wtekiela.opensub4j.response.SubtitleFile;
import com.github.wtekiela.opensub4j.response.SubtitleInfo; import com.github.wtekiela.opensub4j.response.SubtitleInfo;
import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.spec.EmbedCreateSpec;
import discord4j.rest.util.Color; import discord4j.rest.util.Color;
import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.XmlRpcException;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -52,31 +53,8 @@ public class Download extends Command {
else if (SubtitleManager.getSubtitlesId(movie, languageId) > 0) else if (SubtitleManager.getSubtitlesId(movie, languageId) > 0)
embed.setDescription("This movie already has already this language imported.") embed.setDescription("This movie already has already this language imported.")
.setColor(Color.ORANGE); .setColor(Color.ORANGE);
else { else
Stream<SubtitleInfo> subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie); computeImportation(event, args, languageId, embed, movie);
Optional<SubtitleInfo> subtitleInfo = subtitleInfoStream == null ? Optional.empty() :
subtitleInfoStream.filter(info ->
LanguageManager.getIdLanguage(info.getLanguage()) == languageId)
.findFirst();
if (subtitleInfo.isEmpty())
embed.setDescription("Nothing was found with these parameters.");
else {
ListResponse<SubtitleFile> subs = OpenSubtitles.downloadSubtitle(
subtitleInfo.get().getSubtitleFileId());
if (subs.getStatus().equals(ResponseStatus.OK)) {
List<SubtitleBlock> blocks = new SubtitleParser().parseSRT(
subs.getData().get(0).getContentAsString("cp1252"));
SubtitleLineManager.importSubtitleLines(blocks, languageId, movie,
event.getGuildId().isPresent() ? event.getGuildId().get() : null,
event.getMember().isPresent() ? event.getMessage().getId() : null);
embed.setColor(Color.MEDIUM_SEA_GREEN).setDescription("Everything went well. " +
"Congratulations and thank you for your contribution!");
} else {
logger.error("Could not download subtitle '{}': {}", args.get(0), subs.getStatus());
embed.setDescription("An error occurred. Please contact my administrator.");
}
}
}
} catch (XmlRpcException | IOException e) { } catch (XmlRpcException | IOException e) {
logger.error("An error occurred while fetching the data from opensubtitles: {}", e.getMessage()); logger.error("An error occurred while fetching the data from opensubtitles: {}", e.getMessage());
embed.setDescription("An error occurred, please contact my administrator."); embed.setDescription("An error occurred, please contact my administrator.");
@ -85,4 +63,32 @@ public class Download extends Command {
} }
))).then(event.getMessage().removeSelfReaction(WAITING)).then(); ))).then(event.getMessage().removeSelfReaction(WAITING)).then();
} }
private void computeImportation(MessageCreateEvent event, List<String> args,
int languageId, EmbedCreateSpec embed, Movie movie) throws XmlRpcException {
Stream<SubtitleInfo> subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie);
Optional<SubtitleInfo> subtitleInfo = subtitleInfoStream == null ? Optional.empty() :
subtitleInfoStream.filter(info ->
LanguageManager.getIdLanguage(info.getLanguage()) == languageId)
.findFirst();
if (subtitleInfo.isEmpty())
embed.setDescription("Nothing was found with these parameters.");
else {
ListResponse<SubtitleFile> subs = OpenSubtitles.downloadSubtitle(
subtitleInfo.get().getSubtitleFileId());
if (subs.getStatus().equals(ResponseStatus.OK)) {
List<SubtitleBlock> blocks = new SubtitleParser().parseSRT(
subs.getData().get(0).getContentAsString("cp1252"));
SubtitleLineManager.importSubtitleLines(blocks, languageId, movie,
event.getGuildId().isPresent() ? event.getGuildId().get() : null,
event.getMember().isPresent() ? event.getMessage().getId() : null);
embed.setColor(Color.MEDIUM_SEA_GREEN).setDescription("Everything went well. " +
"Congratulations and thank you for your contribution!");
embed.addField("You imported", blocks.size() + " lines into my database", false);
} else {
logger.error("Could not download subtitle '{}': {}", args.get(0), subs.getStatus());
embed.setDescription("An error occurred. Please contact my administrator.");
}
}
}
} }