public CsvRow(StreamingService service, string animeName, string url, MalId malAnimeId) { Service = service; AnimeName = animeName; Url = url; MalAnimeId = malAnimeId; }
static List <CsvRow> LoadCsv(string inputFile) { List <CsvRow> inputCsvRows = new List <CsvRow>(); var csvConfig = new CsvHelper.Configuration.Configuration() { AllowComments = false, HasHeaderRecord = true, IgnoreBlankLines = true, IncludePrivateMembers = true, TrimOptions = CsvHelper.Configuration.TrimOptions.None, }; using (FileStream inputStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read)) using (TextReader inputReader = new StreamReader(inputStream, Encoding.UTF8)) using (CsvReader csvReader = new CsvReader(inputReader, csvConfig)) { csvReader.Read(); csvReader.ReadHeader(); while (csvReader.Read()) { StreamingService service = csvReader.GetField <StreamingService>("Service"); string animeName = csvReader.GetField <string>("Anime"); string url = csvReader.GetField <string>("URL"); string malIdString = csvReader.GetField <string>("MAL ID (or n/a)"); MalId malId; // could be blank if (string.IsNullOrWhiteSpace(malIdString)) { malId = new MalId(malAnimeId: null, specified: false); } else if (malIdString.Equals("n/a", StringComparison.OrdinalIgnoreCase)) { // n/a means the stream does not correspond to a MAL anime. Maybe it's a stream of anime reviews // or maybe it's something incredibly obscure. malId = new MalId(malAnimeId: null, specified: true); } else { int malIdInt = int.Parse(malIdString); malId = new MalId(malAnimeId: malIdInt, specified: true); } CsvRow row = new CsvRow(service, animeName, url, malId); inputCsvRows.Add(row); } } return(inputCsvRows); }
static List<CsvRow> LoadCsv(string inputFile) { List<CsvRow> inputCsvRows = new List<CsvRow>(); using (TextFieldParser csvReader = new TextFieldParser(inputFile)) { csvReader.TextFieldType = FieldType.Delimited; csvReader.HasFieldsEnclosedInQuotes = true; csvReader.Delimiters = new string[] { "," }; csvReader.TrimWhiteSpace = false; // Skip header line csvReader.ReadFields(); while (!csvReader.EndOfData) { string[] fields = csvReader.ReadFields(); // Skip blank lines if (fields.Length == 0) { continue; } string serviceString = fields[0]; if (string.IsNullOrWhiteSpace(serviceString)) { continue; // Skip blank lines } string animeName = fields[1]; string url = fields[2]; string malIdString = fields[3]; StreamingService service = (StreamingService)Enum.Parse(typeof(StreamingService), serviceString); MalId malId; // could be blank if (string.IsNullOrWhiteSpace(malIdString)) { malId = new MalId(malAnimeId: null, specified: false); } else if (malIdString.Equals("n/a", StringComparison.OrdinalIgnoreCase)) { // n/a means the stream does not correspond to a MAL anime. Maybe it's a stream of anime reviews // or maybe it's something incredibly obscure. malId = new MalId(malAnimeId: null, specified: true); } else { int malIdInt = int.Parse(malIdString); malId = new MalId(malAnimeId: malIdInt, specified: true); } inputCsvRows.Add(new CsvRow(service: service, animeName: animeName, url: url, malAnimeId: malId)); } } return inputCsvRows; }