Skip to content

Commit

Permalink
Auto find seasons on Jackett
Browse files Browse the repository at this point in the history
  • Loading branch information
SirSparkles committed Mar 23, 2023
1 parent 731db39 commit 613dcf1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
20 changes: 20 additions & 0 deletions TVRename/ItemsAndActions/ItemDownloading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

using Alphaleonis.Win32.Filesystem;
using CefSharp;
using System;

namespace TVRename;
Expand All @@ -23,6 +24,9 @@ public class ItemDownloading : Item
public override string? DestinationFile => entry.FileIdentifier;
public override string SourceDetails => entry.RemainingText ?? string.Empty;

private readonly ShowConfiguration? internalShow;
private readonly int? internalSeasonNumber;

public override int IconNumber { get; }
public override string? TargetFolder => string.IsNullOrEmpty(entry.Destination) ? null : new FileInfo(entry.Destination).DirectoryName;

Expand Down Expand Up @@ -51,6 +55,22 @@ public ItemDownloading(IDownloadInformation dl, MovieConfiguration mc, string de
Movie = mc;
}

public ItemDownloading(IDownloadInformation dl, ShowConfiguration series, int? seasonNumberAsInt, string desiredLocationNoExt, DownloadingFinder.DownloadApp tApp, ItemMissing me)
: this(dl, desiredLocationNoExt, tApp, me)
{
Episode=null;
Movie = null;
internalShow = series;
internalSeasonNumber = seasonNumberAsInt;
}

public override string SeriesName => internalShow?.Name ?? base.SeriesName;
public override string SeasonNumber => internalSeasonNumber is null
? base.SeasonNumber
: TVSettings.SeasonNameFor(internalSeasonNumber.Value);
public override int? SeasonNumberAsInt => internalSeasonNumber ?? base.SeasonNumberAsInt;
public override ShowConfiguration? Series => internalShow ?? base.Series;

#region Item Members

public override bool SameAs(Item o) => o is ItemDownloading torrent && entry == torrent.entry;
Expand Down
6 changes: 6 additions & 0 deletions TVRename/ScanActivity/Finders/DownloadFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright (c) TV Rename. This code is released under GPLv3 https://github.com/TV-Rename/tvrename/blob/master/LICENSE.md
//

using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -42,6 +43,11 @@ protected static bool RssMatch(RSSItem rss, MovieConfiguration pe)
string simpleShowName = pe.ShowName.CompareName();
return FileHelper.SimplifyAndCheckFilename(rss.ShowName.HasValue() ? rss.ShowName : rss.Title, simpleShowName, true, false);
}
protected static bool RssMatch(RSSItem rss, ShowConfiguration series, int seasonNumberAsInt)
{
string simpleShowName = $"{series.ShowName.CompareName()} S{seasonNumberAsInt}";
return FileHelper.SimplifyAndCheckFilename(rss.ShowName.HasValue() ? rss.ShowName : rss.Title, simpleShowName, true, false);
}

protected static IEnumerable<ActionTDownload> Rationalize(ItemList newItems)
=> newItems.DownloadTorrents.Where(HasEnoughSeeders).Where(NotContainsUnwantedTerms).WithMax(NumberOfGoodTerms);
Expand Down
51 changes: 48 additions & 3 deletions TVRename/ScanActivity/Finders/JackettFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using TMDbLib.Objects.TvShows;

namespace TVRename;

Expand Down Expand Up @@ -62,9 +63,9 @@ protected override void DoCheck(SetProgressDelegate progress)
case MovieItemMissing movieItemMissing:
FindMissingEpisode(movieItemMissing, toRemove, newItems);
break;
//case ShowSeasonMissing seasonMissing:
//TODO - FindMissingSeason(seasonMissing, toRemove, newItems);
//break;
case ShowSeasonMissing seasonMissing:
FindMissingSeason(seasonMissing, toRemove, newItems);
break;
}
}
}
Expand Down Expand Up @@ -145,6 +146,50 @@ private static void FindMissingEpisode(MovieItemMissing action, ItemList toRemov
}
}

private static void FindMissingSeason(ShowSeasonMissing action, ItemList toRemove, ItemList newItems)
{
string url = TVSettings.Instance.UseJackettTextSearch ? TextJackettUrl(action.Series , action.SeasonNumberAsInt) : NormalJackettUrl(action.Series, action.SeasonNumberAsInt);

RssItemList rssList = new();
rssList.DownloadRSS(url, false, "Jackett");
ItemList newItemsForThisMissingEpisode = new();

foreach (RSSItem rss in rssList.Where(rss => RssMatch(rss, action.Series, action.SeasonNumberAsInt??0 )))
{
if (TVSettings.Instance.DetailedRSSJSONLogging)
{
LOGGER.Info(
$"Adding {rss.URL} from RSS feed as it appears to be match for {action.Show.ShowName} S{action.SeasonNumber}");
}
ItemDownloading becomes = new(new FutureTorrentEntry(rss.URL, action.TheFileNoExt), action.Series, action.SeasonNumberAsInt, action.TheFileNoExt, DownloadingFinder.DownloadApp.qBitTorrent, action);
newItemsForThisMissingEpisode.Add(new ActionTDownload(rss, action, becomes));
}

System.Collections.Generic.IEnumerable<ActionTDownload> bestDownloads = Rationalize(newItemsForThisMissingEpisode);

if (bestDownloads.HasAny())
{
newItems.AddNullableRange(bestDownloads);
toRemove.Add(action);
}
}
private static string NormalJackettUrl(ShowConfiguration series, int? seasonNumberAsInt)
{
string apikey = TVSettings.Instance.JackettAPIKey;
string simpleShowName = WebUtility.UrlEncode(series.ShowName.CompareName());

return
$"{IndexerUrl()}api?t=tvsearch&q={simpleShowName}&tvdbid={series.TvdbCode}&season={seasonNumberAsInt}&apikey={apikey}";
}

private static string TextJackettUrl(ShowConfiguration series, int? seasonNumberAsInt)
{
string apikey = TVSettings.Instance.JackettAPIKey;
const string FORMAT = "{ShowName} S{Season:2}E{Episode}[-E{Episode2}]";
string text = WebUtility.UrlEncode(CustomSeasonName.NameFor(series,seasonNumberAsInt??0, FORMAT));
return $"{IndexerUrl()}api?t=tvsearch&q={text}&apikey={apikey}";
}

private static string IndexerUrl()
{
string serverName = TVSettings.Instance.JackettServer;
Expand Down
5 changes: 1 addition & 4 deletions TVRename/Settings/TVSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public static TVSettings Instance
lock (syncRoot)
{
// ReSharper disable once ConvertIfStatementToNullCoalescingAssignment
if (instance is null)
{
instance = new TVSettings();
}
instance ??= new TVSettings();
}
}

Expand Down

0 comments on commit 613dcf1

Please sign in to comment.