Skip to content

Commit

Permalink
Fixingup code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
SirSparkles committed Jul 6, 2022
1 parent 25d0828 commit bad674a
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 237 deletions.
3 changes: 0 additions & 3 deletions TVRename/App/ApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ protected override void OnCreateMainForm()
SplashScreen.SafeInvoke(
() => ((TVRenameSplash)SplashScreen).UpdateStatus("Initializing"), true);

// Update RegVersion to bring the WebBrowser up to speed
RegistryHelper.UpdateBrowserEmulationVersion();

doc = LoadSettings(parameters);

if (TVSettings.Instance.mode == TVSettings.BetaMode.BetaToo || TVSettings.Instance.ShareLogs)
Expand Down
4 changes: 2 additions & 2 deletions TVRename/Forms/Preferences/Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ private void UpdateSettings()
s.RemoveDownloadDirectoriesFiles = cbCleanUpDownloadDir.Checked;
s.RemoveDownloadDirectoriesFilesMatchMovies = cbCleanUpDownloadDirMovies.Checked;
s.RemoveDownloadDirectoriesFilesMatchMoviesLengthCheck = cbCleanUpDownloadDirMoviesLength.Checked;
int.TryParse(tbCleanUpDownloadDirMoviesLength.Text, out s.RemoveDownloadDirectoriesFilesMatchMoviesLengthCheckLength);
s.RemoveDownloadDirectoriesFilesMatchMoviesLengthCheckLength = tbCleanUpDownloadDirMoviesLength.Text.ToInt(8);

s.DeleteShowFromDisk = cbDeleteShowFromDisk.Checked;
s.DeleteMovieFromDisk = cbDeleteMovieFromDisk.Checked;
Expand All @@ -431,7 +431,7 @@ private void UpdateSettings()
s.Tidyup.EmptyIgnoreExtensions = cbEmptyIgnoreExtensions.Checked;
s.Tidyup.EmptyIgnoreExtensionList = txtEmptyIgnoreExtensions.Text;
s.Tidyup.EmptyMaxSizeCheck = cbEmptyMaxSize.Checked;
int.TryParse(txtEmptyMaxSize.Text, out s.Tidyup.EmptyMaxSizeMB);
s.Tidyup.EmptyMaxSizeMB = txtEmptyMaxSize.Text.ToInt(100);

s.BulkAddCompareNoVideoFolders = cbIgnoreNoVideoFolders.Checked;
s.BulkAddIgnoreRecycleBin = cbIgnoreRecycleBin.Checked;
Expand Down
56 changes: 27 additions & 29 deletions TVRename/Model/CachePersistor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,42 +105,40 @@ private static void SaveCacheFileInternal(ConcurrentDictionary<int, CachedSeries
NewLineOnAttributes = true
};

using (XmlWriter writer = XmlWriter.Create(cacheFile.FullName, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("Data");
writer.WriteAttributeToXml("time", timestamp);
using XmlWriter writer = XmlWriter.Create(cacheFile.FullName, settings);
writer.WriteStartDocument();
writer.WriteStartElement("Data");
writer.WriteAttributeToXml("time", timestamp);

foreach (KeyValuePair<int, CachedSeriesInfo> kvp in series)
foreach (KeyValuePair<int, CachedSeriesInfo> kvp in series)
{
if (kvp.Value.SrvLastUpdated != 0)
{
if (kvp.Value.SrvLastUpdated != 0)
{
kvp.Value.WriteXml(writer);
}
else
{
Logger.Info(
$"Cannot save TV {kvp.Key} ({kvp.Value.Name}) to {cacheFile.Name} as it has not been updated at all.");
}
kvp.Value.WriteXml(writer);
}
else
{
Logger.Info(
$"Cannot save TV {kvp.Key} ({kvp.Value.Name}) to {cacheFile.Name} as it has not been updated at all.");
}
}

foreach (KeyValuePair<int, CachedMovieInfo> kvp in movies)
foreach (KeyValuePair<int, CachedMovieInfo> kvp in movies)
{
if (!kvp.Value.IsSearchResultOnly)
{
if (!kvp.Value.IsSearchResultOnly)
{
kvp.Value.WriteXml(writer);
}
else
{
Logger.Info(
$"Cannot save Movie {kvp.Key} ({kvp.Value.Name}) to {cacheFile.Name} as it is a search result that has not been used.");
}
kvp.Value.WriteXml(writer);
}
else
{
Logger.Info(
$"Cannot save Movie {kvp.Key} ({kvp.Value.Name}) to {cacheFile.Name} as it is a search result that has not been used.");
}
}

writer.WriteEndElement(); // data
writer.WriteEndElement(); // data

writer.WriteEndDocument();
}
writer.WriteEndDocument();
}

