/// <summary> /// Parses the metadata of the given <paramref name="files"/>. /// </summary> /// <param name="files">Files whose metadata to parse.</param> /// <returns>Task.</returns> private async Task ParseFiles(string[] files) { OnStatusUpdated("Trying to parse selected files..."); List <string> errors = new List <string>(); var newFiles = new List <LoadedFileViewModel>(); await Task.Run(() => { foreach (string file in files) { try { if (SUPPORTEDFILES.Contains(Path.GetExtension(file).ToLower())) { ILocalFile audioFile = _localFileFactory.CreateFile(file); if (string.IsNullOrEmpty(audioFile.Artist)) { throw new Exception("No artist name found"); } if (string.IsNullOrEmpty(audioFile.Track)) { throw new Exception("No track name found"); } var vm = new LoadedFileViewModel(audioFile); vm.ToScrobbleChanged += Vm_ToScrobbleChanged; newFiles.Add(vm); } } catch (Exception ex) { errors.Add(string.Format("{0} {1}", file, ex.Message)); } } }); Scrobbles = new ObservableCollection <LoadedFileViewModel>(Scrobbles.Concat(newFiles)); if (errors.Count > 0) { OnStatusUpdated(string.Format("Finished parsing selected files. {0} files could not be parsed", errors.Count)); if (_windowManager.MessageBoxService.ShowDialog("Some files could not be parsed. Do you want to save a text file with the files that could not be parsed?", "Error parsing files", IMessageBoxServiceButtons.YesNo) == IMessageBoxServiceResult.Yes) { IFileDialog sfd = _windowManager.CreateSaveFileDialog(); sfd.Filter = "Text Files|*.txt"; if (sfd.ShowDialog()) { _fileOperator.WriteAllLines(sfd.FileName, errors.ToArray()); } } } else { OnStatusUpdated("Successfully parsed selected files"); } }
/// <summary> /// Loads and parses a csv file. /// </summary> /// <returns>Task.</returns> public async Task ParseCSVFile() { try { EnableControls = false; OnStatusUpdated("Reading CSV file..."); Scrobbles.Clear(); using (ITextFieldParser parser = _parserFactory.CreateParser(CSVFilePath)) { parser.SetDelimiters(Settings.Default.CSVDelimiters.Select(x => new string(x, 1)).ToArray()); string[] fields = null; List <string> errors = new List <string>(); ObservableCollection <ParsedCSVScrobbleViewModel> parsedScrobbles = new ObservableCollection <ParsedCSVScrobbleViewModel>(); await Task.Run(() => { while (!parser.EndOfData) { try { fields = parser.ReadFields(); string dateString = fields[Settings.Default.TimestampFieldIndex]; // check for 'now playing' if (dateString == "" && ScrobbleMode == CSVScrobbleMode.Normal) { continue; } DateTime date = DateTime.Now; if (!DateTime.TryParse(dateString, out date)) { 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!"); } } // try to get optional parameters first string album = fields.ElementAtOrDefault(Settings.Default.AlbumFieldIndex); string albumArtist = fields.ElementAtOrDefault(Settings.Default.AlbumArtistFieldIndex); string duration = fields.ElementAtOrDefault(Settings.Default.DurationFieldIndex); TimeSpan time = TimeSpan.FromSeconds(Duration); TimeSpan.TryParse(duration, out time); DatedScrobble parsedScrobble = new DatedScrobble(date.AddSeconds(1), fields[Settings.Default.TrackFieldIndex], fields[Settings.Default.ArtistFieldIndex], album, albumArtist, time); parsedScrobbles.Add(new ParsedCSVScrobbleViewModel(parsedScrobble, ScrobbleMode)); } 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(string.Format("Successfully parsed CSV file. Parsed {0} rows", parsedScrobbles.Count)); } else { OnStatusUpdated(string.Format("Partially parsed CSV file. {0} rows could not be parsed", errors.Count)); if (_windowManager.MessageBoxService.ShowDialog("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", IMessageBoxServiceButtons.YesNo) == IMessageBoxServiceResult.Yes) { IFileDialog sfd = _windowManager.CreateSaveFileDialog(); sfd.Filter = "Text Files|*.txt"; if (sfd.ShowDialog()) { _fileOperator.WriteAllLines(sfd.FileName, errors.ToArray()); } } } Scrobbles = parsedScrobbles; } } catch (Exception ex) { Scrobbles.Clear(); OnStatusUpdated(string.Format("Error parsing CSV file: {0}", ex.Message)); } finally { EnableControls = true; } }