Skip to content

Commit

Permalink
Add lookup by TVDB Id on Sonarr (#120)
Browse files Browse the repository at this point in the history
* feat(Sonarr): add new method addWithId to AddStrategy.java

* feat(Sonarr): change command to use new addWithId method

* test: refactor CommandProcessorTests to use only show id for add by id command

* fix(addShowCommand): remove remaining references of show title on ResponseBuilders

* test: remove showTitle on DiscordBootstrapTests for 'add by id'  command

* fix: bumb version on version.txt to 5.6.14
  • Loading branch information
g-nogueira committed Apr 23, 2024
1 parent 0ea239a commit b10df70
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 50 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/botdarr/api/AddStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ public CommandResponse addWithSearchId(String searchText, String id) {
}
}

public CommandResponse addWithId(String id) {
try {
List<T> 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<CommandResponse> addWithSearchTitle(String searchText) {
try {
List<T> items = lookupContent(searchText);
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/botdarr/api/sonarr/SonarrApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public List<CommandResponse> 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<CommandResponse> addWithTitle(String searchText) {
Expand Down Expand Up @@ -137,9 +137,8 @@ public List<SonarrShow> lookupContent(String search) throws Exception {
}

@Override
public List<SonarrShow> lookupItemById(String id) {
//TODO: if sonarr has a lookup by id, implement
return Collections.emptyList();
public List<SonarrShow> lookupItemById(String id) throws Exception {
return lookupShows("tvdb:" + id);
}

@Override
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/com/botdarr/api/sonarr/SonarrCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ public static List<Command> getCommands(SonarrApi sonarrApi) {
return new ArrayList<Command>() {{
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<CommandResponse> 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(
Expand Down Expand Up @@ -85,8 +83,8 @@ public List<CommandResponse> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<Component> actionComponents = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public MatrixResponse build(ShowResponse showResponse) {
MatrixResponse matrixResponse = new MatrixResponse();
matrixResponse.addContent("<b>Title</b> - " + show.getTitle());
matrixResponse.addContent("<b>TvdbId</b> - " + show.getTvdbId());
matrixResponse.addContent("<b>" + ADD_SHOW_COMMAND_FIELD_PREFIX + "</b> - " + SonarrCommands.getAddShowCommandStr(show.getTitle(), show.getTvdbId()));
matrixResponse.addContent("<b>" + ADD_SHOW_COMMAND_FIELD_PREFIX + "</b> - " + SonarrCommands.getAddShowCommandStr(show.getTvdbId()));
matrixResponse.addImage(show.getRemoteImage());
return matrixResponse;
}
Expand Down Expand Up @@ -223,7 +223,7 @@ public MatrixResponse build(NewShowResponse newShowResponse) {
MatrixResponse matrixResponse = new MatrixResponse();
matrixResponse.addContent("<b>Title</b> - " + sonarrShow.getTitle());
matrixResponse.addContent("<b>TvdbId</b> - " + sonarrShow.getTvdbId());
matrixResponse.addContent("<b>" + ADD_SHOW_COMMAND_FIELD_PREFIX + "</b> - " + SonarrCommands.getAddShowCommandStr(sonarrShow.getTitle(), sonarrShow.getTvdbId()));
matrixResponse.addContent("<b>" + ADD_SHOW_COMMAND_FIELD_PREFIX + "</b> - " + SonarrCommands.getAddShowCommandStr(sonarrShow.getTvdbId()));
matrixResponse.addImage(sonarrShow.getRemoteImage());
return matrixResponse;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public TelegramResponse build(ShowResponse showResponse) {
List<DomContent> 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);
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.6.13
5.6.14
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ public void getCommandFromEmbed_returnsShowCommand() {
mockedEmbed.getFields(); result = new ArrayList<MessageEmbed.Field>(){{
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
Expand Down
28 changes: 7 additions & 21 deletions src/test/java/com/botdarr/commands/CommandProcessorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b10df70

Please sign in to comment.