/// <summary> /// Constructor. /// </summary> /// <param name="parsedScrobble">The scrobble parsed from the csv file.</param> /// <param name="scrobbleMode">The current scrobble mode.</param> public ParsedCSVScrobbleViewModel(CSVScrobble parsedScrobble, CSVScrobbleMode scrobbleMode) { ParsedScrobble = parsedScrobble; _scrobbleMode = scrobbleMode; }
/// <summary> /// Loads and parses a csv file. /// </summary> /// <param name="path">Path of the csv file to load.</param> private async void LoadCSVFile(string path) { try { EnableControls = false; OnStatusUpdated("Reading CSV file..."); CSVFilePath = path; Scrobbles.Clear(); TextFieldParser parser = new TextFieldParser(CSVFilePath); parser.HasFieldsEnclosedInQuotes = true; parser.SetDelimiters(","); string[] fields = new string[0]; List<string> errors = new List<string>(); await Task.Run(() => { while (!parser.EndOfData) { try { // csv should be "Artist, Album, Track, Date" fields = parser.ReadFields(); if (fields.Length != 4) throw new Exception("Parsed row has wrong number of fields!"); DateTime date = DateTime.Now; string dateString = fields[3]; // check for 'now playing' if (fields[3] == "" && ScrobbleMode == CSVScrobbleMode.Normal) continue; if (DateTime.TryParse(dateString, out date)) { } else { bool parsed = false; // try different formats until succeeded foreach (string format in Formats) { parsed = DateTime.TryParseExact(dateString, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out date); if (parsed) break; } if (!parsed && ScrobbleMode == CSVScrobbleMode.Normal) throw new Exception("Timestamp could not be parsed!"); } CSVScrobble parsedScrobble = new CSVScrobble(fields[0], fields[1], fields[2], date.AddSeconds(1)); ParsedCSVScrobbleViewModel vm = new ParsedCSVScrobbleViewModel(parsedScrobble, ScrobbleMode); vm.ToScrobbleChanged += ToScrobbleChanged; _dispatcher.Invoke(() => Scrobbles.Add(vm)); } catch (Exception ex) { string errorString = "CSV line number: " + parser.LineNumber + ","; foreach (string s in fields) { errorString += s + ","; } errorString += ex.Message; errors.Add(errorString); } } }); if (errors.Count == 0) OnStatusUpdated("Successfully parsed CSV file. Parsed " + Scrobbles.Count + " rows"); else { OnStatusUpdated("Partially parsed CSV file. " + errors.Count + " rows could not be parsed"); if (MessageBox.Show("Some rows could not be parsed. Do you want to save a text file with the rows that could not be parsed?", "Error parsing rows", MessageBoxButtons.YesNo) == DialogResult.Yes) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Text Files|*.txt"; if (sfd.ShowDialog() == DialogResult.OK) File.WriteAllLines(sfd.FileName, errors.ToArray()); } } } catch (Exception ex) { Scrobbles.Clear(); OnStatusUpdated("Error parsing CSV file: " + ex.Message); } finally { EnableControls = true; } }