From e3ef39f10742cf87a3fd51fb804f9f28b5e9f61d Mon Sep 17 00:00:00 2001 From: damienhaynes Date: Sun, 1 Sep 2019 20:46:10 +1000 Subject: [PATCH] Fixed issue parsing IMDb CSV if user's regional settings did not use "." for decimal separator. Changed default culture to 'en-US' when reading an IMDb CSV. Enhanced IMDb CSV file to ignore records which are in error rather than ignore whole file, log will show what row and field in error for analysis. --- Sites/IMDb.cs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/Sites/IMDb.cs b/Sites/IMDb.cs index a29c777..ad2d3f0 100644 --- a/Sites/IMDb.cs +++ b/Sites/IMDb.cs @@ -48,6 +48,8 @@ public IMDb(string aRatingsFile, string aWatchlistFile, List aImdbCustom mRatingsFileCsv = aRatingsFile; mWatchlistFileCsv = aWatchlistFile; mCustomListsCsvs = aImdbCustomLists; + + SetCSVHelperOptions(); } #endregion @@ -72,7 +74,7 @@ public void ImportRatings() { mCsvConfiguration.RegisterClassMap(); - lRatedCsvItems = ParseCsvFile(mRatingsFileCsv, mCsvConfiguration); + lRatedCsvItems = ParseCsvFile(mRatingsFileCsv); if (lRatedCsvItems == null) { UIUtils.UpdateStatus("Failed to parse IMDb ratings file!", true); @@ -90,7 +92,7 @@ public void ImportRatings() { mCsvConfiguration.RegisterClassMap(); - lWatchlistCsvItems = ParseCsvFile(mWatchlistFileCsv, mCsvConfiguration); + lWatchlistCsvItems = ParseCsvFile(mWatchlistFileCsv); if (lWatchlistCsvItems == null) { UIUtils.UpdateStatus("Failed to parse IMDb watchlist file!", true); @@ -112,7 +114,7 @@ public void ImportRatings() { UIUtils.UpdateStatus($"Reading IMDb custom list '{list}'"); - var lListCsvItems = ParseCsvFile(list, mCsvConfiguration); + var lListCsvItems = ParseCsvFile(list); if (lListCsvItems == null) { UIUtils.UpdateStatus("Failed to parse IMDb custom list file!", true); @@ -739,19 +741,27 @@ public void Cancel() #region Private Methods - private List ParseCsvFile(string aFilename, CsvConfiguration aConfig) + private void SetCSVHelperOptions() { - try + mCsvConfiguration.IsHeaderCaseSensitive = false; + + // IMDb use "." for decimal seperator so set culture to cater for this + mCsvConfiguration.CultureInfo = new System.Globalization.CultureInfo("en-US"); + + // if we're unable parse a row, log the details for analysis + mCsvConfiguration.IgnoreReadingExceptions = true; + mCsvConfiguration.ReadingExceptionCallback = (ex, row) => { - var textReader = File.OpenText(aFilename); - var csv = new CsvReader(textReader, aConfig); + FileLog.Error($"Error reading row '{ex.Data["CsvHelper"]}'"); + }; + } - return csv.GetRecords().ToList(); - } - catch(Exception e) + private List ParseCsvFile(string aFilename) + { + using (var reader = new StreamReader(aFilename)) + using (var csv = new CsvReader(reader, mCsvConfiguration)) { - FileLog.Error($"Error parsing '{aFilename}'. Exception = '{e.Message}', Stack = '{e.StackTrace}'"); - return null; + return csv.GetRecords().ToList(); } }