Skip to content

Commit

Permalink
Merge pull request #409 from MarkSummerville/25
Browse files Browse the repository at this point in the history
25
  • Loading branch information
SirSparkles authored May 21, 2018
2 parents c553253 + 817d968 commit 0259f7a
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 122 deletions.
4 changes: 2 additions & 2 deletions TVRename#/Exporter/ShowsHTML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public override void Run()
private static string CreateHTML(ShowItem si)
{
string posterURL = TheTVDB.GetBannerURL(si.TheSeries().GetImage(TVSettings.FolderJpgIsType.Poster));
int minYear = si.TheSeries().MinYear;
int maxYear = si.TheSeries().MaxYear;
int minYear = si.TheSeries().MinYear();
int maxYear = si.TheSeries().MaxYear();
string yearRange = (minYear==maxYear) ? minYear.ToString() : minYear + "-" + maxYear;
string episodeSummary = si.TheSeries().AiredSeasons.Sum(pair => pair.Value.Episodes.Count).ToString();
string stars = StarRating(si.TheSeries().GetSiteRating());
Expand Down
34 changes: 22 additions & 12 deletions TVRename#/Finders/FileFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class FileFinder:Finder
{
public FileFinder(TVDoc i) : base(i) { }

private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

public override bool Active() => TVSettings.Instance.SearchLocally;

Expand Down Expand Up @@ -56,9 +56,10 @@ public override void Check(SetProgressDelegate prog, int startpct, int totPct)

foreach (DirCacheEntry dce in dirCache)
{
if (ReviewFile(me, thisRound, dce)) {numberMatched++;
matchedFile = dce;
}
if (!ReviewFile(me, thisRound, dce)) continue;

numberMatched++;
matchedFile = dce;
}

if (numberMatched == 1 )
Expand All @@ -68,9 +69,9 @@ public override void Check(SetProgressDelegate prog, int startpct, int totPct)
toRemove.Add(action1);
newList.AddRange(thisRound);
}
else logger.Warn($"Ignoring potential match for {action1.Episode.SI.ShowName} S{action1.Episode.AppropriateSeasonNumber} E{action1.Episode.AppropriateEpNum}: with file {matchedFile.TheFile.FullName} as there are multiple actions for that file");
else Logger.Warn($"Ignoring potential match for {action1.Episode.SI.ShowName} S{action1.Episode.AppropriateSeasonNumber} E{action1.Episode.AppropriateEpNum}: with file {matchedFile?.TheFile.FullName} as there are multiple actions for that file");
}
else if (numberMatched>1) { logger.Warn($"Ignoring potential match for {action1.Episode.SI.ShowName} S{action1.Episode.AppropriateSeasonNumber} E{action1.Episode.AppropriateEpNum}: with file {matchedFile.TheFile.FullName} as there are multiple files for that action");}
else if (numberMatched>1) { Logger.Warn($"Ignoring potential match for {action1.Episode.SI.ShowName} S{action1.Episode.AppropriateSeasonNumber} E{action1.Episode.AppropriateEpNum}: with file {matchedFile?.TheFile.FullName} as there are multiple files for that action");}

}

Expand Down Expand Up @@ -133,7 +134,16 @@ private bool OtherActionsMatch(DirCacheEntry matchedFile, ItemMissing me, ItemLi
if (!(testAction is ItemMissing testMissingAction)) continue;
if (testMissingAction.SameAs(me)) continue;

if (ReviewFile(testMissingAction, new ItemList(), matchedFile)) return true;
if (ReviewFile(testMissingAction, new ItemList(), matchedFile))
{
//We have 2 options that match me and testAction - See whether one is subset of the other

if (me.Episode.SI.ShowName.Contains(testMissingAction.Episode.SI.ShowName)) continue; //'me' is a better match, so don't worry about the new one

return true;


}

}

