Skip to content

Commit

Permalink
Fixed issue parsing IMDb CSV if user's regional settings did not use …
Browse files Browse the repository at this point in the history
…"." 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.
  • Loading branch information
damienhaynes committed Sep 1, 2019
1 parent 4474d48 commit e3ef39f
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions Sites/IMDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public IMDb(string aRatingsFile, string aWatchlistFile, List<string> aImdbCustom
mRatingsFileCsv = aRatingsFile;
mWatchlistFileCsv = aWatchlistFile;
mCustomListsCsvs = aImdbCustomLists;

SetCSVHelperOptions();
}

#endregion
Expand All @@ -72,7 +74,7 @@ public void ImportRatings()
{
mCsvConfiguration.RegisterClassMap<IMDbRatingCsvMap>();

lRatedCsvItems = ParseCsvFile<IMDbRateItem>(mRatingsFileCsv, mCsvConfiguration);
lRatedCsvItems = ParseCsvFile<IMDbRateItem>(mRatingsFileCsv);
if (lRatedCsvItems == null)
{
UIUtils.UpdateStatus("Failed to parse IMDb ratings file!", true);
Expand All @@ -90,7 +92,7 @@ public void ImportRatings()
{
mCsvConfiguration.RegisterClassMap<IMDbListCsvMap>();

lWatchlistCsvItems = ParseCsvFile<IMDbListItem>(mWatchlistFileCsv, mCsvConfiguration);
lWatchlistCsvItems = ParseCsvFile<IMDbListItem>(mWatchlistFileCsv);
if (lWatchlistCsvItems == null)
{
UIUtils.UpdateStatus("Failed to parse IMDb watchlist file!", true);
Expand All @@ -112,7 +114,7 @@ public void ImportRatings()
{
UIUtils.UpdateStatus($"Reading IMDb custom list '{list}'");

var lListCsvItems = ParseCsvFile<IMDbListItem>(list, mCsvConfiguration);
var lListCsvItems = ParseCsvFile<IMDbListItem>(list);
if (lListCsvItems == null)
{
UIUtils.UpdateStatus("Failed to parse IMDb custom list file!", true);
Expand Down Expand Up @@ -739,19 +741,27 @@ public void Cancel()

#region Private Methods

private List<T> ParseCsvFile<T>(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<T>().ToList();
}
catch(Exception e)
private List<T> ParseCsvFile<T>(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<T>().ToList();
}
}

Expand Down

0 comments on commit e3ef39f

Please sign in to comment.