Skip to content

Commit

Permalink
Merge pull request #24 from shayaantx/feature/radarr-v3
Browse files Browse the repository at this point in the history
Add radarr v3 support deprecate radarr v2
  • Loading branch information
shayaantx authored Jan 25, 2021
2 parents 2d1f07a + b3df3c5 commit 5b8e541
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 101 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Made this simple slack/discord/telegram/matrix bot so I could access radarr, son

## Currently Supported API's

- [x] Radarr (v2)
- [ ] Radarr (v3)
- [ ] Sonarr (v2)
- [x] Radarr (v3)
- [x] Sonarr (v3)
- [x] Lidarr (v1)
- [x] ~~Radarr (v2) - no longer supported~~
- [ ] ~~Sonarr (v2) - no plans to support~~

## Currently Supported Chat Client's

Expand All @@ -33,10 +33,10 @@ Made this simple slack/discord/telegram/matrix bot so I could access radarr, son
- [x] Configurable value for max number of movies, shows, and artists per user
- [x] Configurable command prefix (i.e., /help, $help, !help)
- [x] Configurable value for url base for radarr, sonarr, and lidarr
- [x] Lookup torrents for movies and force download
- [x] (discord/slack only) Thumbs up reaction will add search results
- [x] User requests audited to local database\
- [x] Blacklist content by paths from showing up in searches
- [ ] Lookup torrents for movies and force download
- [ ] Cancel/blacklist existing downloads
- [ ] Episode/season search
- [ ] Album/song search
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/botdarr/api/DownloadsStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Collections;
import java.util.List;