Expand Down Expand Up @@ -206,7 +216,7 @@ private void KeepTogether(ItemList actionlist)
catch (System.IO.PathTooLongException e)
{
string t = "Path or filename too long. " + action.From.FullName + ", " + e.Message;
logger.Warn(e, "Path or filename too long. " + action.From.FullName);
Logger.Warn(e, "Path or filename too long. " + action.From.FullName);

if ((!this.Doc.Args.Unattended) && (!this.Doc.Args.Hide)) MessageBox.Show(t, "Path or filename too long", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

Expand Down Expand Up @@ -284,9 +294,9 @@ private bool ReviewFile(ItemMissing me, ItemList addTo, DirCacheEntry dce)
if (!me.Episode.SI.SeasonRules.ContainsKey(seasF)) me.Episode.SI.SeasonRules[seasF] = new List<ShowRule>();

me.Episode.SI.SeasonRules[seasF].Add(sr);
logger.Info(
Logger.Info(
$"Looking at {me.Episode.SI.ShowName} and have identified that episode {epF} and {maxEp} of season {seasF} have been merged into one file {dce.TheFile.FullName}");
logger.Info($"Added new rule automatically for {sr}");
Logger.Info($"Added new rule automatically for {sr}");

//Regenerate the episodes with the new rule added
this.Doc.Library.GenerateEpisodeDict(me.Episode.SI);
Expand Down Expand Up @@ -345,7 +355,7 @@ private bool ReviewFile(ItemMissing me, ItemList addTo, DirCacheEntry dce)
catch (System.IO.PathTooLongException e)
{
string t = "Path too long. " + dce.TheFile.FullName + ", " + e.Message;
logger.Warn(e, "Path too long. " + dce.TheFile.FullName);
Logger.Warn(e, "Path too long. " + dce.TheFile.FullName);

t += ". More information is available in the log file";
if ((!this.Doc.Args.Unattended) && (!this.Doc.Args.Hide))
Expand All @@ -359,7 +369,7 @@ private bool ReviewFile(ItemMissing me, ItemList addTo, DirCacheEntry dce)
t += "To: " + me.TheFileNoExt;
}

logger.Warn(t);
Logger.Warn(t);
}

return false;
Expand Down
105 changes: 79 additions & 26 deletions TVRename#/Finders/RSSFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,119 @@

namespace TVRename
{
class RSSFinder:Finder
// ReSharper disable once InconsistentNaming
internal class RSSFinder:Finder
{
public RSSFinder(TVDoc i) : base(i) { }

private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

public override bool Active() => TVSettings.Instance.SearchRSS;

public override Finder.FinderDisplayType DisplayType() => FinderDisplayType.RSS;
public override FinderDisplayType DisplayType() => FinderDisplayType.RSS;

public override void Check(SetProgressDelegate prog, int startpct, int totPct)
{
int c = this.ActionList.Count + 2;
int n = 1;
prog.Invoke(startpct);
// ReSharper disable once InconsistentNaming
RSSItemList RSSList = new RSSItemList();
foreach (string s in TVSettings.Instance.RSSURLs)
RSSList.DownloadRSS(s, TVSettings.Instance.FNPRegexs);

ItemList newItems = new ItemList();
ItemList toRemove = new ItemList();

foreach (Item Action1 in this.ActionList)
foreach (Item testItem in this.ActionList)
{
if (this.ActionCancel)
return;

prog.Invoke(startpct + (totPct - startpct) * (++n) / (c));

if (!(Action1 is ItemMissing))
if (!(testItem is ItemMissing action))
continue;

ItemMissing Action = (ItemMissing)(Action1);

ProcessedEpisode pe = Action.Episode;
ProcessedEpisode pe = action.Episode;
string simpleShowName = Helpers.SimplifyName(pe.SI.ShowName);
string simpleSeriesName = Helpers.SimplifyName(pe.TheSeries.Name);

foreach (RSSItem rss in RSSList)
{
if ((FileHelper.SimplifyAndCheckFilename(rss.ShowName, simpleShowName, true, false) || (string.IsNullOrEmpty(rss.ShowName) && FileHelper.SimplifyAndCheckFilename(rss.Title, simpleSeriesName, true, false))) && (rss.Season == pe.AppropriateSeasonNumber) && (rss.Episode == pe.AppropriateEpNum ))
if (
!FileHelper.SimplifyAndCheckFilename(rss.ShowName, simpleShowName, true, false) &&
!(
string.IsNullOrEmpty(rss.ShowName) &&
FileHelper.SimplifyAndCheckFilename(rss.Title, simpleSeriesName, true, false)
)
) continue;

if (rss.Season != pe.AppropriateSeasonNumber) continue;
if (rss.Episode != pe.AppropriateEpNum) continue;

Logger.Info($"Adding {rss.URL} as it appears to be match for {testItem.Episode.SI.ShowName} S{testItem.Episode.AppropriateSeasonNumber}E{testItem.Episode.AppropriateEpNum}");
newItems.Add(new ActionRSS(rss, action.TheFileNoExt, pe));
toRemove.Add(testItem);
}
}

//We now want to rationlise the newItems - just in case we've added duplicates
List<ActionRSS> duplicateActionRSS = new List<ActionRSS>();

foreach (Item x in newItems)
{
if (!(x is ActionRSS testActionRSSOne))
continue;
foreach (Item y in newItems)
{
if (!(y is ActionRSS testActionRSSTwo))
continue;
if (x.Equals(y)) continue;

string[] preferredTerms = TVSettings.Instance.PreferredRSSSearchTerms();

if (testActionRSSOne.RSS.ShowName.ContainsOneOf(preferredTerms) &&
!testActionRSSTwo.RSS.ShowName.ContainsOneOf(preferredTerms))
{
duplicateActionRSS.Add(testActionRSSTwo);
Logger.Info($"Removing {testActionRSSTwo.RSS.URL} as it is not as good a match as {testActionRSSOne.RSS.URL }");
}

if (testActionRSSOne.RSS.Title.ContainsOneOf(preferredTerms) &&
!testActionRSSTwo.RSS.Title.ContainsOneOf(preferredTerms))
{
newItems.Add(new ActionRSS(rss, Action.TheFileNoExt, pe));
toRemove.Add(Action1);

duplicateActionRSS.Add(testActionRSSTwo);
Logger.Info(
$"Removing {testActionRSSTwo.RSS.URL} as it is not as good a match as {testActionRSSOne.RSS.URL}");
}
}
}

foreach (ActionRSS x in duplicateActionRSS)
newItems.Remove(x);

foreach (Item i in toRemove)
this.ActionList.Remove(i);

foreach (Item Action in newItems)
this.ActionList.Add(Action);
foreach (Item action in newItems)
this.ActionList.Add(action);

prog.Invoke(totPct);

}

}
// ReSharper disable once InconsistentNaming
public class RSSItem
{
public int Episode;
public int Season;
public string ShowName;
public string Title;
public string URL;
public readonly int Episode;
public readonly int Season;
public readonly string ShowName;
public readonly string Title;
// ReSharper disable once InconsistentNaming
public readonly string URL;

public RSSItem(string url, string title, int season, int episode, string showName)
{
Expand All @@ -79,13 +129,15 @@ public RSSItem(string url, string title, int season, int episode, string showNam
}
}

public class RSSItemList : System.Collections.Generic.List<RSSItem>
// ReSharper disable once InconsistentNaming
public class RSSItemList : List<RSSItem>
{
private List<FilenameProcessorRE> Rexps; // only trustable while in DownloadRSS or its called functions
private List<FilenameProcessorRE> regxps; // only trustable while in DownloadRSS or its called functions

// ReSharper disable once InconsistentNaming
public bool DownloadRSS(string URL, List<FilenameProcessorRE> rexps)
{
this.Rexps = rexps;
this.regxps = rexps;

System.Net.WebClient wc = new System.Net.WebClient();
try
Expand Down Expand Up @@ -120,7 +172,7 @@ public bool DownloadRSS(string URL, List<FilenameProcessorRE> rexps)

if (reader.Name == "channel")
{
if (!this.ReadChannel(reader.ReadSubtree()))
if (!ReadChannel(reader.ReadSubtree()))
return false;
reader.Read();
}
Expand All @@ -135,7 +187,7 @@ public bool DownloadRSS(string URL, List<FilenameProcessorRE> rexps)
}
finally
{
this.Rexps = null;
this.regxps = null;
}

return true;
Expand All @@ -151,7 +203,7 @@ private bool ReadChannel(XmlReader r)
break;
if (r.Name == "item")
{
if (!this.ReadItem(r.ReadSubtree()))
if (!ReadItem(r.ReadSubtree()))
return false;
r.Read();
}
Expand All @@ -161,7 +213,7 @@ private bool ReadChannel(XmlReader r)
return true;
}

public bool ReadItem(XmlReader r)
private bool ReadItem(XmlReader r)
{
string title = "";
string link = "";
Expand Down Expand Up @@ -192,7 +244,7 @@ public bool ReadItem(XmlReader r)

string showName = "";

TVDoc.FindSeasEp("", title, out int season, out int episode, out int maxEp, null, this.Rexps);
TVDoc.FindSeasEp("", title, out int season, out int episode, out int maxEp, null, this.regxps);

try
{
Expand All @@ -208,10 +260,11 @@ public bool ReadItem(XmlReader r)
}
catch
{
// ignored
}

if ((season != -1) && (episode != -1))
this.Add(new RSSItem(link, title, season, episode, showName));
Add(new RSSItem(link, title, season, episode, showName));

return true;
}
Expand Down
Loading

0 comments on commit 0259f7a

Please sign in to comment.