Jackett/src/Jackett/Indexers/MoreThanTV.cs

237 lines
7.7 KiB
C#
Raw Normal View History

2015-04-19 02:05:38 +00:00
using CsQuery;
2015-07-19 00:27:41 +00:00
using Jackett.Models;
2015-04-19 02:05:38 +00:00
using Newtonsoft.Json.Linq;
2015-07-19 00:27:41 +00:00
using NLog;
2015-04-19 02:05:38 +00:00
using System;
using System.Collections.Generic;
2015-05-25 23:50:20 +00:00
using System.IO;
2015-04-19 02:05:38 +00:00
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Jackett.Indexers
{
2015-07-19 13:22:50 +00:00
public class MoreThanTV : IIndexer
2015-04-19 02:05:38 +00:00
{
public string DisplayName
{
get { return "MoreThanTV"; }
}
public string DisplayDescription
{
get { return "ROMANIAN Private Torrent Tracker for TV / MOVIES, and the internal tracker for the release group DRACULA"; }
}
public Uri SiteLink
{
get { return new Uri(BaseUrl); }
}
public bool RequiresRageIDLookupDisabled { get { return true; } }
2015-04-19 02:05:38 +00:00
2015-07-19 13:22:50 +00:00
public event Action<IIndexer, JToken> OnSaveConfigurationRequested;
public event Action<IIndexer, string, Exception> OnResultParsingError;
2015-04-19 02:05:38 +00:00
public bool IsConfigured { get; private set; }
2015-04-22 00:57:04 +00:00
static string BaseUrl = "https://www.morethan.tv";
2015-04-19 02:05:38 +00:00
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=";
CookieContainer cookies;
HttpClientHandler handler;
HttpClient client;
2015-07-19 14:08:56 +00:00
private Logger logger;
2015-04-19 02:05:38 +00:00
string cookieHeader;
2015-05-25 23:50:20 +00:00
int retries = 3;
2015-07-19 00:27:41 +00:00
public MoreThanTV(Logger l)
2015-04-19 02:05:38 +00:00
{
2015-07-19 00:27:41 +00:00
logger = l;
2015-04-19 02:05:38 +00:00
IsConfigured = false;
cookies = new CookieContainer();
handler = new HttpClientHandler
{
CookieContainer = cookies,
AllowAutoRedirect = true,
UseCookies = true,
};
client = new HttpClient(handler);
}
public Task<ConfigurationData> GetConfigurationForSetup()
{
var config = new ConfigurationDataBasicLogin();
return Task.FromResult<ConfigurationData>(config);
}
2015-04-22 00:57:04 +00:00
public async Task ApplyConfiguration(JToken configJson)
2015-04-19 02:05:38 +00:00
{
var config = new ConfigurationDataBasicLogin();
config.LoadValuesFromJson(configJson);
var pairs = new Dictionary<string, string> {
{ "username", config.Username.Value },
{ "password", config.Password.Value },
{ "login", "Log in" },
{ "keeplogged", "1" }
};
2015-04-19 02:05:38 +00:00
var content = new FormUrlEncodedContent(pairs);
string responseContent;
var configSaveData = new JObject();
2015-07-19 13:22:50 +00:00
if (Engine.IsWindows)
{
// If Windows use .net http
var response = await client.PostAsync(LoginUrl, content);
responseContent = await response.Content.ReadAsStringAsync();
cookies.DumpToJson(SiteLink, configSaveData);
}
else
{
// If UNIX system use curl
var response = await CurlHelper.PostAsync(LoginUrl, pairs);
responseContent = Encoding.UTF8.GetString(response.Content);
cookieHeader = response.CookieHeader;
configSaveData["cookie_header"] = cookieHeader;
}
2015-04-19 02:05:38 +00:00
if (!responseContent.Contains("logout.php?"))
{
CQ dom = responseContent;
dom["#loginform > table"].Remove();
var errorMessage = dom["#loginform"].Text().Trim().Replace("\n\t", " ");
throw new ExceptionWithConfigData(errorMessage, (ConfigurationData)config);
2015-04-19 02:05:38 +00:00
}
else
{
if (OnSaveConfigurationRequested != null)
OnSaveConfigurationRequested(this, configSaveData);
IsConfigured = true;
}
}
public void LoadFromSavedConfiguration(JToken jsonConfig)
2015-04-19 02:05:38 +00:00
{
2015-07-19 00:27:41 +00:00
cookies.FillFromJson(SiteLink, jsonConfig, logger);
cookieHeader = cookies.GetCookieHeader(SiteLink);
2015-04-19 02:05:38 +00:00
IsConfigured = true;
}
2015-05-25 23:50:20 +00:00
static void FillReleaseInfoFromJson(ReleaseInfo release, JObject r)
2015-04-19 02:05:38 +00:00
{
var id = r["torrentId"];
release.Size = (long)r["size"];
release.Seeders = (int)r["seeders"];
release.Peers = (int)r["leechers"] + release.Seeders;
release.Guid = new Uri(GuidUrl + id);
release.Comments = release.Guid;
release.Link = new Uri(DownloadUrl + id);
}
public async Task<ReleaseInfo[]> PerformQuery(TorznabQuery query)
{
List<ReleaseInfo> releases = new List<ReleaseInfo>();
var searchString = query.SanitizedSearchTerm + " " + query.GetEpisodeSearchString();
var episodeSearchUrl = SearchUrl + HttpUtility.UrlEncode(searchString);
2015-04-19 02:05:38 +00:00
string results;
2015-07-19 14:08:56 +00:00
if (Engine.IsWindows)
{
results = await client.GetStringAsync(episodeSearchUrl, retries);
}
else
{
var response = await CurlHelper.GetAsync(episodeSearchUrl, cookieHeader);
results = Encoding.UTF8.GetString(response.Content);
}
try
{
var json = JObject.Parse(results);
foreach (JObject r in json["response"]["results"])
{
DateTime pubDate = DateTime.MinValue;
double dateNum;
if (double.TryParse((string)r["groupTime"], out dateNum))
2015-04-19 02:05:38 +00:00
{
pubDate = UnixTimestampToDateTime(dateNum);
pubDate = DateTime.SpecifyKind(pubDate, DateTimeKind.Utc).ToLocalTime();
}
2015-05-04 04:12:14 +00:00
var groupName = (string)r["groupName"];
2015-05-04 04:12:14 +00:00
if (r["torrents"] is JArray)
{
foreach (JObject t in r["torrents"])
2015-04-19 02:05:38 +00:00
{
var release = new ReleaseInfo();
release.PublishDate = pubDate;
release.Title = groupName;
release.Description = groupName;
FillReleaseInfoFromJson(release, t);
2015-04-19 02:05:38 +00:00
releases.Add(release);
}
2015-05-04 04:12:14 +00:00
}
else
{
var release = new ReleaseInfo();
release.PublishDate = pubDate;
release.Title = groupName;
release.Description = groupName;
FillReleaseInfoFromJson(release, r);
releases.Add(release);
}
2015-05-04 04:12:14 +00:00
}
}
catch (Exception ex)
{
OnResultParsingError(this, results, ex);
throw ex;
2015-04-19 02:05:38 +00:00
}
return releases.ToArray();
}
static DateTime UnixTimestampToDateTime(double unixTime)
{
DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
long unixTimeStampInTicks = (long)(unixTime * TimeSpan.TicksPerSecond);
return new DateTime(unixStart.Ticks + unixTimeStampInTicks);
}
public async Task<byte[]> Download(Uri link)
2015-04-19 02:05:38 +00:00
{
2015-07-19 13:22:50 +00:00
if (Engine.IsWindows)
{
return await client.GetByteArrayAsync(link);
}
else
{
var response = await CurlHelper.GetAsync(link.ToString(), cookieHeader);
return response.Content;
}
2015-04-19 02:05:38 +00:00
}
}
}