/src/NzbDrone.Core/Indexers/Gazelle/GazelleRequestGenerator.cs

https://github.com/lidarr/Lidarr · C# · 135 lines · 106 code · 29 blank · 0 comment · 9 complexity · 6f747bf73ae18983c354020fc6f9d0ec MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using NLog;
  4. using NzbDrone.Common.Cache;
  5. using NzbDrone.Common.Extensions;
  6. using NzbDrone.Common.Http;
  7. using NzbDrone.Common.Serializer;
  8. using NzbDrone.Core.IndexerSearch.Definitions;
  9. namespace NzbDrone.Core.Indexers.Gazelle
  10. {
  11. public class GazelleRequestGenerator : IIndexerRequestGenerator
  12. {
  13. public GazelleSettings Settings { get; set; }
  14. public ICached<Dictionary<string, string>> AuthCookieCache { get; set; }
  15. public IHttpClient HttpClient { get; set; }
  16. public Logger Logger { get; set; }
  17. public virtual IndexerPageableRequestChain GetRecentRequests()
  18. {
  19. var pageableRequests = new IndexerPageableRequestChain();
  20. pageableRequests.Add(GetRequest(null));
  21. return pageableRequests;
  22. }
  23. public IndexerPageableRequestChain GetSearchRequests(AlbumSearchCriteria searchCriteria)
  24. {
  25. var pageableRequests = new IndexerPageableRequestChain();
  26. pageableRequests.Add(GetRequest(string.Format("&artistname={0}&groupname={1}", searchCriteria.ArtistQuery, searchCriteria.AlbumQuery)));
  27. return pageableRequests;
  28. }
  29. public IndexerPageableRequestChain GetSearchRequests(ArtistSearchCriteria searchCriteria)
  30. {
  31. var pageableRequests = new IndexerPageableRequestChain();
  32. pageableRequests.Add(GetRequest(string.Format("&artistname={0}", searchCriteria.ArtistQuery)));
  33. return pageableRequests;
  34. }
  35. private IEnumerable<IndexerRequest> GetRequest(string searchParameters)
  36. {
  37. Authenticate();
  38. var filter = "";
  39. if (searchParameters == null)
  40. {
  41. }
  42. var request =
  43. new IndexerRequest(
  44. $"{Settings.BaseUrl.Trim().TrimEnd('/')}/ajax.php?action=browse&searchstr={searchParameters}{filter}",
  45. HttpAccept.Json);
  46. var cookies = AuthCookieCache.Find(Settings.BaseUrl.Trim().TrimEnd('/'));
  47. foreach (var cookie in cookies)
  48. {
  49. request.HttpRequest.Cookies[cookie.Key] = cookie.Value;
  50. }
  51. yield return request;
  52. }
  53. private GazelleAuthResponse GetIndex(Dictionary<string, string> cookies)
  54. {
  55. var indexRequestBuilder = new HttpRequestBuilder($"{Settings.BaseUrl.Trim().TrimEnd('/')}")
  56. {
  57. LogResponseContent = true
  58. };
  59. indexRequestBuilder.SetCookies(cookies);
  60. indexRequestBuilder.Method = HttpMethod.POST;
  61. indexRequestBuilder.Resource("ajax.php?action=index");
  62. var authIndexRequest = indexRequestBuilder
  63. .Accept(HttpAccept.Json)
  64. .Build();
  65. var indexResponse = HttpClient.Execute(authIndexRequest);
  66. var result = Json.Deserialize<GazelleAuthResponse>(indexResponse.Content);
  67. return result;
  68. }
  69. private void Authenticate()
  70. {
  71. var requestBuilder = new HttpRequestBuilder($"{Settings.BaseUrl.Trim().TrimEnd('/')}")
  72. {
  73. LogResponseContent = true
  74. };
  75. requestBuilder.Method = HttpMethod.POST;
  76. requestBuilder.Resource("login.php");
  77. requestBuilder.PostProcess += r => r.RequestTimeout = TimeSpan.FromSeconds(15);
  78. var authKey = Settings.BaseUrl.Trim().TrimEnd('/');
  79. var cookies = AuthCookieCache.Find(authKey);
  80. if (cookies == null)
  81. {
  82. AuthCookieCache.Remove(authKey);
  83. var authLoginRequest = requestBuilder
  84. .AddFormParameter("username", Settings.Username)
  85. .AddFormParameter("password", Settings.Password)
  86. .AddFormParameter("keeplogged", "1")
  87. .SetHeader("Content-Type", "multipart/form-data")
  88. .Accept(HttpAccept.Json)
  89. .Build();
  90. var response = HttpClient.Execute(authLoginRequest);
  91. cookies = response.GetCookies();
  92. AuthCookieCache.Set(authKey, cookies);
  93. }
  94. var index = GetIndex(cookies);
  95. if (index == null || index.Status.IsNullOrWhiteSpace() || index.Status != "success")
  96. {
  97. Logger.Debug("Gazelle authentication failed.");
  98. AuthCookieCache.Remove(authKey);
  99. throw new Exception("Failed to authenticate with Gazelle.");
  100. }
  101. Logger.Debug("Gazelle authentication succeeded.");
  102. Settings.AuthKey = index.Response.Authkey;
  103. Settings.PassKey = index.Response.Passkey;
  104. }
  105. }
  106. }