Skip to content

Commit

Permalink
Improve RSS Download resultion
Browse files Browse the repository at this point in the history
  • Loading branch information
SirSparkles committed May 21, 2018
1 parent 917a01a commit 817d968
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 37 deletions.
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
34 changes: 28 additions & 6 deletions TVRename#/Forms/Preferences.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions TVRename#/Forms/Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ private void OKButton_Click(object sender, System.EventArgs e)
S.AutoCreateFolders = this.cbAutoCreateFolders.Checked ;
S.SpecialsFolderName = this.txtSpecialsFolderName.Text;
S.searchSeasonWordsString = this.tbSeasonSearchTerms.Text;
S.preferredRSSSearchTermsString = this.tbPreferredRSSTerms.Text;


S.defaultSeasonWord = this.txtSeasonFolderName.Text;
S.keepTogetherExtensionsString = this.txtKeepTogether.Text;

Expand Down Expand Up @@ -370,6 +373,7 @@ private void Preferences_Load(object sender, System.EventArgs e)
this.txtOtherExtensions.Text = S.GetOtherExtensionsString();
this.txtKeepTogether.Text = S.GetKeepTogetherString();
this.tbSeasonSearchTerms.Text = S.GetSeasonSearchTermsString();
this.tbPreferredRSSTerms .Text = S.GetPreferredRSSSearchTermsString();

this.cbKeepTogether.Checked = S.KeepTogether;
this.cbKeepTogether_CheckedChanged(null, null);
Expand Down
15 changes: 13 additions & 2 deletions TVRename#/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,12 @@ public enum KeepTogetherModes
public string defaultSeasonWord = "Season";

public string[] searchSeasonWordsArray => this.searchSeasonWordsString.Split(';');
public string[] PreferredRSSSearchTerms() => this.preferredRSSSearchTermsString.Split(';');

public string searchSeasonWordsString = "Season;Series;Saison;Temporada;Seizoen";
public string preferredRSSSearchTermsString = "720p;1080p";



internal bool IncludeBetaUpdates()
{
return (this.mode== BetaMode.BetaToo );
Expand Down Expand Up @@ -667,6 +669,8 @@ public void load(XmlReader reader)
this.defaultSeasonWord = reader.ReadElementContentAsString( );
else if (reader.Name == "SearchSeasonNames")
this.searchSeasonWordsString = reader.ReadElementContentAsString();
else if (reader.Name == "PreferredRSSSearchTerms")
this.preferredRSSSearchTermsString = reader.ReadElementContentAsString();
else if (reader.Name == "KeepTogetherType")
this.keepTogetherMode = (KeepTogetherModes) reader.ReadElementContentAsInt();
else if (reader.Name == "KeepTogetherExtensions")
Expand Down Expand Up @@ -944,7 +948,7 @@ public void WriteXML(XmlWriter writer)
XMLHelper.WriteElementToXML(writer, "PercentDirtyUpgrade", this.upgradeDirtyPercent);
XMLHelper.WriteElementToXML(writer, "BaseSeasonName", this.defaultSeasonWord);
XMLHelper.WriteElementToXML(writer, "SearchSeasonNames", this.searchSeasonWordsString);

XMLHelper.WriteElementToXML(writer, "PreferredRSSSearchTerms", this.preferredRSSSearchTermsString);
XMLHelper.WriteElementToXML(writer, "BulkAddIgnoreRecycleBin", this.BulkAddIgnoreRecycleBin);
XMLHelper.WriteElementToXML(writer, "BulkAddCompareNoVideoFolders", this.BulkAddCompareNoVideoFolders);
XMLHelper.WriteElementToXML(writer, "AutoAddMovieTerms", this.AutoAddMovieTerms);
Expand Down Expand Up @@ -1018,6 +1022,8 @@ internal float PercentDirtyUpgrade()
public bool RunOnStartUp() => this.runStartupCheck;

public string GetSeasonSearchTermsString() => this.searchSeasonWordsString;
public string GetPreferredRSSSearchTermsString() => this.preferredRSSSearchTermsString;


public static bool OKExtensionsString(string s)
{
Expand Down Expand Up @@ -1235,5 +1241,10 @@ public bool KeepTogetherFilesWithType(string fileExtension)

return true;
}





}
}
Loading

0 comments on commit 817d968

Please sign in to comment.