Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review Show Collections Functions #503

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
31e09d8
Merge pull request #1 from Chlorel/master
Chlorel Sep 6, 2018
e451972
Merge pull request #2 from Chlorel/master
Chlorel Sep 6, 2018
4ee3f85
ShowCollections
Chlorel Sep 6, 2018
cedfbb2
Merge pull request #4 from Chlorel/master
Chlorel Sep 9, 2018
f1fe27f
Merge pull request #5 from Chlorel/master
Chlorel Sep 11, 2018
06334f1
Merge pull request #6 from Chlorel/master
Chlorel Sep 14, 2018
a3c5a00
Merge pull request #7 from TV-Rename/master
Chlorel Oct 2, 2018
b1fd7fa
Merge branch 'coll-showcollection' into master
Chlorel Oct 2, 2018
c983e99
Merge pull request #8 from Chlorel/master
Chlorel Oct 2, 2018
5144dfe
Revert "Merge branch 'coll-showcollection' into master"
Chlorel Oct 7, 2018
7beed44
Merge pull request #10 from TV-Rename/master
Chlorel Oct 7, 2018
f82e4e4
Merge pull request #11 from Chlorel/master
Chlorel Oct 7, 2018
2a27b9c
Merge pull request #12 from TV-Rename/master
Chlorel Oct 14, 2018
109b473
Merge pull request #13 from TV-Rename/master
Chlorel Oct 14, 2018
654cb9c
Back to past
Chlorel Oct 15, 2018
8370b4f
Merge pull request #15 from TV-Rename/master
Chlorel Oct 16, 2018
7067185
Up To 2.6.x Rcx
Chlorel Oct 16, 2018
993886d
Merge branch 'coll-showcollection' into master
Chlorel Oct 16, 2018
3bfccea
Merge pull request #16 from Chlorel/master
Chlorel Oct 16, 2018
3d5e305
App Base changes
Chlorel Oct 16, 2018
22551b3
better vers
Chlorel Oct 16, 2018
2055162
One other way to get to last stand ...
Chlorel Oct 16, 2018
9a136ca
Conflit ?
Chlorel Oct 16, 2018
062d639
Oops
Chlorel Oct 16, 2018
7397f76
4 spaces removing later ....
Chlorel Oct 16, 2018
ab296a2
Merge pull request #19 from TV-Rename/master
Chlorel Oct 16, 2018
99e2cb8
Fix for https://groups.google.com/forum/#!topic/tvrename/sspgpyMLYpI
Chlorel Oct 18, 2018
ba0234e
Up to last commit
Chlorel Oct 27, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions TVRename/App/ApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ protected override void OnCreateSplashScreen()
protected override void OnCreateMainForm()
{
CommandLineArgs clargs = new CommandLineArgs(CommandLineArgs);
PathManager.ShowCollection = "";

if (clargs.Hide)
SplashScreen.SafeInvoke(
() => ((TVRenameSplash)SplashScreen).Visible = false, true);
Expand All @@ -55,44 +57,41 @@ protected override void OnCreateMainForm()

SetupCustomSettings(clargs);

FileInfo tvdbFile = PathManager.TVDBFile;
FileInfo settingsFile = PathManager.TVDocSettingsFile;
TVDoc doc;

do // Loop until files correctly load
{
// Try loading settings file
doc = new TVDoc(clargs);

FileInfo tvdbFile = PathManager.TVDBFile;
FileInfo showsFile = PathManager.TVDocShowsFile;

if (recover) doc.SetDirty();
recover = !doc.LoadOk;

// Continue if correctly loaded
if (!recover) continue;

// Set recover message
recoverText = string.Empty;
if (!doc.LoadOk && !string.IsNullOrEmpty(doc.LoadErr)) recoverText = doc.LoadErr;
if (!TheTVDB.Instance.LoadOk && !string.IsNullOrEmpty(TheTVDB.Instance.LoadErr)) recoverText += $"{Environment.NewLine}{TheTVDB.Instance.LoadErr}";
if (recover) // Recovery required, prompt user
{
RecoverXML recoveryForm = new RecoverXML(recoverText);

if (recoveryForm.ShowDialog() == DialogResult.OK)
{
tvdbFile = recoveryForm.DbFile;
settingsFile = recoveryForm.SettingsFile;
showsFile = recoveryForm.SettingsFile;
}
else
{
// TODO: Throw an error
return;
}
}

// Try loading TheTVDB cache file
TheTVDB.Instance.Setup(tvdbFile, PathManager.TVDBFile, clargs);

// Try loading settings file
doc = new TVDoc(settingsFile, clargs);

if (recover) doc.SetDirty();
recover = !doc.LoadOk;

// Continue if correctly loaded
if (!recover) continue;

// Set recover message
recoverText = string.Empty;
if (!doc.LoadOk && !string.IsNullOrEmpty(doc.LoadErr)) recoverText = doc.LoadErr;
if (!TheTVDB.Instance.LoadOk && !string.IsNullOrEmpty(TheTVDB.Instance.LoadErr)) recoverText += $"{Environment.NewLine}{TheTVDB.Instance.LoadErr}";
} while (recover);

ConvertSeriesTimeZones(doc, TheTVDB.Instance);
Expand Down
2 changes: 1 addition & 1 deletion TVRename/Custom Controls/MyListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected override void OnItemSelectionChanged(ListViewItemSelectionChangedEvent

protected override void OnItemCheck(ItemCheckEventArgs ice)
{
if (!menuCheck && !keyCheck && !checkEnable)
if (!menuCheck && !keyCheck && (false == checkEnable))
{
ice.NewValue = ice.CurrentValue;
return;
Expand Down
2 changes: 1 addition & 1 deletion TVRename/DownloadIdentifers/DownloadXBMCImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public override ItemList ProcessSeason(ShowItem si, string folder, int snum, boo

public override ItemList ProcessEpisode(ProcessedEpisode dbep, FileInfo filo, bool forceRefresh)
{
if (TVSettings.Instance.EpTBNs || TVSettings.Instance.KODIImages)
if (TVSettings.Instance.EpTBNs)
{
ItemList theActionList = new ItemList();
if (dbep.Type == ProcessedEpisode.ProcessedEpisodeType.merged)
Expand Down
49 changes: 49 additions & 0 deletions TVRename/Finders/DownloadFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Main website for TVRename is http://tvrename.com
//
// Source code available at https://github.com/TV-Rename/tvrename
//
// This code is released under GPLv3 https://github.com/TV-Rename/tvrename/blob/master/LICENSE.md
//

using System.Collections.Generic;

namespace TVRename
{
public abstract class DownloadFinder:Finder
{
protected DownloadFinder(TVDoc doc) : base(doc)
{
}

protected static IEnumerable<ActionTDownload> FindDuplicates(ItemList newItems)
{
//We now want to rationlise the newItems - just in case we've added duplicates
List<ActionTDownload> duplicateActionRss = new List<ActionTDownload>();

foreach (Item x in newItems)
{
if (!(x is ActionTDownload testActionRssOne))
continue;
foreach (Item y in newItems)
{
if (!(y is ActionTDownload testActionRssTwo))
continue;
if (x.Equals(y)) continue;

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

if (testActionRssOne.SourceName.ContainsOneOf(preferredTerms) &&
!testActionRssTwo.SourceName.ContainsOneOf(preferredTerms))
{
duplicateActionRss.Add(testActionRssTwo);
Logger.Info(
$"Removing {testActionRssTwo.Produces} as it is not as good a match as {testActionRssOne.Produces}");
}
}
}

return duplicateActionRss;
}
}
}
78 changes: 56 additions & 22 deletions TVRename/Finders/FileFinder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//
// Main website for TVRename is http://tvrename.com
//
// Source code available at https://github.com/TV-Rename/tvrename
//
// This code is released under GPLv3 https://github.com/TV-Rename/tvrename/blob/master/LICENSE.md
//

using System;
using System.Collections.Generic;
using System.Windows.Forms;
Expand Down Expand Up @@ -37,15 +45,13 @@ public override void Check(SetProgressDelegate prog, int startpct, int totPct)

int currentItem = 0;
int totalN = ActionList.Count;
foreach (Item action1 in ActionList)
foreach (ItemMissing me in ActionList.MissingItems())
{
if (ActionCancel)
return;

prog.Invoke(startpct + ((totPct-startpct) * (++currentItem) / (totalN + 1)));

if (!(action1 is ItemMissing me)) continue;

ItemList thisRound = new ItemList();
List<DirCacheEntry> matchedFiles= new List<DirCacheEntry>();

Expand All @@ -60,19 +66,36 @@ public override void Check(SetProgressDelegate prog, int startpct, int totPct)
{
if (!OtherActionsMatch(matchedFiles[0], me))
{
toRemove.Add(action1);
toRemove.Add(me);
newList.AddRange(thisRound);
}
else
{
Logger.Warn($"Ignoring potential match for {me.Episode.Show.ShowName} S{me.Episode.AppropriateSeasonNumber} E{action1.Episode.AppropriateEpNum}: with file {matchedFiles[0]?.TheFile.FullName} as there are multiple actions for that file");
Logger.Warn($"Ignoring potential match for {me.Episode.Show.ShowName} S{me.Episode.AppropriateSeasonNumber} E{me.Episode.AppropriateEpNum}: with file {matchedFiles[0]?.TheFile.FullName} as there are multiple actions for that file");
me.AddComment(
$"Ignoring potential match with file {matchedFiles[0]?.TheFile.FullName} as there are multiple actions for that file");
}
}
else if (matchedFiles.Count > 1)
else if (matchedFiles.Count > 1)
{
foreach (DirCacheEntry matchedFile in matchedFiles)
List<DirCacheEntry> bestMatchedFiles = IdentifyBestMatches(matchedFiles);

if (bestMatchedFiles.Count == 1)
{
Logger.Warn($"Ignoring potential match for {me.Episode.Show.ShowName} S{me.Episode.AppropriateSeasonNumber} E{action1.Episode.AppropriateEpNum}: with file {matchedFile?.TheFile.FullName} as there are multiple files for that action");
//We have one file that is better, lets use it
toRemove.Add(me);
newList.AddRange(thisRound);
}
else
{
foreach (DirCacheEntry matchedFile in matchedFiles)
{
Logger.Warn(
$"Ignoring potential match for {me.Episode.Show.ShowName} S{me.Episode.AppropriateSeasonNumber} E{me.Episode.AppropriateEpNum}: with file {matchedFile?.TheFile.FullName} as there are multiple files for that action");

me.AddComment(
$"Ignoring potential match with file {matchedFile?.TheFile.FullName} as there are multiple files for that action");
}
}
}
}
Expand Down Expand Up @@ -127,12 +150,34 @@ public override void Check(SetProgressDelegate prog, int startpct, int totPct)
ActionList.Add(i);
}

private static List<DirCacheEntry> IdentifyBestMatches(List<DirCacheEntry> matchedFiles)
{
//See whether there are any of the matched files that stand out
List<DirCacheEntry> bestMatchedFiles = new List<DirCacheEntry>();
foreach (DirCacheEntry matchedFile in matchedFiles)
{
//test first file against all the others
bool betterThanAllTheRest = true;
foreach (DirCacheEntry compareAgainst in matchedFiles)
{
if (matchedFile.TheFile.FullName == compareAgainst.TheFile.FullName) continue;
if (FileHelper.BetterQualityFile(matchedFile.TheFile, compareAgainst.TheFile) !=
FileHelper.VideoComparison.FirstFileBetter)
{
betterThanAllTheRest = false;
}
}
if (betterThanAllTheRest) bestMatchedFiles.Add(matchedFile);
}

return bestMatchedFiles;
}

private bool OtherActionsMatch(DirCacheEntry matchedFile, Item me)
//This is used to check whether the selected file may match any other files we are looking for
{
foreach (Item testAction in ActionList)
foreach (ItemMissing testMissingAction in ActionList.MissingItems())
{
if (!(testAction is ItemMissing testMissingAction)) continue;
if (testMissingAction.SameAs(me)) continue;

if (ReviewFile(testMissingAction, new ItemList(), matchedFile))
Expand Down Expand Up @@ -234,18 +279,7 @@ private static bool AlreadyHaveAction(ItemList actionlist, Item action)

private static bool ActionListContains(ItemList actionlist, ActionCopyMoveRename newitem)
{
foreach (Item ai2 in actionlist)
{
if (!(ai2 is ActionCopyMoveRename))
continue;

if (((ActionCopyMoveRename)(ai2)).SameSource(newitem))
{
return true;
}
}

return false;
return actionlist.CopyMoveItems().Any(cmAction => cmAction.SameSource(newitem));
}

private bool ReviewFile(ItemMissing me, ItemList addTo, DirCacheEntry dce)
Expand Down
10 changes: 1 addition & 9 deletions TVRename/Finders/Finder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ namespace TVRename
{
public abstract class Finder
{
public enum DownloadApp
{
// ReSharper disable once InconsistentNaming
SABnzbd,
uTorrent,
qBitTorrent
}

protected bool ActionCancel;
protected readonly TVDoc Doc;
protected static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
Expand All @@ -19,7 +11,7 @@ public enum DownloadApp
protected Finder(TVDoc doc) => Doc = doc;

// ReSharper disable once InconsistentNaming
public enum FinderDisplayType { local, downloading, rss}
public enum FinderDisplayType { local, downloading, search}

public abstract void Check(SetProgressDelegate prog, int startpct, int totPct);

Expand Down
96 changes: 96 additions & 0 deletions TVRename/Finders/JSONWebpageFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Main website for TVRename is http://tvrename.com
//
// Source code available at https://github.com/TV-Rename/tvrename
//
// This code is released under GPLv3 https://github.com/TV-Rename/tvrename/blob/master/LICENSE.md
//

using System;
using System.Net;
using Newtonsoft.Json.Linq;

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

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

public override FinderDisplayType DisplayType() => FinderDisplayType.search;

public override void Check(SetProgressDelegate prog, int startpct, int totPct)
{
int c = ActionList.Count + 2;
int n = 1;
prog.Invoke(startpct);

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

foreach (ItemMissing action in ActionList.MissingItems())
{
if (ActionCancel)
return;

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

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

string imdbId= action.Episode.TheSeries.GetImdbNumber();


if (string.IsNullOrWhiteSpace(imdbId)) continue;

try
{
WebClient client = new WebClient();
client.Headers.Add("user-agent",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
string response = client.DownloadString($"{TVSettings.Instance.SearchJSONURL}{imdbId}");

JObject jsonResponse = JObject.Parse(response);

foreach (JToken item in jsonResponse[TVSettings.Instance.SearchJSONRootNode])
{
string itemName = (string)item[TVSettings.Instance.SearchJSONFilenameToken];
string itemUrl = (string)item[TVSettings.Instance.SearchJSONURLToken];

if (!FileHelper.SimplifyAndCheckFilename(itemName, simpleShowName, true, false) &&
!FileHelper.SimplifyAndCheckFilename(itemName, simpleSeriesName, true, false)) continue;

if (!TVDoc.FindSeasEp(itemName, out int seas, out int ep, out int _, action.Episode.Show))
continue;

if (seas != pe.AppropriateSeasonNumber) continue;
if (ep != pe.AppropriateEpNum) continue;

Logger.Info($"Adding {itemUrl} as it appears to be match for {pe.Show.ShowName} S{pe.AppropriateSeasonNumber}E{pe.AppropriateEpNum}");
newItems.Add(new ActionTDownload(itemName, itemUrl, action.TheFileNoExt, pe));
toRemove.Add(action);
}

}
catch (NullReferenceException _)
{
//No entries for this show
}
}

foreach (ActionTDownload x in FindDuplicates(newItems))
newItems.Remove(x);

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

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

prog.Invoke(totPct);
}
}
}
Loading