diff --git a/src/main/java/com/botdarr/api/AddStrategy.java b/src/main/java/com/botdarr/api/AddStrategy.java index 12e9d7c..25214dd 100644 --- a/src/main/java/com/botdarr/api/AddStrategy.java +++ b/src/main/java/com/botdarr/api/AddStrategy.java @@ -49,6 +49,29 @@ public CommandResponse addWithSearchId(String searchText, String id) { } } + public CommandResponse addWithId(String id) { + try { + List items = lookupItemById(id); + + if (items.isEmpty()) { + LOGGER.warn("Search id " + id + "yielded no " + this.contentDisplayName + "s, stopping"); + return new ErrorResponse("No " + this.contentDisplayName + "s found"); + } + for (T item : items) { + if (getItemId(item).equalsIgnoreCase(id)) { + if (doesItemExist(item)) { + return new ErrorResponse(this.contentDisplayName + " already exists"); + } + return addContent(item); + } + } + return new ErrorResponse("Could not find " + contentDisplayName + " with id=" + id); + } catch (Throwable e) { + LOGGER.error("Error trying to add " + contentDisplayName, e); + return new ErrorResponse("Error adding content, e=" + e.getMessage()); + } + } + public List addWithSearchTitle(String searchText) { try { List items = lookupContent(searchText); diff --git a/src/main/java/com/botdarr/api/sonarr/SonarrApi.java b/src/main/java/com/botdarr/api/sonarr/SonarrApi.java index 9949ae4..268f454 100644 --- a/src/main/java/com/botdarr/api/sonarr/SonarrApi.java +++ b/src/main/java/com/botdarr/api/sonarr/SonarrApi.java @@ -25,8 +25,8 @@ public List downloads() { return getDownloadsStrategy().downloads(); } - public CommandResponse addWithId(String searchText, String id) { - return getAddStrategy().addWithSearchId(searchText, id); + public CommandResponse addWithId(String id) { + return getAddStrategy().addWithId(id); } public List addWithTitle(String searchText) { @@ -137,9 +137,8 @@ public List lookupContent(String search) throws Exception { } @Override - public List lookupItemById(String id) { - //TODO: if sonarr has a lookup by id, implement - return Collections.emptyList(); + public List lookupItemById(String id) throws Exception { + return lookupShows("tvdb:" + id); } @Override diff --git a/src/main/java/com/botdarr/api/sonarr/SonarrCommands.java b/src/main/java/com/botdarr/api/sonarr/SonarrCommands.java index 9701aac..9dffec6 100644 --- a/src/main/java/com/botdarr/api/sonarr/SonarrCommands.java +++ b/src/main/java/com/botdarr/api/sonarr/SonarrCommands.java @@ -15,19 +15,17 @@ public static List getCommands(SonarrApi sonarrApi) { return new ArrayList() {{ add(new BaseCommand( "show id add", - "Adds a show using search text and tmdb id (i.e., show id add 30 clock 484767)", - Arrays.asList("show-title", "show-tvdbid")) { + "Adds a show using tvdb id (i.e., show id add 484767)", + Collections.singletonList("show-tvdbid")) { @Override public List execute(String command) { - int lastSpace = command.lastIndexOf(" "); - if (lastSpace == -1) { - throw new RuntimeException("Missing expected arguments - usage: show id add SHOW_TITLE_HERE SHOW_ID_HERE"); + if (command.isEmpty()) { + throw new RuntimeException("Missing expected arguments - usage: show id add SHOW_ID_HERE"); } - String searchText = command.substring(0, lastSpace); - String id = command.substring(lastSpace + 1); - validateShowTitle(searchText); - validateShowId(id); - return Collections.singletonList(sonarrApi.addWithId(searchText, id)); + + validateShowId(command); + + return Collections.singletonList(sonarrApi.addWithId(command)); } }); add(new BaseCommand( @@ -85,8 +83,8 @@ public List execute(String command) { }}; } - public static String getAddShowCommandStr(String title, long tvdbId) { - return CommandContext.getConfig().getPrefix() + "show id add " + title + " " + tvdbId; + public static String getAddShowCommandStr(long tvdbId) { + return CommandContext.getConfig().getPrefix() + "show id add " + tvdbId; } public static String getHelpShowCommandStr() { diff --git a/src/main/java/com/botdarr/clients/discord/DiscordBootstrap.java b/src/main/java/com/botdarr/clients/discord/DiscordBootstrap.java index d7ac416..c0964a6 100644 --- a/src/main/java/com/botdarr/clients/discord/DiscordBootstrap.java +++ b/src/main/java/com/botdarr/clients/discord/DiscordBootstrap.java @@ -198,7 +198,7 @@ public String getCommandFromEmbed(MessageEmbed embed) { return RadarrCommands.getAddMovieCommandStr(embed.getTitle(), Long.parseLong(field.getValue())); } if (field.getName().equals(SHOW_LOOKUP_FIELD)) { - return SonarrCommands.getAddShowCommandStr(embed.getTitle(), Long.parseLong(field.getValue())); + return SonarrCommands.getAddShowCommandStr(Long.parseLong(field.getValue())); } if (field.getName().equals(ARTIST_LOOKUP_KEY_FIELD)) { return LidarrCommands.getAddArtistCommandStr(embed.getTitle(), field.getValue()); diff --git a/src/main/java/com/botdarr/clients/discord/DiscordResponseBuilder.java b/src/main/java/com/botdarr/clients/discord/DiscordResponseBuilder.java index 9f990ee..b1b4c99 100644 --- a/src/main/java/com/botdarr/clients/discord/DiscordResponseBuilder.java +++ b/src/main/java/com/botdarr/clients/discord/DiscordResponseBuilder.java @@ -87,7 +87,7 @@ public DiscordResponse build(ShowResponse showResponse) { SonarrShow show = showResponse.getShow(); embedBuilder.setTitle(show.getTitle()); embedBuilder.addField(SHOW_LOOKUP_FIELD, String.valueOf(show.getTvdbId()), false); - embedBuilder.addField(ADD_SHOW_COMMAND_FIELD_PREFIX, SonarrCommands.getAddShowCommandStr(show.getTitle(), show.getTvdbId()), false); + embedBuilder.addField(ADD_SHOW_COMMAND_FIELD_PREFIX, SonarrCommands.getAddShowCommandStr(show.getTvdbId()), false); embedBuilder.setImage(show.getRemoteImage()); return new DiscordResponse(embedBuilder.build()); } @@ -213,7 +213,7 @@ public DiscordResponse build(NewShowResponse newShowResponse) { embedBuilder.addField("TvdbId", "" + sonarrShow.getTvdbId(), true); embedBuilder.setImage(sonarrShow.getRemoteImage()); if (!usingSlashCommand) { - embedBuilder.addField(ADD_SHOW_COMMAND_FIELD_PREFIX, SonarrCommands.getAddShowCommandStr(sonarrShow.getTitle(), sonarrShow.getTvdbId()), false); + embedBuilder.addField(ADD_SHOW_COMMAND_FIELD_PREFIX, SonarrCommands.getAddShowCommandStr(sonarrShow.getTvdbId()), false); return new DiscordResponse(embedBuilder.build()); } List actionComponents = new ArrayList<>(); diff --git a/src/main/java/com/botdarr/clients/matrix/MatrixResponseBuilder.java b/src/main/java/com/botdarr/clients/matrix/MatrixResponseBuilder.java index fb13956..b3a0853 100644 --- a/src/main/java/com/botdarr/clients/matrix/MatrixResponseBuilder.java +++ b/src/main/java/com/botdarr/clients/matrix/MatrixResponseBuilder.java @@ -77,7 +77,7 @@ public MatrixResponse build(ShowResponse showResponse) { MatrixResponse matrixResponse = new MatrixResponse(); matrixResponse.addContent("Title - " + show.getTitle()); matrixResponse.addContent("TvdbId - " + show.getTvdbId()); - matrixResponse.addContent("" + ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(show.getTitle(), show.getTvdbId())); + matrixResponse.addContent("" + ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(show.getTvdbId())); matrixResponse.addImage(show.getRemoteImage()); return matrixResponse; } @@ -223,7 +223,7 @@ public MatrixResponse build(NewShowResponse newShowResponse) { MatrixResponse matrixResponse = new MatrixResponse(); matrixResponse.addContent("Title - " + sonarrShow.getTitle()); matrixResponse.addContent("TvdbId - " + sonarrShow.getTvdbId()); - matrixResponse.addContent("" + ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(sonarrShow.getTitle(), sonarrShow.getTvdbId())); + matrixResponse.addContent("" + ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(sonarrShow.getTvdbId())); matrixResponse.addImage(sonarrShow.getRemoteImage()); return matrixResponse; } diff --git a/src/main/java/com/botdarr/clients/slack/SlackResponseBuilder.java b/src/main/java/com/botdarr/clients/slack/SlackResponseBuilder.java index 32ec120..438ad3d 100644 --- a/src/main/java/com/botdarr/clients/slack/SlackResponseBuilder.java +++ b/src/main/java/com/botdarr/clients/slack/SlackResponseBuilder.java @@ -106,7 +106,7 @@ public SlackResponse build(ShowResponse showResponse) { .text(PlainTextObject.builder().text("TvdbId - " + show.getTvdbId()).build()) .build()); slackResponse.addBlock(SectionBlock.builder() - .text(PlainTextObject.builder().text(ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(show.getTitle(), show.getTvdbId())).build()) + .text(PlainTextObject.builder().text(ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(show.getTvdbId())).build()) .build()); if (!Strings.isBlank(show.getRemoteImage())) { //if there is no poster to display, slack will fail to render all the blocks @@ -348,7 +348,7 @@ public SlackResponse build(NewShowResponse newShowResponse) { .text(MarkdownTextObject.builder().text("TvdbId - " + sonarrShow.getTvdbId()).build()) .build()); slackResponse.addBlock(SectionBlock.builder() - .text(MarkdownTextObject.builder().text(ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(sonarrShow.getTitle(), sonarrShow.getTvdbId())).build()) + .text(MarkdownTextObject.builder().text(ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(sonarrShow.getTvdbId())).build()) .build()); slackResponse.addBlock(ImageBlock.builder() .imageUrl(sonarrShow.getRemoteImage()) diff --git a/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java b/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java index 4a032ac..40501cc 100644 --- a/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java +++ b/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java @@ -82,7 +82,7 @@ public TelegramResponse build(ShowResponse showResponse) { List domContents = new ArrayList<>(); domContents.add(b("*Title* - " + show.getTitle())); domContents.add(code("TvdbId - " + show.getTvdbId())); - domContents.add(u(ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(show.getTitle(), show.getTvdbId()))); + domContents.add(u(ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(show.getTvdbId()))); domContents.add(a(show.getRemoteImage())); return new TelegramResponse(domContents); } @@ -246,7 +246,7 @@ public TelegramResponse build(NewShowResponse newShowResponse) { domContents.add(b("*Title* - " + sonarrShow.getTitle())); domContents.add(code("TvdbId - " + sonarrShow.getTvdbId())); domContents.add(a(sonarrShow.getRemoteImage())); - return getAddResponse(domContents, SonarrCommands.getAddShowCommandStr(sonarrShow.getTitle(), sonarrShow.getTvdbId())); + return getAddResponse(domContents, SonarrCommands.getAddShowCommandStr(sonarrShow.getTvdbId())); } @Override diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt index 3b338e2..db4dd40 100644 --- a/src/main/resources/version.txt +++ b/src/main/resources/version.txt @@ -1 +1 @@ -5.6.13 \ No newline at end of file +5.6.14 \ No newline at end of file diff --git a/src/test/java/com/botdarr/clients/discord/DiscordBootstrapTests.java b/src/test/java/com/botdarr/clients/discord/DiscordBootstrapTests.java index ae2ccf9..2900ab8 100644 --- a/src/test/java/com/botdarr/clients/discord/DiscordBootstrapTests.java +++ b/src/test/java/com/botdarr/clients/discord/DiscordBootstrapTests.java @@ -101,10 +101,9 @@ public void getCommandFromEmbed_returnsShowCommand() { mockedEmbed.getFields(); result = new ArrayList(){{ add(new MessageEmbed.Field("TvdbId", "43234", false)); }}; - mockedEmbed.getTitle(); result = "ShowTitle1"; }}; String command = new DiscordBootstrap().getCommandFromEmbed(mockedEmbed); - Assert.assertEquals("!show id add ShowTitle1 43234", command); + Assert.assertEquals("!show id add 43234", command); } @Test diff --git a/src/test/java/com/botdarr/commands/CommandProcessorTests.java b/src/test/java/com/botdarr/commands/CommandProcessorTests.java index 8d2bc50..d5a3d75 100644 --- a/src/test/java/com/botdarr/commands/CommandProcessorTests.java +++ b/src/test/java/com/botdarr/commands/CommandProcessorTests.java @@ -219,39 +219,25 @@ public void processMessage_validMovieTitleWithSpacesForFindExistingMovieCommand( } @Test - public void processMessage_missingShowTitleAndIdForAddCommand() { + public void processMessage_missingShowIdForAddCommand() { validateInvalidCommand("!show id add", "Error trying to parse command !show id add, " + - "error=Missing expected arguments - usage: show id add SHOW_TITLE_HERE SHOW_ID_HERE"); - } - - @Test - public void processMessage_missingShowTitleForAddCommand() { - validateInvalidCommand("!show id add 541515", - "Error trying to parse command !show id add 541515, " + - "error=Missing expected arguments - usage: show id add SHOW_TITLE_HERE SHOW_ID_HERE"); - } - - @Test - public void processMessage_missingShowIdForAddCommand() { - validateInvalidCommand("!show id add Princess5", - "Error trying to parse command !show id add Princess5, " + - "error=Missing expected arguments - usage: show id add SHOW_TITLE_HERE SHOW_ID_HERE"); + "error=Missing expected arguments - usage: show id add SHOW_ID_HERE"); } @Test public void processMessage_invalidShowIdForAddCommand() { - validateInvalidCommand("!show id add Princess5 4647x5", - "Error trying to parse command !show id add Princess5 4647x5, " + + validateInvalidCommand("!show id add 4647x5", + "Error trying to parse command !show id add 4647x5, " + "error=Show id is not a number"); } @Test - public void processMessage_validShowTitleAndIdForAddCommand() { + public void processMessage_validShowIdForAddCommand() { new Expectations() {{ - sonarrApi.addWithId("princess5", "46475"); times = 1; result = new TestCommandResponse(); + sonarrApi.addWithId("46475"); times = 1; result = new TestCommandResponse(); }}; - validateValidCommand("!show id add Princess5 46475"); + validateValidCommand("!show id add 46475"); } @Test