1
0
Fork 0
mirror of https://github.com/Jackett/Jackett synced 2024-12-29 11:17:22 +00:00

Interim commit

This commit is contained in:
KZ 2015-07-19 01:59:30 +01:00
parent 33a97b148f
commit e518a3b58e
6 changed files with 88 additions and 54 deletions

View file

@ -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);

View file

@ -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<IndexerInterface, JToken> OnSaveConfigurationRequested;
event Action<IndexerInterface, string, Exception> OnResultParsingError;
string DisplayName { get; }
string DisplayDescription { get; }
Uri SiteLink { get; }

View file

@ -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<IndexerInterface, JToken> OnSaveConfigurationRequested;
public event Action<IndexerInterface, string, Exception> 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<ConfigurationData> GetConfigurationForSetup()
public override Task<ConfigurationData> GetConfigurationForSetup()
{
var config = new ConfigurationDataBasicLogin();
return Task.FromResult<ConfigurationData>(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<ReleaseInfo[]> PerformQuery(TorznabQuery query)
public override async Task<ReleaseInfo[]> PerformQuery(TorznabQuery query)
{
List<ReleaseInfo> releases = new List<ReleaseInfo>();
@ -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<byte[]> Download(Uri link)
public override async Task<byte[]> Download(Uri link)
{
if (WebServer.IsWindows)
{

View file

@ -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<byte[]> Download(Uri link);
public abstract Task<ConfigurationData> GetConfigurationForSetup();
public abstract void LoadFromSavedConfiguration(JToken jsonConfig);
public abstract Task<ReleaseInfo[]> 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);
}
}
}

View file

@ -133,6 +133,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Indexers\BaseIndexer.cs" />
<Compile Include="Models\ApiKey.cs" />
<Compile Include="Utils\BrowserUtil.cs" />
<Compile Include="Models\CachedResult.cs" />

View file

@ -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<IndexerInterface> 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());
}
}
}