public static bool LoadTvCache<T>(FileInfo loadFrom, T cache) where T : MediaCache, iTVSource
Expand Down Expand Up @@ -328,7 +326,7 @@ private static void ProcessXmlBannerCache(XElement r, iTVSource localCache)
{
int seriesId = bannersXml.ExtractInt("SeriesId") ?? -1;

localCache.GetSeries(seriesId)?.AddBanners(seriesId, bannersXml.Descendants("Banners").Descendants("Banner")
localCache.GetSeries(seriesId)?.AddBanners(bannersXml.Descendants("Banners").Descendants("Banner")
.Select(banner => ShowImage.GenerateFromLegacyBannerXml(seriesId, banner, localCache.SourceProvider())));
}
}
Expand Down
2 changes: 1 addition & 1 deletion TVRename/Model/CachedSeriesInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private void LoadXml(XElement seriesXml)
}
}

internal void AddBanners(int seriesId, IEnumerable<ShowImage> enumerable)
internal void AddBanners(IEnumerable<ShowImage> enumerable)
{
foreach (ShowImage s in enumerable)
{
Expand Down
3 changes: 1 addition & 2 deletions TVRename/ScanActivity/Finders/FinderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,7 @@ public static (string title, int? year) SplitIntoTitleYear(string hint)
//Seems like we have a year in the date

//Work out the year
int.TryParse(m.Groups[1].Value, out int year);
possibleYear = year;
possibleYear = m.Groups[1].Value.ToInt();

//remove year from string
hint = Regex.Replace(hint.Trim(), PATTERN, " ");
Expand Down
2 changes: 2 additions & 0 deletions TVRename/Sources/MediaCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace TVRename;

Expand Down Expand Up @@ -151,4 +152,5 @@ public bool HasMovie(int id)
public abstract int PrimaryKey(ISeriesSpecifier ss);
public abstract string CacheSourceName();
public abstract void ReConnect(bool b);
public abstract bool GetUpdates(IEnumerable<ISeriesSpecifier> ss, bool showErrorMsgBox, CancellationToken cts);
}
34 changes: 17 additions & 17 deletions TVRename/Sources/TMDB/LocalCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ public override bool EnsureUpdated(ISeriesSpecifier s, bool bannersToo, bool sho
}

return s.Media == MediaConfiguration.MediaType.movie
? EnsureMovieUpdated(s, showErrorMsgBox)
: EnsureSeriesUpdated(s, showErrorMsgBox);
? EnsureMovieUpdated(s)
: EnsureSeriesUpdated(s);
}

