From e518a3b58e7d5cfd15c4d68fe022419ef8fed6b9 Mon Sep 17 00:00:00 2001 From: KZ Date: Sun, 19 Jul 2015 01:59:30 +0100 Subject: [PATCH] Interim commit --- src/Jackett/CookieContainerExtensions.cs | 5 ++ src/Jackett/IndexerInterface.cs | 6 -- src/Jackett/Indexers/AlphaRatio.cs | 77 +++++++------------ src/Jackett/Indexers/BaseIndexer.cs | 43 +++++++++++ src/Jackett/Jackett.csproj | 1 + src/Jackett/Services/IndexerManagerService.cs | 10 +++ 6 files changed, 88 insertions(+), 54 deletions(-) create mode 100644 src/Jackett/Indexers/BaseIndexer.cs diff --git a/src/Jackett/CookieContainerExtensions.cs b/src/Jackett/CookieContainerExtensions.cs index 8d7343a35..83dae0bd5 100644 --- a/src/Jackett/CookieContainerExtensions.cs +++ b/src/Jackett/CookieContainerExtensions.cs @@ -50,6 +50,11 @@ namespace Jackett } } + public static void DumpToJson(this CookieContainer cookies, string uri, JToken json) + { + DumpToJson(cookies, new Uri(uri), json); + } + public static void DumpToJson(this CookieContainer cookies, Uri uri, JToken json) { json["cookie_header"] = cookies.GetCookieHeader(uri); diff --git a/src/Jackett/IndexerInterface.cs b/src/Jackett/IndexerInterface.cs index f8629b4c4..186829d0d 100644 --- a/src/Jackett/IndexerInterface.cs +++ b/src/Jackett/IndexerInterface.cs @@ -11,12 +11,6 @@ namespace Jackett { public interface IndexerInterface { - - // Invoked when the indexer configuration has been applied and verified so the cookie needs to be saved - event Action OnSaveConfigurationRequested; - - event Action OnResultParsingError; - string DisplayName { get; } string DisplayDescription { get; } Uri SiteLink { get; } diff --git a/src/Jackett/Indexers/AlphaRatio.cs b/src/Jackett/Indexers/AlphaRatio.cs index b75fc7539..501d6dabc 100644 --- a/src/Jackett/Indexers/AlphaRatio.cs +++ b/src/Jackett/Indexers/AlphaRatio.cs @@ -12,56 +12,40 @@ using System.Net.Http.Headers; using Jackett.Utils; using Jackett.Models; using NLog; +using Jackett.Services; namespace Jackett.Indexers { - public class AlphaRatio : IndexerInterface + public class AlphaRatio : BaseIndexer { - public string DisplayName - { - get { return "AlphaRatio"; } - } - - public string DisplayDescription - { - get { return "Legendary"; } - } - - public Uri SiteLink - { - get { return new Uri(BaseUrl); } - } - - - public event Action OnSaveConfigurationRequested; - public event Action OnResultParsingError; - - public bool IsConfigured { get; private set; } - - static string BaseUrl = "https://alpharatio.cc"; - - static string LoginUrl = BaseUrl + "/login.php"; - - static string SearchUrl = BaseUrl + "/ajax.php?action=browse&searchstr="; - - static string DownloadUrl = BaseUrl + "/torrents.php?action=download&id="; - - static string GuidUrl = BaseUrl + "/torrents.php?torrentid="; - - static string chromeUserAgent = BrowserUtil.ChromeUserAgent; + private string LoginUrl; + private string SearchUrl; + private string DownloadUrl; + private string GuidUrl; CookieContainer cookies; HttpClientHandler handler; HttpClient client; Logger logger; + private IIndexerManagerService managementService; string cookieHeader; - public AlphaRatio(Logger l) + public AlphaRatio(Logger l, IIndexerManagerService m): + base(name: "AlphaRatio", + description: "Legendary", + link: new Uri("https://alpharatio.cc"), + logger:l) { logger = l; - IsConfigured = false; - cookies = new CookieContainer(); + managementService = m; + + LoginUrl = SiteLink.ToString() + "/login.php"; + SearchUrl = SiteLink.ToString() + "/ajax.php?action=browse&searchstr="; + DownloadUrl = SiteLink.ToString() + "/torrents.php?action=download&id="; + GuidUrl = SiteLink.ToString() + "/torrents.php?torrentid="; + + cookies = new CookieContainer(); handler = new HttpClientHandler { CookieContainer = cookies, @@ -72,17 +56,16 @@ namespace Jackett.Indexers client = new HttpClient(handler); } - public Task GetConfigurationForSetup() + public override Task GetConfigurationForSetup() { var config = new ConfigurationDataBasicLogin(); return Task.FromResult(config); } - public async Task ApplyConfiguration(JToken configJson) + public override async Task ApplyConfiguration(JToken configJson) { var configSaveData = new JObject(); - if (OnSaveConfigurationRequested != null) - OnSaveConfigurationRequested(this, configSaveData); + managementService.SaveConfig(this, configSaveData); var config = new ConfigurationDataBasicLogin(); config.LoadValuesFromJson(configJson); @@ -129,9 +112,7 @@ namespace Jackett.Indexers } else { - if (OnSaveConfigurationRequested != null) - OnSaveConfigurationRequested(this, configSaveData); - + managementService.SaveConfig(this, configSaveData); IsConfigured = true; } } @@ -141,11 +122,11 @@ namespace Jackett.Indexers var message = new HttpRequestMessage(); message.Method = HttpMethod.Post; message.RequestUri = uri; - message.Headers.UserAgent.ParseAdd(chromeUserAgent); + message.Headers.UserAgent.ParseAdd(BrowserUtil.ChromeUserAgent); return message; } - public void LoadFromSavedConfiguration(JToken jsonConfig) + public override void LoadFromSavedConfiguration(JToken jsonConfig) { cookies.FillFromJson(SiteLink, jsonConfig, logger); cookieHeader = cookies.GetCookieHeader(SiteLink); @@ -163,7 +144,7 @@ namespace Jackett.Indexers release.Link = new Uri(DownloadUrl + id); } - public async Task PerformQuery(TorznabQuery query) + public override async Task PerformQuery(TorznabQuery query) { List releases = new List(); @@ -225,7 +206,7 @@ namespace Jackett.Indexers } catch (Exception ex) { - OnResultParsingError(this, results, ex); + LogParseError(results, ex); throw ex; } } @@ -240,7 +221,7 @@ namespace Jackett.Indexers return new DateTime(unixStart.Ticks + unixTimeStampInTicks); } - public async Task Download(Uri link) + public override async Task Download(Uri link) { if (WebServer.IsWindows) { diff --git a/src/Jackett/Indexers/BaseIndexer.cs b/src/Jackett/Indexers/BaseIndexer.cs new file mode 100644 index 000000000..97210a89b --- /dev/null +++ b/src/Jackett/Indexers/BaseIndexer.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Jackett.Models; +using Newtonsoft.Json.Linq; +using NLog; + +namespace Jackett.Indexers +{ + public abstract class BaseIndexer: IndexerInterface + { + public string DisplayDescription { get; } + public string DisplayName { get; } + public bool IsConfigured { get; protected set; } + public Uri SiteLink { get; } + + public abstract Task ApplyConfiguration(JToken configJson); + public abstract Task Download(Uri link); + public abstract Task GetConfigurationForSetup(); + public abstract void LoadFromSavedConfiguration(JToken jsonConfig); + public abstract Task PerformQuery(TorznabQuery query); + + private Logger logger; + + public BaseIndexer(string name, string description, Uri link, Logger logger) + { + DisplayName = name; + DisplayDescription = description; + SiteLink = link; + this.logger = logger; + } + + protected void LogParseError(string results, Exception ex) + { + var fileName = string.Format("Error on {0} for {1}.txt", DateTime.Now.ToString("yyyyMMddHHmmss"), DisplayName); + var spacing = string.Join("", Enumerable.Repeat(Environment.NewLine, 5)); + var fileContents = string.Format("{0}{1}{2}", ex, spacing, results); + logger.Error(fileName + fileContents); + } + } +} diff --git a/src/Jackett/Jackett.csproj b/src/Jackett/Jackett.csproj index b04a292f6..a1e7eec78 100644 --- a/src/Jackett/Jackett.csproj +++ b/src/Jackett/Jackett.csproj @@ -133,6 +133,7 @@ + diff --git a/src/Jackett/Services/IndexerManagerService.cs b/src/Jackett/Services/IndexerManagerService.cs index b5e1fa966..6b16f22fb 100644 --- a/src/Jackett/Services/IndexerManagerService.cs +++ b/src/Jackett/Services/IndexerManagerService.cs @@ -1,5 +1,6 @@ using Autofac; using Jackett.Models; +using Newtonsoft.Json.Linq; using NLog; using System; using System.Collections.Generic; @@ -16,6 +17,7 @@ namespace Jackett.Services void DeleteIndexer(string name); IndexerInterface GetIndexer(string name); IEnumerable GetAllIndexers(); + void SaveConfig(IndexerInterface indexer, JToken obj); } public class IndexerManagerService : IIndexerManagerService @@ -64,5 +66,13 @@ namespace Jackett.Services { return Path.Combine(configService.GetIndexerConfigDir(), indexer.GetType().Name.ToLower() + ".json"); } + + public void SaveConfig(IndexerInterface indexer, JToken obj) + { + var configFilePath = GetIndexerConfigFilePath(indexer); + if (!Directory.Exists(configService.GetIndexerConfigDir())) + Directory.CreateDirectory(configService.GetIndexerConfigDir()); + File.WriteAllText(configFilePath, obj.ToString()); + } } }