/src/NzbDrone.Core/ImportLists/Sonarr/SonarrImport.cs

https://github.com/NzbDrone/NzbDrone · C# · 131 lines · 111 code · 19 blank · 1 comment · 12 complexity · 93c3e1aca3494385f9ad3d809931574c MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FluentValidation.Results;
  5. using NLog;
  6. using NzbDrone.Common.Extensions;
  7. using NzbDrone.Core.Configuration;
  8. using NzbDrone.Core.Parser;
  9. using NzbDrone.Core.Validation;
  10. using NzbDrone.Core.Parser.Model;
  11. namespace NzbDrone.Core.ImportLists.Sonarr
  12. {
  13. public class SonarrImport : ImportListBase<SonarrSettings>
  14. {
  15. private readonly ISonarrV3Proxy _sonarrV3Proxy;
  16. public override string Name => "Sonarr";
  17. public override ImportListType ListType => ImportListType.Program;
  18. public SonarrImport(ISonarrV3Proxy sonarrV3Proxy,
  19. IImportListStatusService importListStatusService,
  20. IConfigService configService,
  21. IParsingService parsingService,
  22. Logger logger)
  23. : base(importListStatusService, configService, parsingService, logger)
  24. {
  25. _sonarrV3Proxy = sonarrV3Proxy;
  26. }
  27. public override IList<ImportListItemInfo> Fetch()
  28. {
  29. var series = new List<ImportListItemInfo>();
  30. try
  31. {
  32. var remoteSeries = _sonarrV3Proxy.GetSeries(Settings);
  33. foreach (var item in remoteSeries)
  34. {
  35. if ((!Settings.ProfileIds.Any() || Settings.ProfileIds.Contains(item.QualityProfileId)) &&
  36. (!Settings.LanguageProfileIds.Any() || Settings.LanguageProfileIds.Contains(item.LanguageProfileId)) &&
  37. (!Settings.TagIds.Any() || Settings.TagIds.Any(tagId => item.Tags.Any(itemTagId => itemTagId == tagId))))
  38. {
  39. series.Add(new ImportListItemInfo
  40. {
  41. TvdbId = item.TvdbId,
  42. Title = item.Title
  43. });
  44. }
  45. }
  46. _importListStatusService.RecordSuccess(Definition.Id);
  47. }
  48. catch
  49. {
  50. _importListStatusService.RecordFailure(Definition.Id);
  51. }
  52. return CleanupListItems(series);
  53. }
  54. public override object RequestAction(string action, IDictionary<string, string> query)
  55. {
  56. // Return early if there is not an API key
  57. if (Settings.ApiKey.IsNullOrWhiteSpace())
  58. {
  59. return new
  60. {
  61. devices = new List<object>()
  62. };
  63. }
  64. if (action == "getProfiles")
  65. {
  66. Settings.Validate().Filter("ApiKey").ThrowOnError();
  67. var profiles = _sonarrV3Proxy.GetQualityProfiles(Settings);
  68. return new
  69. {
  70. options = profiles.OrderBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase)
  71. .Select(d => new
  72. {
  73. value = d.Id,
  74. name = d.Name
  75. })
  76. };
  77. }
  78. if (action == "getLanguageProfiles")
  79. {
  80. Settings.Validate().Filter("ApiKey").ThrowOnError();
  81. var langProfiles = _sonarrV3Proxy.GetLanguageProfiles(Settings);
  82. return new
  83. {
  84. options = langProfiles.OrderBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase)
  85. .Select(d => new
  86. {
  87. value = d.Id,
  88. name = d.Name
  89. })
  90. };
  91. }
  92. if (action == "getTags")
  93. {
  94. var tags = _sonarrV3Proxy.GetTags(Settings);
  95. return new
  96. {
  97. options = tags.OrderBy(d => d.Label, StringComparer.InvariantCultureIgnoreCase)
  98. .Select(d => new
  99. {
  100. value = d.Id,
  101. name = d.Label
  102. })
  103. };
  104. }
  105. return new { };
  106. }
  107. protected override void Test(List<ValidationFailure> failures)
  108. {
  109. failures.AddIfNotNull(_sonarrV3Proxy.Test(Settings));
  110. }
  111. }
  112. }