mejortorrent: fix download link and code clean up (#8420)

This commit is contained in:
Diego Heras
2020-04-30 10:44:44 +02:00
committed by GitHub
parent 781cf3ffb3
commit 72f59278fa

View File

@@ -19,7 +19,6 @@ using WebClient = Jackett.Common.Utils.Clients.WebClient;
namespace Jackett.Common.Indexers
{
//TODO fix ReSharper notice
// ReSharper disable once UnusedType.Global
public class MejorTorrent : BaseWebIndexer
{
@@ -96,25 +95,22 @@ namespace Jackett.Common.Indexers
public override async Task<byte[]> Download(Uri link)
{
var parser = new HtmlParser();
var downloadUrl = link.ToString();
// Eg http://www.mejortorrentt.org/peli-descargar-torrent-11995-Harry-Potter-y-la-piedra-filosofal.html
var result = await RequestStringWithCookies(downloadUrl);
if (result.Status != HttpStatusCode.OK)
throw new ExceptionWithConfigData(result.Content, configData);
var searchResultParser = new HtmlParser();
var html = searchResultParser.ParseDocument(result.Content);
downloadUrl = SiteLink + html.QuerySelector("a[href*=\"sec=descargas\"]").GetAttribute("href");
var dom = parser.ParseDocument(result.Content);
downloadUrl = SiteLink + dom.QuerySelector("a[href*=\"sec=descargas\"]").GetAttribute("href");
// Eg http://www.mejortorrentt.org/secciones.php?sec=descargas&ap=contar&tabla=peliculas&id=11995&link_bajar=1
result = await RequestStringWithCookies(downloadUrl);
if (result.Status != HttpStatusCode.OK)
throw new ExceptionWithConfigData(result.Content, configData);
searchResultParser = new HtmlParser();
html = searchResultParser.ParseDocument(result.Content);
var onclick = html.QuerySelector("a[onclick*=\"/uploads/\"]").GetAttribute("onclick");
var table = onclick.Split(new[] { "table: '" }, StringSplitOptions.None)[1].Split(new[] { "'" }, StringSplitOptions.None)[0];
var name = onclick.Split(new[] { "name: '" }, StringSplitOptions.None)[1].Split(new[] { "'" }, StringSplitOptions.None)[0];
downloadUrl = SiteLink + "uploads/torrents/" + table + "/" + name;
dom = parser.ParseDocument(result.Content);
downloadUrl = SiteLink + dom.QuerySelector("a[href*=\".torrent\"]").GetAttribute("href").TrimStart('/');
// Eg https://www.mejortorrentt.org/uploads/torrents/peliculas/Harry_Potter_1_y_la_Piedra_Filosofal_MicroHD_1080p.torrent
var content = await base.Download(new Uri(downloadUrl));
@@ -141,20 +137,15 @@ namespace Jackett.Common.Indexers
string rowQuality = null;
foreach (var row in container.Children)
{
if (row.TagName.Equals("A"))
{
rowTitle = row.TextContent;
rowCommentsLink = SiteLink + row.GetAttribute("href");
}
else if (rowPublishDate == null && row.TagName.Equals("SPAN"))
{
rowPublishDate = row.TextContent;
}
else if (rowPublishDate != null && row.TagName.Equals("SPAN"))
{
rowQuality = row.TextContent;
}
else if (row.TagName.Equals("BR"))
{
// we add parsed items to parsedCommentsLink to avoid duplicates in newest torrents
@@ -171,8 +162,6 @@ namespace Jackett.Common.Indexers
rowPublishDate = null;
rowQuality = null;
}
}
}
catch (Exception ex)
{
@@ -200,13 +189,12 @@ namespace Jackett.Common.Indexers
var table = doc.QuerySelector("#main_table_center_center2 table table");
// check the search term is valid
if (table != null && table.QuerySelector("tr table") != null)
if (table?.QuerySelector("tr table") != null)
{
// check there are results
table = table.QuerySelector("tr table");
var rows = table.QuerySelectorAll("tr");
if (rows != null && rows.Length > 0 && rows[0].QuerySelectorAll("td").Length == 2)
{
foreach (var row in rows)
{
var link = row.QuerySelector("td a");
@@ -220,7 +208,6 @@ namespace Jackett.Common.Indexers
await ParseRelease(releases, rowTitle, rowCommentsLink, rowMejortorrentCat,
null, rowQuality, query, matchWords);
}
}
}
}
catch (Exception ex)
@@ -253,9 +240,7 @@ namespace Jackett.Common.Indexers
// parsing is different for each category
if (cat == MejorTorrentCatType.Serie || cat == MejorTorrentCatType.SerieHd)
{
await ParseSeriesRelease(releases, query, title, commentsLink, cat, publishDate);
}
else if (query.Episode == null) // if it's scene series, we don't return other categories
{
if (cat == MejorTorrentCatType.Pelicula)
@@ -263,7 +248,8 @@ namespace Jackett.Common.Indexers
else
{
const long size = 104857600L; // 100 MB
GenerateRelease(releases, title, commentsLink, commentsLink, cat, publishDate, size);
var release = GenerateRelease(title, commentsLink, commentsLink, cat, publishDate, size);
releases.Add(release);
}
}
}
@@ -303,7 +289,8 @@ namespace Jackett.Common.Indexers
if (episodeTitle.ToLower().Contains("720p"))
size = 1288490188L; // 1.2 GB
GenerateRelease(releases, episodeTitle, commentsLink, downloadLink, cat, episodePublish, size);
var release = GenerateRelease(episodeTitle, commentsLink, downloadLink, cat, episodePublish, size);
releases.Add(release);
}
}
@@ -363,11 +350,12 @@ namespace Jackett.Common.Indexers
else if (title.ToLower().Contains("bdremux"))
size = 21474836480L; // 20 GB
GenerateRelease(releases, title, commentsLink, commentsLink, cat, publishDate, size);
var release = GenerateRelease(title, commentsLink, commentsLink, cat, publishDate, size);
releases.Add(release);
}
// TODO refactor for IEnumerable
private void GenerateRelease(ICollection<ReleaseInfo> releases, string title, string commentsLink,
string downloadLink, string cat, DateTime publishDate, long size)
private ReleaseInfo GenerateRelease(string title, string commentsLink, string downloadLink, string cat,
DateTime publishDate, long size)
{
var link = new Uri(downloadLink);
var comments = new Uri(commentsLink);
@@ -388,7 +376,7 @@ namespace Jackett.Common.Indexers
DownloadVolumeFactor = 0,
UploadVolumeFactor = 1
};
releases.Add(release);
return release;
}
private static bool CheckTitleMatchWords(string queryStr, string title)
@@ -460,17 +448,11 @@ namespace Jackett.Common.Indexers
{
newEpisodeTitle = "";
foreach (Match m in matches)
{
if (newEpisodeTitle.Equals(""))
{
newEpisodeTitle += "S" + m.Groups[1].Value.PadLeft(2, '0')
+ "E" + m.Groups[2].Value.PadLeft(2, '0');
}
else
{
newEpisodeTitle += "-E" + m.Groups[2].Value.PadLeft(2, '0');
}
}
// newEpisodeTitle = S05E08-E13
// newEpisodeTitle = S02E01-E02-E03
}
@@ -479,12 +461,10 @@ namespace Jackett.Common.Indexers
// episodeTitle = 1x04 - 05.
var m = Regex.Match(newEpisodeTitle, "^([0-9]+)x([0-9]+)[^0-9]+([0-9]+)[.]?$", RegexOptions.IgnoreCase);
if (m.Success)
{
newEpisodeTitle = "S" + m.Groups[1].Value.PadLeft(2, '0')
+ "E" + m.Groups[2].Value.PadLeft(2, '0') + "-"
+ "E" + m.Groups[3].Value.PadLeft(2, '0');
// newEpisodeTitle = S01E04-E05
}
// newEpisodeTitle = S01E04-E05
else
{
// episodeTitle = 1x02
@@ -534,9 +514,7 @@ namespace Jackett.Common.Indexers
else if (mejortorrentCat.Equals(MejorTorrentCatType.Pelicula) ||
mejortorrentCat.Equals(MejorTorrentCatType.Serie) ||
mejortorrentCat.Equals(MejorTorrentCatType.Musica))
{
cat = mejortorrentCat;
}
// hack to separate SD & HD series
if (cat.Equals(MejorTorrentCatType.Serie) && title.ToLower().Contains("720p"))
@@ -552,10 +530,8 @@ namespace Jackett.Common.Indexers
return null;
var longestWord = words.First();
foreach (var word in words)
{
if (word.Length >= longestWord.Length)
longestWord = word;
}
return longestWord;
}