private bool EnsureSeriesUpdated(ISeriesSpecifier s, bool showErrorMsgBox)
private bool EnsureSeriesUpdated(ISeriesSpecifier s)
{
lock (SERIES_LOCK)
{
Expand All @@ -146,7 +146,7 @@ private bool EnsureSeriesUpdated(ISeriesSpecifier s, bool showErrorMsgBox)
Say($"Series {s.Name} from TMDB");
try
{
CachedSeriesInfo downloadedSi = DownloadSeriesNow(s, showErrorMsgBox);
CachedSeriesInfo downloadedSi = DownloadSeriesNow(s);

if (downloadedSi.TmdbCode != s.TmdbId && s.TmdbId == -1)
{
Expand Down Expand Up @@ -185,7 +185,7 @@ private bool EnsureSeriesUpdated(ISeriesSpecifier s, bool showErrorMsgBox)
return true;
}

private bool EnsureMovieUpdated(ISeriesSpecifier id, bool showErrorMsgBox)
private bool EnsureMovieUpdated(ISeriesSpecifier id)
{
lock (MOVIE_LOCK)
{
Expand All @@ -198,7 +198,7 @@ private bool EnsureMovieUpdated(ISeriesSpecifier id, bool showErrorMsgBox)
Say($"Movie: {id.Name} from TMDB");
try
{
CachedMovieInfo downloadedSi = DownloadMovieNow(id, showErrorMsgBox);
CachedMovieInfo downloadedSi = DownloadMovieNow(id);

if (downloadedSi.TmdbCode != id.TmdbId && id.TmdbId == -1)
{
Expand Down Expand Up @@ -228,7 +228,7 @@ private bool EnsureMovieUpdated(ISeriesSpecifier id, bool showErrorMsgBox)
}
}

public bool GetUpdates(IEnumerable<ISeriesSpecifier> ss, bool showErrorMsgBox, CancellationToken cts)
public override bool GetUpdates(IEnumerable<ISeriesSpecifier> ss, bool showErrorMsgBox, CancellationToken cts)
{
Say("Validating TMDB cache");
this.MarkPlaceHoldersDirty(ss);
Expand Down Expand Up @@ -379,11 +379,11 @@ public void LatestUpdateTimeIs(string time)

public override TVDoc.ProviderType Provider() => TVDoc.ProviderType.TMDB;

public CachedMovieInfo GetMovieAndDownload(ISeriesSpecifier id, bool showErrorMsgBox) => HasMovie(id.TmdbId)
public CachedMovieInfo GetMovieAndDownload(ISeriesSpecifier id) => HasMovie(id.TmdbId)
? CachedMovieData[id.TmdbId]
: DownloadMovieNow(id, showErrorMsgBox);
: DownloadMovieNow(id);

internal CachedMovieInfo DownloadMovieNow(ISeriesSpecifier id, bool showErrorMsgBox,bool saveToCache = true)
internal CachedMovieInfo DownloadMovieNow(ISeriesSpecifier id,bool saveToCache = true)
{
string imageLanguage = $"{id.LanguageToUse().Abbreviation},null";
try
Expand Down Expand Up @@ -547,7 +547,7 @@ private static void AddMovieImages(Movie downloadedMovie, CachedMovieInfo m)
return null;
}

internal CachedSeriesInfo DownloadSeriesNow(ISeriesSpecifier ss, bool showErrorMsgBox, bool saveToCache = true)
internal CachedSeriesInfo DownloadSeriesNow(ISeriesSpecifier ss, bool saveToCache = true)
{
int id = ss.TmdbId > 0 ? ss.TmdbId : GetSeriesIdFromOtherCodes(ss) ?? 0;

Expand Down Expand Up @@ -832,11 +832,11 @@ public override void Search(string text, bool showErrorMsgBox, MediaConfiguratio
switch (type)
{
case MediaConfiguration.MediaType.tv:
DownloadSeriesNow(ss, showErrorMsgBox);
DownloadSeriesNow(ss);
break;

case MediaConfiguration.MediaType.movie:
DownloadMovieNow(ss, showErrorMsgBox);
DownloadMovieNow(ss);
break;
}
}
Expand Down Expand Up @@ -952,14 +952,14 @@ private CachedMovieInfo File(SearchMovie result)
return null;
}

public CachedMovieInfo? LookupMovieByImdb(string imdbToTest, Locale locale, bool showErrorMsgBox)
public CachedMovieInfo? LookupMovieByImdb(string imdbToTest, Locale locale)
{
FindContainer? results = Client.FindAsync(FindExternalSource.Imdb, imdbToTest).Result;
LOGGER.Info($"Got {results.MovieResults.Count:N0} results searching for {imdbToTest}");
foreach (SearchMovie result in results.MovieResults)
{
SearchSpecifier ss = new(result.Id, locale, TVDoc.ProviderType.TMDB, MediaConfiguration.MediaType.movie);
DownloadMovieNow(ss, showErrorMsgBox);
DownloadMovieNow(ss);
}

if (results.MovieResults.Count == 0)
Expand Down Expand Up @@ -1015,14 +1015,14 @@ public Dictionary<int, CachedMovieInfo> GetMovieIdsFromCollection(int collection
return returnValue;
}

public CachedMovieInfo? LookupMovieByTvdb(int tvdbId, bool showErrorMsgBox,Locale locale)
public CachedMovieInfo? LookupMovieByTvdb(int tvdbId,Locale locale)
{
FindContainer? results = Client.FindAsync(FindExternalSource.TvDb, tvdbId.ToString()).Result;
LOGGER.Info($"Got {results.MovieResults.Count:N0} results searching for {tvdbId}");
foreach (SearchMovie result in results.MovieResults)
{
SearchSpecifier ss = new(result.Id, locale, TVDoc.ProviderType.TMDB, MediaConfiguration.MediaType.movie);
DownloadMovieNow(ss, showErrorMsgBox);
DownloadMovieNow(ss);
}

if (results.MovieResults.Count == 0)
Expand Down
8 changes: 4 additions & 4 deletions TVRename/Sources/TMDB/TmdbAccuracyCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void ServerAccuracyCheck(CachedMovieInfo si)
Logger.Info($"Accuracy Check for {si.Name} on TMDB");
try
{
CachedMovieInfo newSi = lc.DownloadMovieNow(si, false,false);
CachedMovieInfo newSi = lc.DownloadMovieNow(si,false);

if (!Match(newSi, si))
{
Expand All @@ -49,7 +49,7 @@ public void ServerAccuracyCheck(CachedSeriesInfo si)
Logger.Info($"Accuracy Check for {si.Name} on TMDB");
try
{
CachedSeriesInfo newSi = lc.DownloadSeriesNow(si, false,false);
CachedSeriesInfo newSi = lc.DownloadSeriesNow(si,false);

if (!Match(newSi, si)) //NB - we use a match method as we can't rely on update time
{
Expand All @@ -68,7 +68,7 @@ public void ServerAccuracyCheck(CachedSeriesInfo si)
}
}

private bool Match(CachedMovieInfo newSi, CachedMovieInfo si)
private static bool Match(CachedMovieInfo newSi, CachedMovieInfo si)
{
if (newSi.CollectionName != si.CollectionName)
{
Expand Down Expand Up @@ -103,7 +103,7 @@ private bool Match(CachedMovieInfo newSi, CachedMovieInfo si)
return true;
}

private bool Match(CachedSeriesInfo newSi, CachedSeriesInfo si)
private static bool Match(CachedSeriesInfo newSi, CachedSeriesInfo si)
{
if (newSi.Name != si.Name)
{
Expand Down
2 changes: 1 addition & 1 deletion TVRename/Sources/TVMAZE/LocalCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public override bool EnsureUpdated(ISeriesSpecifier s, bool bannersToo, bool sho
return true;
}

public bool GetUpdates(IEnumerable<ISeriesSpecifier> ss, bool showErrorMsgBox, CancellationToken cts)
public override bool GetUpdates(IEnumerable<ISeriesSpecifier> ss, bool showErrorMsgBox, CancellationToken cts)
{
Say("Validating TVmaze cache");
foreach (ISeriesSpecifier downloadShow in ss.Where(downloadShow => !HasSeries(downloadShow.TvMazeId)))
Expand Down
4 changes: 2 additions & 2 deletions TVRename/Sources/TheTVDB/LocalCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private void HandleConnectionProblem(bool showErrorMsgBox, Exception ex)
}
}

public bool GetUpdates(bool showErrorMsgBox, IEnumerable<ISeriesSpecifier> ss, CancellationToken cts)
public override bool GetUpdates(IEnumerable<ISeriesSpecifier> ss, bool showErrorMsgBox, CancellationToken cts)
{
Say("Validating TheTVDB cache");
IEnumerable<ISeriesSpecifier> seriesSpecifiers = ss.ToList();
Expand Down Expand Up @@ -2315,7 +2315,7 @@ private void ProcessBannerResponses(int code, CachedSeriesInfo si, Locale locale
private static ShowImage CreateShowImage(int tvdbId, JObject bannerData)
{
double.TryParse((string?)bannerData["ratingsInfo"]?["average"], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.CreateSpecificCulture("en-US"), out double rating);
int.TryParse((string?)bannerData["subKey"], out int seasonId);
int? seasonId = ((string?)bannerData["subKey"]).ToInt();
// {
// "fileName": "string",
// "id": 0,
Expand Down
2 changes: 1 addition & 1 deletion TVRename/TVRename/CacheUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private void Downloader(object? token)

if (downloadIds.Any(s => s.Provider == TVDoc.ProviderType.TheTVDB))
{
if (!TheTVDB.LocalCache.Instance.GetUpdates(showErrorMsgBox, downloadIds.Where(specifier => specifier.Provider == TVDoc.ProviderType.TheTVDB),
if (!TheTVDB.LocalCache.Instance.GetUpdates(downloadIds.Where(specifier => specifier.Provider == TVDoc.ProviderType.TheTVDB), showErrorMsgBox,
cts))
{
DownloadDone = true;
Expand Down
10 changes: 5 additions & 5 deletions TVRename/TVRename/PossibleNewMovie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void GuessMovie(bool showErrorMsgBox)
int? tmdbId = FindShowCode("tmdbid", "tmdb").ToInt();
Locale preferredLocale = new();

int? tmdbCode = ValidateOnTMDB(tmdbId, preferredLocale, showErrorMsgBox);
int? tmdbCode = ValidateOnTMDB(tmdbId, preferredLocale);
if (tmdbCode.HasValue)
{
SetId(tmdbCode.Value, TVDoc.ProviderType.TMDB);
Expand All @@ -81,7 +81,7 @@ public void GuessMovie(bool showErrorMsgBox)

if (imdbToTest.HasValue())
{
CachedMovieInfo? s = TMDB.LocalCache.Instance.LookupMovieByImdb(imdbToTest, preferredLocale, showErrorMsgBox);
CachedMovieInfo? s = TMDB.LocalCache.Instance.LookupMovieByImdb(imdbToTest, preferredLocale);
if (s != null)
{
SetId(s.TmdbCode, TVDoc.ProviderType.TMDB);
Expand Down Expand Up @@ -110,7 +110,7 @@ public void GuessMovie(bool showErrorMsgBox)
int? tvdbId = FindShowCode("tvdbid", "tvdb").ToInt();
if (tvdbId.HasValue)
{
CachedMovieInfo? s2 = TMDB.LocalCache.Instance.LookupMovieByTvdb(tvdbId.Value, showErrorMsgBox, preferredLocale);
CachedMovieInfo? s2 = TMDB.LocalCache.Instance.LookupMovieByTvdb(tvdbId.Value, preferredLocale);
if (s2 != null)
{
SetId(s2.TmdbCode, TVDoc.ProviderType.TMDB);
Expand Down Expand Up @@ -167,15 +167,15 @@ public void SetId(int code, TVDoc.ProviderType provider)
return null;
}

private static int? ValidateOnTMDB(int? tmdbId, Locale locale, bool showErrorMsgBox)
private static int? ValidateOnTMDB(int? tmdbId, Locale locale)
{
if (tmdbId.HasValue)
{
try
{
ISeriesSpecifier ss = new SearchSpecifier(tmdbId.Value, locale, TVDoc.ProviderType.TMDB, MediaConfiguration.MediaType.movie);

CachedMovieInfo series = TMDB.LocalCache.Instance.GetMovieAndDownload(ss, showErrorMsgBox);
CachedMovieInfo series = TMDB.LocalCache.Instance.GetMovieAndDownload(ss);
return series.TmdbCode;
}
catch (MediaNotFoundException)
Expand Down
Loading

0 comments on commit bad674a

Please sign in to comment.