Skip to content

Commit

Permalink
Add sonarr v4 support + update dependencies + refactor api urls across (
Browse files Browse the repository at this point in the history
#86)

Arr apis
  • Loading branch information
shayaantx authored Dec 23, 2022
1 parent 903e803 commit c10b8d8
Show file tree
Hide file tree
Showing 32 changed files with 672 additions and 315 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Made this simple multi chat-client bot to access radarr, sonarr, and lidarr with
## Currently, Supported API's

- [x] Radarr (v4)
- [x] Sonarr (v4)
- [x] Sonarr (v3)
- [x] Lidarr (v1)
- [x] Lidarr (v0)
Expand Down Expand Up @@ -166,6 +167,7 @@ botdarr:
| SONARR_DEFAULT_PROFILE | The sonarr quality profile (should be already configured in sonarr) | yes - if you use sonarr |
| SONARR_PATH | Where your sonarr shows should go (if you add/update them) | yes - if you use sonarr |
| SONARR_URL_BASE | Only populate this if you use a custom sonarr url base (which is configurable in Sonarr->Settings->General->URL Base) don't include prefix/suffix slashes | no |
| SONARR_V4 | Whether you are using sonarr v4 or not | no | no |

#### Lidarr
| Environment Variable | Description | Required | Default |
Expand Down
1 change: 1 addition & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ if [ ! -e "$propertiesFile" ]; then
[[ ! -z "${SONARR_DEFAULT_PROFILE}" ]] && addConfiguration "sonarr-default-profile" "${SONARR_DEFAULT_PROFILE}" "${propertiesFile}"
[[ ! -z "${SONARR_PATH}" ]] && addConfiguration "sonarr-path" "${SONARR_PATH}" "${propertiesFile}"
[[ ! -z "${SONARR_URL_BASE}" ]] && addConfiguration "sonarr-url-base" "${SONARR_URL_BASE}" "${propertiesFile}"
[[ ! -z "${SONARR_V4}" ]] && addConfiguration "sonarr-v4" "${SONARR_V4}" "${propertiesFile}"

[[ ! -z "${LIDARR_URL}" ]] && addConfiguration "lidarr-url" "${LIDARR_URL}" "${propertiesFile}"
[[ ! -z "${LIDARR_TOKEN}" ]] && addConfiguration "lidarr-token" "${LIDARR_TOKEN}" "${propertiesFile}"
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand All @@ -66,7 +66,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
<version>2.13.4.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
2 changes: 2 additions & 0 deletions sample.properties
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ sonarr-path=
# Only populate this if you use a custom sonarr url base (which is configurable in Sonarr->Settings->General->URL Base)
# don't include prefix/suffix slashes
sonarr-url-base=
# Whether your sonarr instance is v4 or not
sonarr-v4

lidarr-url=
lidarr-token=
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/botdarr/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ public static final class Constants {
*/
public static final String SONARR_URL_BASE = "sonarr-url-base";

/**
* Whether to turn on sonarr v4 support or not
*/
public static final String SONARR_V4 = "sonarr-v4";

/**
* The url to your lidarr instance
*/
Expand Down Expand Up @@ -416,6 +421,7 @@ public static final class Constants {
* Config for the log level
*/
public static final String LOG_LEVEL = "log-level";
public static final int VALUE_MAX_LENGTH = 1024;
}

private static String propertiesPath = "config/properties";
Expand Down
35 changes: 1 addition & 34 deletions src/main/java/com/botdarr/api/Api.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
package com.botdarr.api;

import com.botdarr.Config;
import com.botdarr.clients.ChatClient;
import com.botdarr.clients.ChatClientResponse;
import com.botdarr.clients.ChatClientResponseBuilder;
import com.botdarr.clients.discord.DiscordResponse;
import com.botdarr.commands.responses.CommandResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.Strings;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public interface Api {
/**
* The url base for this api (can be null/empty)
*/
String getUrlBase();

/**
* Get this api's url endpoint
*/
String getApiUrl(String path);

/**
* Gets all the in-progress downloads
*/
Expand All @@ -35,24 +17,9 @@ public interface Api {
*/
void cacheData();

/**
* Gets the auth token for this api
*/
String getApiToken();

default String getApiUrl(String apiUrlKey, String apiTokenKey, String path) {
try {
String urlBase = Strings.isBlank(getUrlBase()) ? "" : "/" + getUrlBase();
return Config.getProperty(apiUrlKey) + urlBase + "/api/" + path + "?apikey=" + URLEncoder.encode(Config.getProperty(apiTokenKey), "UTF-8");
} catch (UnsupportedEncodingException e) {
LOGGER.error("Error encoding api token", e);
throw new RuntimeException("Error calculating the api url", e);
}
}

default List<ChatClientResponse> subList(List<ChatClientResponse> responses, int max) {
return responses.subList(0, responses.size() > max ? max - 1 : responses.size());
}

static final Logger LOGGER = LogManager.getLogger();
Logger LOGGER = LogManager.getLogger();
}
54 changes: 54 additions & 0 deletions src/main/java/com/botdarr/api/ArrRequestBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.botdarr.api;

import com.botdarr.Config;
import com.botdarr.connections.RequestBuilder;
import org.apache.logging.log4j.util.Strings;
import java.util.HashMap;
import java.util.Map;

public class ArrRequestBuilder {
private final String urlConfigName;
private final String urlBaseConfigName;
private final String tokenName;
private final RequestBuilder requestBuilder = new RequestBuilder();
public ArrRequestBuilder(String urlConfigName, String urlBaseConfigName, String tokenName) {
this.urlConfigName = urlConfigName;
this.urlBaseConfigName = urlBaseConfigName;
this.tokenName = tokenName;
}

private RequestBuilder tokens(RequestBuilder requestBuilder) {
//TODO: which *arr's actually need the api key in path and headers? (this is likely a side effect from one
//of the apis requiring one or the other and me being lazy)
return requestBuilder.param("apiKey", Config.getProperty(this.tokenName))
.headers("X-Api-Key", Config.getProperty(this.tokenName));
}

private String getRadarrHost() {
String host = Config.getProperty(this.urlConfigName);
String rawUrlBase = Config.getProperty(this.urlBaseConfigName);
String urlBase = Strings.isBlank(rawUrlBase) ? "" : "/" + rawUrlBase;
return host + urlBase + getApiSuffix();
}

public String getApiSuffix() {
return "/api/";
}

public RequestBuilder buildGet(String path) {
return buildGet(path, new HashMap<>());
}

public RequestBuilder buildGet(String path, Map<String, String> params) {
for (Map.Entry<String, String> entry : params.entrySet()) {
this.requestBuilder.param(entry.getKey(), entry.getValue());
}
RequestBuilder requestBuilder = this.requestBuilder.host(getRadarrHost() + path);
return this.tokens(requestBuilder);
}

public RequestBuilder buildPost(String path, String postBody) {
RequestBuilder requestBuilder = this.requestBuilder.host(getRadarrHost() + path).post(postBody);
return this.tokens(requestBuilder);
}
}
15 changes: 8 additions & 7 deletions src/main/java/com/botdarr/api/CacheContentStrategy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.botdarr.api;

import com.botdarr.connections.ConnectionHelper;
import com.botdarr.connections.RequestBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
Expand All @@ -11,17 +12,18 @@
import java.util.List;

public abstract class CacheContentStrategy<T, Z> {
public CacheContentStrategy(Api api, String url) {
this.api = api;
this.url = url;
public CacheContentStrategy(RequestBuilder requestBuilder) {
this.requestBuilder = requestBuilder;
}

public abstract void deleteFromCache(List<Z> itemsAddedUpdated);
public abstract Z addToCache(JsonElement cacheItem);

public void cacheData() {
List<Z> itemsAddedUpdated = new ArrayList<>();
ConnectionHelper.makeGetRequest(this.api, this.url, new ConnectionHelper.SimpleEntityResponseHandler<List<T>>() {
ConnectionHelper.makeGetRequest(
this.requestBuilder,
new ConnectionHelper.SimpleEntityResponseHandler<List<T>>() {
@Override
public List<T> onSuccess(String response) throws Exception {
JsonParser parser = new JsonParser();
Expand All @@ -33,10 +35,9 @@ public List<T> onSuccess(String response) throws Exception {
}
});
deleteFromCache(itemsAddedUpdated);
LOGGER.debug("Finished caching content data for api " + api.getClass().getName());
LOGGER.debug("Finished caching content data for host " + this.requestBuilder.getHost());
}

private final Api api;
private final String url;
private final RequestBuilder requestBuilder;
private static final Logger LOGGER = LogManager.getLogger();
}
30 changes: 3 additions & 27 deletions src/main/java/com/botdarr/api/DownloadsStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.botdarr.commands.responses.CommandResponse;
import com.botdarr.commands.responses.InfoResponse;
import com.botdarr.connections.ConnectionHelper;
import com.botdarr.connections.RequestBuilder;
import com.botdarr.utilities.ListUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand All @@ -16,13 +17,8 @@
import java.util.List;

public abstract class DownloadsStrategy {
public DownloadsStrategy(Api api,
String url) {
this.api = api;
this.url = url;
}

public abstract CommandResponse getResponse(JsonElement rawElement);
public abstract List<CommandResponse> getContentDownloads();

public List<CommandResponse> downloads() {
if (MAX_DOWNLOADS_TO_SHOW <= 0) {
Expand All @@ -31,27 +27,9 @@ public List<CommandResponse> downloads() {
return getContentDownloads();
}

public List<CommandResponse> getContentDownloads() {
return ConnectionHelper.makeGetRequest(this.api, this.url, new ConnectionHelper.SimpleMessageEmbedResponseHandler() {

@Override
public List<CommandResponse> onConnectException(HttpHostConnectException e) {
String message = "Error trying to connect to " + DownloadsStrategy.this.api.getApiUrl(DownloadsStrategy.this.url);
LOGGER.error(message);
return Collections.emptyList();
}

@Override
public List<CommandResponse> onSuccess(String response) {
return parseContent(response);
}
});
}

public List<CommandResponse> parseContent(String response) {
List<CommandResponse> chatClientResponses = new ArrayList<>();
JsonParser parser = new JsonParser();
JsonArray json = parser.parse(response).getAsJsonArray();
JsonArray json = JsonParser.parseString(response).getAsJsonArray();
boolean tooManyDownloads = json.size() >= MAX_DOWNLOADS_TO_SHOW;
for (int i = 0; i < json.size(); i++) {
CommandResponse chatClientResponse = getResponse(json.get(i));
Expand All @@ -67,8 +45,6 @@ public List<CommandResponse> parseContent(String response) {
return chatClientResponses;
}

private final Api api;
private final String url;
private final int MAX_DOWNLOADS_TO_SHOW = new ApiRequests().getMaxDownloadsToShow();
private static Logger LOGGER = LogManager.getLogger();
}
Loading

0 comments on commit c10b8d8

Please sign in to comment.