public abstract class DownloadsStrategy {
public abstract class DownloadsStrategy<T> {
public DownloadsStrategy(Api api,
String url,
ChatClientResponseBuilder<? extends ChatClientResponse> chatClientResponseBuilder,
Expand Down Expand Up @@ -57,7 +57,7 @@ public List<ChatClientResponse> parseContent(String response) {
}
chatClientResponses.add(chatClientResponse);
}
if (tooManyDownloads) {
if (tooManyDownloads && !chatClientResponses.isEmpty()) {
chatClientResponses = ListUtils.subList(chatClientResponses, MAX_DOWNLOADS_TO_SHOW);
chatClientResponses.add(0, chatClientResponseBuilder.createInfoMessage("Too many downloads, limiting results to " + MAX_DOWNLOADS_TO_SHOW));
}
Expand Down
53 changes: 44 additions & 9 deletions src/main/java/com/botdarr/api/radarr/RadarrApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ public ChatClientResponse getNewOrExistingItem(RadarrMovie lookupItem, RadarrMov

@Override
public boolean isPathBlacklisted(RadarrMovie item) {
for (String path : Config.getExistingItemBlacklistPaths()) {
if (item.getPath() != null && item.getPath().startsWith(path)) {
return true;
}
}
return false;
return RadarrApi.this.isPathBlacklisted(item);
}
}.lookup(search, findNew);
}
Expand Down Expand Up @@ -154,7 +149,7 @@ public String getApiToken() {
}

public List<ChatClientResponse> discover() {
return ConnectionHelper.makeGetRequest(this, RadarrUrls.DISCOVER_MOVIES, new ConnectionHelper.SimpleEntityResponseHandler<List<ChatClientResponse>>() {
return ConnectionHelper.makeGetRequest(this, RadarrUrls.DISCOVER_MOVIES, "&includeRecommendations=true", new ConnectionHelper.SimpleEntityResponseHandler<List<ChatClientResponse>>() {
@Override
public List<ChatClientResponse> onSuccess(String response) throws Exception {
List<ChatClientResponse> recommendedMovies = new ArrayList<>();
Expand All @@ -178,13 +173,44 @@ public List<ChatClientResponse> onSuccess(String response) throws Exception {
});
}

private DownloadsStrategy getDownloadsStrategy() {
return new DownloadsStrategy(this, RadarrUrls.DOWNLOAD_BASE, chatClientResponseBuilder, ContentType.MOVIE) {
private DownloadsStrategy<RadarrMovie> getDownloadsStrategy() {
return new DownloadsStrategy<RadarrMovie>(this, RadarrUrls.DOWNLOAD_BASE, chatClientResponseBuilder, ContentType.MOVIE) {
@Override
public ChatClientResponse getResponse(JsonElement rawElement) {
RadarrQueue radarrQueue = new Gson().fromJson(rawElement, RadarrQueue.class);
RadarrMovie radarrMovie = RADARR_CACHE.getExistingMovieWithRadarrId(radarrQueue.getMovieId());
if (radarrMovie == null) {
LOGGER.warn("Could not load radarr movie from cache for id " + radarrQueue.getMovieId() + " title=" + radarrQueue.getTitle());
return null;
}
//the radarr queue title is the title of the actual download, instead of movie title
//so we get the real value here
radarrQueue.setTitle(radarrMovie.getTitle());
if (isPathBlacklisted(radarrMovie)) {
//skip any radarr queue items tied to blacklisted content
return null;
}

return chatClientResponseBuilder.getMovieDownloadResponses(radarrQueue);
}

@Override
public List<ChatClientResponse> getContentDownloads() {
return ConnectionHelper.makeGetRequest(
RadarrApi.this,
RadarrUrls.DOWNLOAD_BASE,
new ConnectionHelper.SimpleMessageEmbedResponseHandler(chatClientResponseBuilder) {
@Override
public List<ChatClientResponse> onSuccess(String response) {
if (response == null || response.isEmpty() || response.equals("{}")) {
return new ArrayList<>();
}
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(response).getAsJsonObject();
return parseContent(json.get("records").toString());
}
});
}
};
}

Expand Down Expand Up @@ -306,6 +332,15 @@ public List<RadarrMovie> onSuccess(String response) {
});
}

private boolean isPathBlacklisted(RadarrMovie item) {
for (String path : Config.getExistingItemBlacklistPaths()) {
if (item.getPath() != null && item.getPath().startsWith(path)) {
return true;
}
}
return false;
}

private final ChatClientResponseBuilder<? extends ChatClientResponse> chatClientResponseBuilder;
private static final RadarrCache RADARR_CACHE = new RadarrCache();
public static final String ADD_MOVIE_COMMAND_FIELD_PREFIX = "Add movie command";
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/botdarr/api/radarr/RadarrCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public RadarrMovie getExistingMovie(long tmdbid) {
return existingTmdbIdsToMovies.get(tmdbid);
}

public boolean doesMovieExist(String title) {
return existingMovieTitlesToIds.containsKey(title.toLowerCase());
public RadarrMovie getExistingMovieWithRadarrId(long radarrId) {
return existingRadarrMovieIdsToMovies.get(radarrId);
}

public Long getMovieSonarrId(String title) {
return existingMovieTitlesToIds.get(title.toLowerCase());
public boolean doesMovieExist(String title) {
return existingMovieTitlesToIds.containsKey(title.toLowerCase());
}

public Collection<RadarrProfile> getQualityProfiles() {
Expand All @@ -23,6 +23,7 @@ public Collection<RadarrProfile> getQualityProfiles() {
public void add(RadarrMovie movie) {
existingTmdbIdsToMovies.put(movie.getKey(), movie);
existingMovieTitlesToIds.put(movie.getTitle().toLowerCase(), movie.getId());
existingRadarrMovieIdsToMovies.put(movie.getId(), movie);
}

public void addProfile(RadarrProfile qualityProfile) {
Expand All @@ -39,17 +40,21 @@ public void removeDeletedProfiles(List<String> addUpdatedProfiles) {

public void removeDeletedMovies(List<Long> addUpdatedMovies) {
List<String> existingMovieTitles = new ArrayList<>();
List<Long> existingMovieIds = new ArrayList<>();
for (Long tmdbId : addUpdatedMovies) {
RadarrMovie radarrMovie = existingTmdbIdsToMovies.get(tmdbId);
if (radarrMovie != null) {
existingMovieTitles.add(radarrMovie.getTitle().toLowerCase());
existingMovieIds.add(radarrMovie.getId());
}
}
existingTmdbIdsToMovies.keySet().retainAll(addUpdatedMovies);
existingMovieTitlesToIds.keySet().retainAll(existingMovieTitles);
existingRadarrMovieIdsToMovies.keySet().retainAll(existingMovieIds);
}

private Map<String, RadarrProfile> existingProfiles = new ConcurrentHashMap<>();
private Map<String, Long> existingMovieTitlesToIds = new ConcurrentHashMap<>();
private Map<Long, RadarrMovie> existingRadarrMovieIdsToMovies = new ConcurrentHashMap<>();
private Map<Long, RadarrMovie> existingTmdbIdsToMovies = new ConcurrentHashMap<>();
}
28 changes: 10 additions & 18 deletions src/main/java/com/botdarr/api/radarr/RadarrQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,10 @@ public void setTimeleft(String timeleft) {
this.timeleft = timeleft;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public RadarrQueueStatusMessages[] getStatusMessages() {
return statusMessages;
}

public void setStatusMessages(RadarrQueueStatusMessages[] statusMessages) {
this.statusMessages = statusMessages;
}

public RadarrProfileQualityItem getQuality() {
return quality;
}
Expand All @@ -41,18 +29,22 @@ public void setQuality(RadarrProfileQualityItem quality) {
this.quality = quality;
}

public RadarrQueueMovie getRadarrQueueMovie() {
return movie;
public long getMovieId() {
return movieId;
}

public String getTitle() {
return title;
}

public void setRadarrQueueMovie(RadarrQueueMovie radarrQueueMovie) {
this.movie = radarrQueueMovie;
public void setTitle(String title) {
this.title = title;
}

private String status;
private String timeleft;
private RadarrProfileQualityItem quality;
private long id;
private RadarrQueueStatusMessages[] statusMessages;
private RadarrQueueMovie movie;
private long movieId;
private String title;
}
13 changes: 0 additions & 13 deletions src/main/java/com/botdarr/api/radarr/RadarrQueueMovie.java

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/java/com/botdarr/api/radarr/RadarrQueuePage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.botdarr.api.radarr;

import java.util.List;

public class RadarrQueuePage {
public List<RadarrQueue> getRecords() {
return records;
}

public void setRecords(List<RadarrQueue> records) {
this.records = records;
}

private Integer page;
private Integer pageSize;
private String sortKey;
private String sortDirection;
private Integer totalRecords;
private List<RadarrQueue> records = null;
}
11 changes: 3 additions & 8 deletions src/main/java/com/botdarr/api/radarr/RadarrUrls.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package com.botdarr.api.radarr;

public class RadarrUrls {
/**
* Specifically used as a post request for forcing a download for a specific release or getting existing available
* downloads for a release
*/
public static final String RELEASE_BASE = "release";

private static final String V3_BASE = "v3/";
/**
* The base download(s) url for get, put, delete requests (which each do different things in radarr)
* See https://github.com/Radarr/Radarr/wiki/API:Queue
*/
public static final String DOWNLOAD_BASE = "queue";
public static final String DOWNLOAD_BASE = V3_BASE + "queue";

/**
* The base movie url for getting and adding movies (get, post requests)
Expand All @@ -32,7 +27,7 @@ public class RadarrUrls {
/**
* The url for triggering gets requests in radarr to discover new movies
*/
public static final String DISCOVER_MOVIES = "movies/discover/recommendations";
public static final String DISCOVER_MOVIES = V3_BASE + "importlist/movie";

/**
* The url base for adding, getting, deleting movie profiles
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/botdarr/api/sonarr/SonarQueueEpisode.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ public void setOverview(String overview) {
this.overview = overview;
}

public long getSeriesId() {
return seriesId;
}

private int seasonNumber;
private int episodeNumber;
private String title;
private String overview;
private long seriesId;
}
25 changes: 19 additions & 6 deletions src/main/java/com/botdarr/api/sonarr/SonarrApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ public ChatClientResponse getNewOrExistingItem(SonarrShow lookupItem, SonarrShow

@Override
public boolean isPathBlacklisted(SonarrShow item) {
for (String path : Config.getExistingItemBlacklistPaths()) {
if (item.getPath() != null && item.getPath().startsWith(path)) {
return true;
}
}
return false;
return SonarrApi.this.isPathBlacklisted(item);
}
}.lookup(search, findNew);
}
Expand Down Expand Up @@ -198,6 +193,15 @@ public ChatClientResponse getResponse(JsonElement rawElement) {
LOGGER.error("Series " + showQueue.getSonarrQueueShow().getTitle() + " missing episode info for id " + showQueue.getId());
return null;
}
SonarrShow sonarrShow = SONARR_CACHE.getExistingShowFromSonarrId(showQueue.getEpisode().getSeriesId());
if (sonarrShow == null) {
LOGGER.warn("Could not load sonarr show from cache for id " + showQueue.getEpisode().getSeriesId() + " title=" + showQueue.getSonarrQueueShow().getTitle());
return null;
}
if (isPathBlacklisted(sonarrShow)) {
//TODO: log
return null;
}
return chatClientResponseBuilder.getShowDownloadResponses(showQueue);
}
};
Expand Down Expand Up @@ -276,6 +280,15 @@ public boolean shouldSkipClass(Class<?> aClass) {
}
};

private boolean isPathBlacklisted(SonarrShow item) {
for (String path : Config.getExistingItemBlacklistPaths()) {
if (item.getPath() != null && item.getPath().startsWith(path)) {
return true;
}
}
return false;
}

private final ChatClientResponseBuilder<? extends ChatClientResponse> chatClientResponseBuilder;
private static final SonarrCache SONARR_CACHE = new SonarrCache();
public static final String ADD_SHOW_COMMAND_FIELD_PREFIX = "Add show command";
Expand Down
Loading

0 comments on commit 5b8e541

Please sign in to comment.