From 96469be7f043f9a5930ca316fcb8361092843d23 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sat, 14 Mar 2015 16:06:46 +0100 Subject: [PATCH] Fixed: Can now specify a cookie for BitMeTv. --- src/NzbDrone.Common/Http/HttpClient.cs | 9 +++++++++ src/NzbDrone.Common/Http/HttpRequest.cs | 17 +++++++++++++++++ .../IndexerTests/BitMeTvTests/BitMeTVFixture.cs | 2 +- .../Indexers/BitMeTv/BitMeTvRequestGenerator.cs | 10 +++++++--- .../Indexers/BitMeTv/BitMeTvSettings.cs | 13 ++++++++++++- 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index 464d2ff53..03f9d92d2 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -59,6 +59,15 @@ namespace NzbDrone.Common.Http AddRequestHeaders(webRequest, request.Headers); } + if (request.Cookies.Count != 0) + { + webRequest.CookieContainer = new CookieContainer(); + foreach (var pair in request.Cookies) + { + webRequest.CookieContainer.Add(new Cookie(pair.Key, pair.Value, "/", request.Url.Host)); + } + } + if (!request.Body.IsNullOrWhiteSpace()) { var bytes = request.Headers.GetEncodingFromContentType().GetBytes(request.Body.ToCharArray()); diff --git a/src/NzbDrone.Common/Http/HttpRequest.cs b/src/NzbDrone.Common/Http/HttpRequest.cs index 34c2954d6..787fab2d8 100644 --- a/src/NzbDrone.Common/Http/HttpRequest.cs +++ b/src/NzbDrone.Common/Http/HttpRequest.cs @@ -14,6 +14,7 @@ namespace NzbDrone.Common.Http Headers = new HttpHeader(); _segments = new Dictionary(); AllowAutoRedirect = true; + Cookies = new Dictionary(); if (httpAccept != null) { @@ -44,6 +45,7 @@ namespace NzbDrone.Common.Http public NetworkCredential NetworkCredential { get; set; } public bool SuppressHttpError { get; set; } public bool AllowAutoRedirect { get; set; } + public Dictionary Cookies { get; private set; } public override string ToString() { @@ -66,5 +68,20 @@ namespace NzbDrone.Common.Http _segments.Add(key, value); } + + public void AddCookie(string key, string value) + { + Cookies[key] = value; + } + + public void AddCookie(string cookies) + { + foreach (var pair in cookies.Split(';')) + { + var split = pair.Split('='); + + Cookies[split[0].Trim()] = split[1].Trim(); + } + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core.Test/IndexerTests/BitMeTvTests/BitMeTVFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/BitMeTvTests/BitMeTVFixture.cs index 9ec01f30a..2d6c93e29 100644 --- a/src/NzbDrone.Core.Test/IndexerTests/BitMeTvTests/BitMeTVFixture.cs +++ b/src/NzbDrone.Core.Test/IndexerTests/BitMeTvTests/BitMeTVFixture.cs @@ -20,7 +20,7 @@ namespace NzbDrone.Core.Test.IndexerTests.BitMeTvTests Subject.Definition = new IndexerDefinition() { Name = "BitMeTV", - Settings = new BitMeTvSettings() + Settings = new BitMeTvSettings() { Cookie = "uid=123" } }; } diff --git a/src/NzbDrone.Core/Indexers/BitMeTv/BitMeTvRequestGenerator.cs b/src/NzbDrone.Core/Indexers/BitMeTv/BitMeTvRequestGenerator.cs index fee49be5d..d23c42efa 100644 --- a/src/NzbDrone.Core/Indexers/BitMeTv/BitMeTvRequestGenerator.cs +++ b/src/NzbDrone.Core/Indexers/BitMeTv/BitMeTvRequestGenerator.cs @@ -14,7 +14,7 @@ namespace NzbDrone.Core.Indexers.BitMeTv { var pageableRequests = new List>(); - pageableRequests.AddIfNotNull(GetRssRequests(null)); + pageableRequests.AddIfNotNull(GetRssRequests()); return pageableRequests; } @@ -44,9 +44,13 @@ namespace NzbDrone.Core.Indexers.BitMeTv return new List>(); } - private IEnumerable GetRssRequests(String searchParameters) + private IEnumerable GetRssRequests() { - yield return new IndexerRequest(String.Format("{0}/rss.php?uid={1}&passkey={2}{3}", Settings.BaseUrl.Trim().TrimEnd('/'), Settings.UserId, Settings.RssPasskey, searchParameters), HttpAccept.Html); + var request = new IndexerRequest(String.Format("{0}/rss.php?uid={1}&passkey={2}", Settings.BaseUrl.Trim().TrimEnd('/'), Settings.UserId, Settings.RssPasskey), HttpAccept.Html); + + request.HttpRequest.AddCookie(Settings.Cookie); + + yield return request; } } } diff --git a/src/NzbDrone.Core/Indexers/BitMeTv/BitMeTvSettings.cs b/src/NzbDrone.Core/Indexers/BitMeTv/BitMeTvSettings.cs index 05dafdf1c..27c64c70e 100644 --- a/src/NzbDrone.Core/Indexers/BitMeTv/BitMeTvSettings.cs +++ b/src/NzbDrone.Core/Indexers/BitMeTv/BitMeTvSettings.cs @@ -1,4 +1,5 @@ using System; +using System.Text.RegularExpressions; using FluentValidation; using FluentValidation.Results; using NzbDrone.Core.Annotations; @@ -14,6 +15,13 @@ namespace NzbDrone.Core.Indexers.BitMeTv RuleFor(c => c.BaseUrl).ValidRootUrl(); RuleFor(c => c.UserId).NotEmpty(); RuleFor(c => c.RssPasskey).NotEmpty(); + + RuleFor(c => c.Cookie).NotEmpty(); + + RuleFor(c => c.Cookie) + .Matches(@"pass=[0-9a-f]{32}", RegexOptions.IgnoreCase) + .WithMessage("Wrong pattern") + .AsWarning(); } } @@ -23,7 +31,7 @@ namespace NzbDrone.Core.Indexers.BitMeTv public BitMeTvSettings() { - BaseUrl = "http://www.bitmetv.org"; + BaseUrl = "https://www.bitmetv.org"; } [FieldDefinition(0, Label = "Website URL")] @@ -35,6 +43,9 @@ namespace NzbDrone.Core.Indexers.BitMeTv [FieldDefinition(2, Label = "RSS Passkey")] public String RssPasskey { get; set; } + [FieldDefinition(3, Label = "Cookie", HelpText = "BitMeTv uses a login cookie needed to access the rss, you'll have to retrieve it via a browser.")] + public String Cookie { get; set; } + public ValidationResult Validate() { return validator.Validate(this);