Jackett/src/Jackett.Common/Indexers/TVVault.cs

190 lines
8.1 KiB
C#
Raw Normal View History

2020-02-09 02:35:16 +00:00
using System;
2016-11-19 10:33:00 +00:00
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
2017-10-29 06:50:47 +00:00
using System.Text;
using System.Text.RegularExpressions;
2017-10-29 06:50:47 +00:00
using System.Threading.Tasks;
using System.Web;
using AngleSharp.Html.Parser;
using Jackett.Common.Models;
using Jackett.Common.Models.IndexerConfig;
using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils;
using Jackett.Common.Utils.Clients;
2017-10-29 06:50:47 +00:00
using Newtonsoft.Json.Linq;
using NLog;
namespace Jackett.Common.Indexers
2016-11-19 10:33:00 +00:00
{
// This tracker is based on GazelleTracker but we can't use the API/abstract because there are some
// missing features. https://github.com/Jackett/Jackett/issues/8508
[ExcludeFromCodeCoverage]
public class TVVault : BaseWebIndexer
2016-11-19 10:33:00 +00:00
{
private string LoginUrl => SiteLink + "login.php";
private string BrowseUrl => SiteLink + "torrents.php";
2016-11-19 10:33:00 +00:00
private new ConfigurationDataBasicLogin configData => (ConfigurationDataBasicLogin)base.configData;
2016-11-19 10:33:00 +00:00
public TVVault(IIndexerConfigurationService configService, WebClient wc, Logger l, IProtectionService ps,
ICacheService cs)
: base(id: "tvvault",
name: "TV-Vault",
description: "A TV tracker for old shows",
2016-11-19 10:33:00 +00:00
link: "https://tv-vault.me/",
caps: new TorznabCapabilities
{
TvSearchParams = new List<TvSearchParam>
{
TvSearchParam.Q, TvSearchParam.Season, TvSearchParam.Ep, TvSearchParam.ImdbId
},
MovieSearchParams = new List<MovieSearchParam>
{
MovieSearchParam.Q, MovieSearchParam.ImdbId
}
},
configService: configService,
2016-11-19 10:33:00 +00:00
client: wc,
logger: l,
p: ps,
cacheService: cs,
configData: new ConfigurationDataBasicLogin())
2016-11-19 10:33:00 +00:00
{
Encoding = Encoding.UTF8;
Language = "en-US";
Type = "private";
webclient.requestDelay = 5.1; // 1 request per 5 seconds
2016-11-19 10:33:00 +00:00
AddCategoryMapping(1, TorznabCatType.TV);
AddCategoryMapping(2, TorznabCatType.Movies);
2020-12-16 04:59:37 +00:00
// as returned by TvCategoryParser.ParseTvShowQuality these two TV cats are required
AddCategoryMapping(3, TorznabCatType.TVHD);
AddCategoryMapping(4, TorznabCatType.TVSD);
2016-11-19 10:33:00 +00:00
}
public override async Task<IndexerConfigurationStatus> ApplyConfiguration(JToken configJson)
2016-11-19 10:33:00 +00:00
{
LoadValuesFromJson(configJson);
2016-11-19 10:33:00 +00:00
var pairs = new Dictionary<string, string>
{
{ "username", configData.Username.Value },
{ "password", configData.Password.Value },
{ "keeplogged", "1" },
{ "login", "Log+In!" }
};
var result = await RequestLoginAndFollowRedirect(LoginUrl, pairs, null, true, null, LoginUrl, true);
await ConfigureIfOK(result.Cookies, result.ContentString?.Contains("logout.php") == true, () =>
{
var parser = new HtmlParser();
var dom = parser.ParseDocument(result.ContentString);
var errorMessage = dom.QuerySelector("form#loginform").TextContent.Trim();
throw new ExceptionWithConfigData(errorMessage, configData);
});
2016-11-19 10:33:00 +00:00
return IndexerConfigurationStatus.RequiresTesting;
}
protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuery query)
2016-11-19 10:33:00 +00:00
{
var releases = new List<ReleaseInfo>();
2017-10-29 06:50:47 +00:00
var qc = new NameValueCollection
{
{ "disablegrouping", "1" }
};
if (query.IsImdbQuery)
{
qc.Add("action", "advanced");
qc.Add("imdbid", query.ImdbID);
}
else if (!string.IsNullOrWhiteSpace(query.GetQueryString()))
qc.Add("searchstr", StripSearchString(query.GetQueryString()));
var searchUrl = BrowseUrl + "?" + qc.GetQueryString();
var results = await RequestWithCookiesAsync(searchUrl);
2016-11-19 10:33:00 +00:00
try
{
var seasonRegEx = new Regex(@$"Season\s+0*{query.Season}[^\d]", RegexOptions.IgnoreCase);
2016-11-19 10:33:00 +00:00
var parser = new HtmlParser();
var doc = parser.ParseDocument(results.ContentString);
// get params to build download link (user could be banned without those params)
var rssFeedUri = new Uri(SiteLink + doc.QuerySelector("link[href^=\"/feeds.php?feed=\"]")
.GetAttribute("href"));
var rssFeedQuery = HttpUtility.ParseQueryString(rssFeedUri.Query);
var downloadLinkExtraParams = "&authkey=" + rssFeedQuery["authkey"] + "&torrent_pass=" + rssFeedQuery["passkey"];
// parse rows
var rows = doc.QuerySelectorAll("table.torrent_table > tbody > tr.torrent");
foreach (var row in rows)
2016-11-19 10:33:00 +00:00
{
var qDetailsLink = row.QuerySelector("a[href^=\"torrents.php?id=\"]");
var title = qDetailsLink.TextContent;
// if it's a season search, we filter results. the trailing space is to match regex
if (query.Season > 0 && !seasonRegEx.Match($"{title} ").Success)
continue;
var description = qDetailsLink.NextSibling.TextContent.Trim();
title += " " + description;
var details = new Uri(SiteLink + qDetailsLink.GetAttribute("href"));
var torrentId = qDetailsLink.GetAttribute("href").Split('=').Last();
var link = new Uri(SiteLink + "torrents.php?action=download&id=" + torrentId + downloadLinkExtraParams);
var files = ParseUtil.CoerceLong(row.QuerySelector("td:nth-child(3)").TextContent);
var publishDate = DateTimeUtil.FromTimeAgo(row.QuerySelector("td:nth-child(4)").TextContent);
var size = ReleaseInfo.GetBytes(row.QuerySelector("td:nth-child(5)").FirstChild.TextContent);
var grabs = ParseUtil.CoerceLong(row.QuerySelector("td:nth-child(6)").TextContent);
var seeders = ParseUtil.CoerceInt(row.QuerySelector("td:nth-child(7)").TextContent);
var leechers = ParseUtil.CoerceInt(row.QuerySelector("td:nth-child(8)").TextContent);
var dlVolumeFactor = row.QuerySelector("strong.freeleech_normal") != null ? 0 : 1;
var category = new List<int> { TvCategoryParser.ParseTvShowQuality(description) };
var release = new ReleaseInfo
{
MinimumRatio = 1,
MinimumSeedTime = 0,
Description = description,
Title = title,
PublishDate = publishDate,
Category = category,
Link = link,
Details = details,
Guid = link,
Seeders = seeders,
Peers = leechers + seeders,
Size = size,
Grabs = grabs,
Files = files,
DownloadVolumeFactor = dlVolumeFactor,
UploadVolumeFactor = 1
};
2016-11-19 10:33:00 +00:00
releases.Add(release);
}
2016-11-19 10:33:00 +00:00
}
catch (Exception ex)
{
OnParseError(results.ContentString, ex);
2016-11-19 10:33:00 +00:00
}
return releases;
}
private string StripSearchString(string term)
{
// Search does not support searching with episode numbers so strip it if we have one
2020-12-16 04:59:37 +00:00
// AND filter the result later to achieve the proper result
term = Regex.Replace(term, @"[S|E]\d\d", string.Empty);
return term.Trim();
}
2016-11-19 10:33:00 +00:00
}
}