Jackett/src/Jackett.Common/Indexers/Shazbat.cs

184 lines
8.6 KiB
C#
Raw Normal View History

2020-02-09 02:35:16 +00:00
using System;
2017-10-29 06:50:47 +00:00
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
2017-10-29 06:50:47 +00:00
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
2020-02-29 23:27:09 +00:00
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;
using Newtonsoft.Json.Linq;
using NLog;
namespace Jackett.Common.Indexers
{
[ExcludeFromCodeCoverage]
public class Shazbat : BaseWebIndexer
{
private string LoginUrl => SiteLink + "login";
private string SearchUrl => SiteLink + "search";
private string TorrentsUrl => SiteLink + "torrents";
private string ShowUrl => SiteLink + "show?id=";
private string RSSProfile => SiteLink + "rss_feeds";
2017-10-29 06:50:47 +00:00
private new ConfigurationDataBasicLoginWithRSS configData
{
get => (ConfigurationDataBasicLoginWithRSS)base.configData;
set => base.configData = value;
}
public Shazbat(IIndexerConfigurationService configService, WebClient c, Logger l, IProtectionService ps)
: base(id: "shazbat",
name: "Shazbat",
description: "Modern indexer",
link: "https://www.shazbat.tv/",
caps: new TorznabCapabilities(
TorznabCatType.TV,
TorznabCatType.TVHD,
TorznabCatType.TVSD),
configService: configService,
client: c,
logger: l,
p: ps,
configData: new ConfigurationDataBasicLoginWithRSS())
{
Encoding = Encoding.UTF8;
Language = "en-us";
Type = "private";
}
public override async Task<IndexerConfigurationStatus> ApplyConfiguration(JToken configJson)
{
LoadValuesFromJson(configJson);
var pairs = new Dictionary<string, string>
{
{"referer", "login"},
{"query", ""},
{"tv_login", configData.Username.Value},
{"tv_password", configData.Password.Value},
{"email", ""}
};
// Get cookie
var result = await RequestLoginAndFollowRedirect(LoginUrl, pairs, null, true, null, LoginUrl);
await ConfigureIfOK(result.Cookies, result.ContentString?.Contains("glyphicon-log-out") == true,
() => throw new ExceptionWithConfigData("The username and password entered do not match.", configData));
var rssProfile = await RequestStringWithCookiesAndRetry(RSSProfile);
2020-02-29 23:27:09 +00:00
var parser = new HtmlParser();
var rssDom = parser.ParseDocument(rssProfile.ContentString);
2020-02-29 23:27:09 +00:00
configData.RSSKey.Value = rssDom.QuerySelector(".col-sm-9:nth-of-type(1)").TextContent.Trim();
if (string.IsNullOrWhiteSpace(configData.RSSKey.Value))
throw new ExceptionWithConfigData("Failed to find RSS key.", configData);
SaveConfig();
return IndexerConfigurationStatus.RequiresTesting;
}
protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuery query)
{
var releases = new List<ReleaseInfo>();
var queryString = query.GetQueryString();
WebResult results = null;
var searchUrls = new List<string>();
if (!string.IsNullOrWhiteSpace(query.SanitizedSearchTerm))
{
var pairs = new Dictionary<string, string>
{
{"search", query.SanitizedSearchTerm}
};
results = await PostDataWithCookiesAndRetry(SearchUrl, pairs, null, TorrentsUrl);
results = await ReloginIfNecessary(results);
2020-02-29 23:27:09 +00:00
var parser = new HtmlParser();
var dom = parser.ParseDocument(results.ContentString);
2020-02-29 23:27:09 +00:00
var shows = dom.QuerySelectorAll("div.show[data-id]");
foreach (var show in shows)
{
var showUrl = ShowUrl + show.GetAttribute("data-id");
searchUrls.Add(showUrl);
}
}
else
searchUrls.Add(TorrentsUrl);
try
{
foreach (var searchUrl in searchUrls)
{
results = await RequestStringWithCookies(searchUrl);
results = await ReloginIfNecessary(results);
2020-02-29 23:27:09 +00:00
var parser = new HtmlParser();
var dom = parser.ParseDocument(results.ContentString);
2020-02-29 23:27:09 +00:00
var rows = dom.QuerySelectorAll(
string.IsNullOrWhiteSpace(queryString) ? "#torrent-table tr" : "table tr");
var globalFreeleech =
dom.QuerySelector("span:contains(\"Freeleech until:\"):has(span.datetime)") != null;
foreach (var row in rows.Skip(1))
{
// TODO switch to initializer
var release = new ReleaseInfo();
2020-02-29 23:27:09 +00:00
var titleRow = row.QuerySelector("td:nth-of-type(3)");
foreach (var child in titleRow.Children)
child.Remove();
release.Title = titleRow.TextContent.Trim();
if ((query.ImdbID == null || !TorznabCaps.SupportsImdbMovieSearch) &&
!query.MatchQueryStringAND(release.Title))
continue;
var bannerStyle = row.QuerySelector("div[style^=\"cursor: pointer; background-image:url\"]")
?.GetAttribute("style");
if (!string.IsNullOrEmpty(bannerStyle))
{
var bannerImg = Regex.Match(bannerStyle, @"url\('(.*?)'\);").Groups[1].Value;
release.BannerUrl = new Uri(SiteLink + bannerImg);
}
var qLink = row.QuerySelector("td:nth-of-type(5) a");
2020-02-29 23:27:09 +00:00
release.Link = new Uri(SiteLink + qLink.GetAttribute("href"));
release.Guid = release.Link;
var qLinkComm = row.QuerySelector("td:nth-of-type(5) a.internal");
2020-02-29 23:27:09 +00:00
release.Comments = new Uri(SiteLink + qLinkComm.GetAttribute("href"));
var dateString = row.QuerySelector(".datetime")?.GetAttribute("data-timestamp");
if (dateString != null)
release.PublishDate = DateTimeUtil
.UnixTimestampToDateTime(ParseUtil.CoerceDouble(dateString)).ToLocalTime();
2020-02-29 23:27:09 +00:00
var infoString = row.QuerySelector("td:nth-of-type(4)").TextContent;
release.Size = ParseUtil.CoerceLong(
Regex.Match(infoString, "\\((\\d+)\\)").Value.Replace("(", "").Replace(")", ""));
var infosplit = infoString.Replace("/", string.Empty).Split(":".ToCharArray());
release.Seeders = ParseUtil.CoerceInt(infosplit[1]);
release.Peers = release.Seeders + ParseUtil.CoerceInt(infosplit[2]);
release.DownloadVolumeFactor = globalFreeleech ? 0 : 1;
release.UploadVolumeFactor = 1;
release.MinimumRatio = 1;
release.MinimumSeedTime = 172800; // 48 hours
2020-02-29 23:27:09 +00:00
// var tags = row.QuerySelector(".label-tag").TextContent; These don't see to parse - bad tags?
releases.Add(release);
}
}
}
catch (Exception ex)
{
OnParseError(results.ContentString, ex);
}
foreach (var release in releases)
release.Category = release.Title.Contains("1080p") || release.Title.Contains("720p")
? new List<int> { TorznabCatType.TVHD.ID }
: new List<int> { TorznabCatType.TVSD.ID };
return releases;
}
private async Task<WebResult> ReloginIfNecessary(WebResult response)
{
if (response.ContentString.Contains("onclick=\"document.location='logout'\""))
return response;
await ApplyConfiguration(null);
response.Request.Cookies = CookieHeader;
return await webclient.GetString(response.Request);
}
}
}