Skip to content

Commit

Permalink
fix(sonarr): V4 actually works this time around
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Nov 24, 2022
1 parent 0e8940d commit f62e70f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/Ombi.Api.Sonarr/ISonarrV3Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface ISonarrV3Api : ISonarrApi
Task<IEnumerable<LanguageProfiles>> LanguageProfiles(string apiKey, string baseUrl);
Task<Tag> CreateTag(string apiKey, string baseUrl, string tagName);
Task<Tag> GetTag(int tagId, string apiKey, string baseUrl);
Task<List<MonitoredEpisodeResult>> MonitorEpisode(int[] episodeIds, bool monitor, string apiKey, string baseUrl);
}
}
19 changes: 15 additions & 4 deletions src/Ombi.Api.Sonarr/Models/Episode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Episode
{
public Episode()
{

}

public Episode(Episode ep)
Expand Down Expand Up @@ -53,7 +53,7 @@ public class Episodefile
{
public Episodefile()
{

}

public Episodefile(Episodefile e)
Expand Down Expand Up @@ -85,7 +85,7 @@ public class EpisodeQuality
{
public EpisodeQuality()
{

}

public EpisodeQuality(EpisodeQuality e)
Expand All @@ -101,7 +101,7 @@ public class Revision
{
public Revision()
{

}

public Revision(Revision r)
Expand All @@ -113,6 +113,17 @@ public Revision(Revision r)
public int real { get; set; }
}

public class MonitoredEpisodeResult
{
public int seriesId { get; set; }
public int tvdbId { get; set; }
public int episodeFileId { get; set; }
public int seasonNumber { get; set; }
public int episodeNumber { get; set; }
public string overview { get; set; }
public bool monitored { get; set; }
public int id { get; set; }
}

public class EpisodeUpdateResult
{
Expand Down
8 changes: 8 additions & 0 deletions src/Ombi.Api.Sonarr/SonarrV3Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@ public Task<Tag> GetTag(int tagId, string apiKey, string baseUrl)

return Api.Request<Tag>(request);
}

public async Task<List<MonitoredEpisodeResult>> MonitorEpisode(int[] episodeIds, bool monitor, string apiKey, string baseUrl)
{
var request = new Request($"{ApiBaseUrl}Episode/monitor", baseUrl, HttpMethod.Put);
request.AddHeader("X-Api-Key", apiKey);
request.AddJsonBody(new { episodeIds = episodeIds, monitored = monitor });
return await Api.Request<List<MonitoredEpisodeResult>>(request);
}
}
}
24 changes: 8 additions & 16 deletions src/Ombi.Core/Senders/TvSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public TvSender(ISonarrApi sonarrApi, ISonarrV3Api sonarrV3Api, ILogger<TvSender
ISettingsService<DogNzbSettings> dog, IDogNzbApi dogApi, ISettingsService<SickRageSettings> srSettings,
ISickRageApi srApi, IRepository<UserQualityProfiles> userProfiles, IRepository<RequestQueue> requestQueue, INotificationHelper notify)
{
SonarrApi = sonarrApi;
SonarrV3Api = sonarrV3Api;
SonarrApi = sonarrV3Api;
Logger = log;
SonarrSettings = sonarrSettings;
DogNzbSettings = dog;
Expand All @@ -40,8 +39,7 @@ public TvSender(ISonarrApi sonarrApi, ISonarrV3Api sonarrV3Api, ILogger<TvSender
_notificationHelper = notify;
}

private ISonarrApi SonarrApi { get; }
private ISonarrV3Api SonarrV3Api { get; }
private ISonarrV3Api SonarrApi { get; }
private IDogNzbApi DogNzbApi { get; }
private ISickRageApi SickRageApi { get; }
private ILogger<TvSender> Logger { get; }
Expand Down Expand Up @@ -324,16 +322,16 @@ private async Task<Tag> GetOrCreateTag(ChildRequests model, SonarrSettings s)
var tagName = model.RequestedUser.UserName;
// Does tag exist?

var allTags = await SonarrV3Api.GetTags(s.ApiKey, s.FullUri);
var allTags = await SonarrApi.GetTags(s.ApiKey, s.FullUri);
var existingTag = allTags.FirstOrDefault(x => x.label.Equals(tagName, StringComparison.InvariantCultureIgnoreCase));
existingTag ??= await SonarrV3Api.CreateTag(s.ApiKey, s.FullUri, tagName);
existingTag ??= await SonarrApi.CreateTag(s.ApiKey, s.FullUri, tagName);

return existingTag;
}

private async Task<Tag> GetTag(int tagId, SonarrSettings s)
{
var tag = await SonarrV3Api.GetTag(tagId, s.ApiKey, s.FullUri);
var tag = await SonarrApi.GetTag(tagId, s.ApiKey, s.FullUri);
if (tag == null)
{
Logger.LogError($"Tag ID {tagId} does not exist in sonarr. Please update the settings");
Expand Down Expand Up @@ -424,16 +422,10 @@ private async Task SendToSonarr(ChildRequests model, SonarrSeries result, Sonarr
epToUnmonitored.Add(ep);
}

foreach (var epToUpdate in epToUnmonitored)
{
await SonarrApi.UpdateEpisode(epToUpdate, s.ApiKey, s.FullUri);
}
await SonarrApi.MonitorEpisode(epToUnmonitored.Select(x => x.id).ToArray(), false, s.ApiKey, s.FullUri);
}
// Now update the episodes that need updating
foreach (var epToUpdate in episodesToUpdate.Where(x => x.seasonNumber == season.SeasonNumber))
{
await SonarrApi.UpdateEpisode(epToUpdate, s.ApiKey, s.FullUri);
}
await SonarrApi.MonitorEpisode(episodesToUpdate.Where(x => x.seasonNumber == season.SeasonNumber).Select(x => x.id).ToArray(), true, s.ApiKey, s.FullUri);
}

if (!s.AddOnly)
Expand Down Expand Up @@ -575,7 +567,7 @@ private async Task<string> GetSonarrRootPath(int pathId, SonarrSettings sonarrSe
return rootFoldersResult.FirstOrDefault().path;
}

foreach (var r in rootFoldersResult.Where(r => r.id == pathId))
foreach (var r in rootFoldersResult?.Where(r => r.id == pathId))
{
return r.path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class SonarrComponent implements OnInit {
if (settings.rootPath) {
this.getRootFolders(this.form);
}
if (settings.languageProfile) {
if (settings.languageProfile && this.sonarrVersion === "3") {
this.getLanguageProfiles(this.form);
}
if (settings.tag || settings.animeTag) {
Expand Down

0 comments on commit f62e70f

Please sign in to comment.