Skip to content

Commit

Permalink
Fixed issue #20 #21
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalteapot committed Jan 4, 2025
1 parent af99473 commit 849d26e
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 111 deletions.
2 changes: 1 addition & 1 deletion AudioFileSorter/AudioFileSorter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CsvHelper" Version="30.1.0" />
<PackageReference Include="CsvHelper" Version="33.0.1" />
</ItemGroup>

</Project>
18 changes: 15 additions & 3 deletions AudioFileSorter/CsvParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
using AudioFileSorter.Model;
using CsvHelper;
using CsvHelper.Configuration;
using System.Linq;


namespace AudioFileSorter;

public class CsvParser
{
public async Task<List<OpenAudible>> ParseDataCsv(string fullPath, CancellationToken token)
public async Task<List<OpenAudible>> ParseDataCsv(string? fullPath, CancellationToken token)
{
using var reader = new StreamReader(fullPath);
using var reader = new StreamReader(fullPath ?? throw new ArgumentNullException(nameof(fullPath)));
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
try
{
var result = new List<OpenAudible>();
csv.Context.RegisterClassMap<AudiobookMap>();
return await csv.GetRecordsAsync<OpenAudible>(token).ToListAsync(token);
await foreach (var openAudible in csv.GetRecordsAsync<OpenAudible>(token))
{
result.Add(openAudible);
}
return result;
}
catch (CsvHelperException ex)
{
Console.WriteLine($"CSV Parsing Error: {ex.Message}");
throw;
}
finally
{
Expand Down
26 changes: 16 additions & 10 deletions AudioFileSorter/Model/AudiobookMap.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;

namespace AudioFileSorter.Model;

public sealed class AudiobookMap : ClassMap<OpenAudible>
{
public AudiobookMap()
{
// Basic mappings
Map(m => m.Key).Name("Key");
// Mandatory Fields
Map(m => m.Key).Name("Key").Optional();
Map(m => m.Title).Name("Title");
Map(m => m.Author).Name("Author");
Map(m => m.Filename).Name("File name");
Map(m => m.FilePaths).Name("File Paths");
Map(m => m.AudibleAAX).Name("Audible (AAX)").Optional();

// Optional Fields
Map(m => m.NarratedBy).Name("Narrated By").Optional();
Map(m => m.PurchaseDate).Name("Purchase Date").TypeConverterOption.Format("yyyy-MM-dd");
Map(m => m.PurchaseDate).Name("Purchase Date").TypeConverterOption.Format("yyyy-MM-dd", "MM/dd/yyyy", "M/d/yyyy");
Map(m => m.Duration).Name("Duration").Optional();
Map(m => m.ReleaseDate).Name("Release Date").TypeConverterOption.Format("yyyy-MM-dd");
Map(m => m.ReleaseDate).Name("Release Date").TypeConverterOption.Format("yyyy-MM-dd", "MM/dd/yyyy", "M/d/yyyy");
Map(m => m.AveRating).Name("Ave. Rating").Optional();
Map(m => m.Genre).Name("Genre").Optional();
Map(m => m.SeriesName).Name("Series Name").Optional();
Expand All @@ -23,25 +29,25 @@ public AudiobookMap()
Map(m => m.BookURL).Name("Book URL").Optional();
Map(m => m.Summary).Name("Summary").Optional();
Map(m => m.Description).Name("Description").Optional();
Map(m => m.RatingCount).Name("Rating Count").Optional();
Map(m => m.Publisher).Name("Publisher").Optional();
Map(m => m.ShortTitle).Name("Short Title").Optional();
Map(m => m.Copyright).Name("Copyright").Optional();
Map(m => m.AuthorURL).Name("Author URL").Optional();
Map(m => m.Filename).Name("File name");
Map(m => m.SeriesURL).Name("Series URL").Optional();
Map(m => m.Abridged).Name("Abridged").Optional();
Map(m => m.Language).Name("Language").Optional();
Map(m => m.PDFURL).Name("PDF URL").Optional();
Map(m => m.ImageURL).Name("Image URL").Optional();
Map(m => m.Region).Name("Region").Optional();
Map(m => m.FilePaths).Name("File Paths");
Map(m => m.AYCE).Name("AYCE").Optional();
Map(m => m.ReadStatus).Name("Read Status");
Map(m => m.ReadStatus).Name("Read Status").Optional();
Map(m => m.UserID).Name("User ID").Optional();
Map(m => m.AudibleAAX).Name("Audible (AAX)");
Map(m => m.Image).Name("Image").Optional();
Map(m => m.M4B).Name("M4B").Optional();
Map(m => m.MP3).Name("MP3").Optional();
Map(m => m.AYCE).Name("AYCE").TypeConverter<BooleanConverter>().Optional();
Map(m => m.RatingCount).Name("Rating Count").TypeConverter<Int32Converter>().Optional();

// **Newly Added Field**
Map(m => m.PDF).Name("PDF").Optional();
}
}
15 changes: 8 additions & 7 deletions AudioFileSorter/Model/OpenAudible.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,40 @@
public class OpenAudible
{
public string? Key { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string? Title { get; set; }
public string? Author { get; set; }
public string? NarratedBy { get; set; }
public DateTime PurchaseDate { get; set; }
public string? Duration { get; set; }
public DateTime ReleaseDate { get; set; }
public double AveRating { get; set; }
public string? Genre { get; set; }
public string SeriesName { get; set; }
public string SeriesSequence { get; set; }
public string? SeriesName { get; set; }
public string? SeriesSequence { get; set; }
public string? ProductID { get; set; }
public string? ASIN { get; set; }
public string? BookURL { get; set; }
public string? Summary { get; set; }
public string? Description { get; set; }
public int RatingCount { get; set; }
public string? Publisher { get; set; }
public string ShortTitle { get; set; }
public string? ShortTitle { get; set; }
public string? Copyright { get; set; }
public string? AuthorURL { get; set; }
public string Filename { get; set; }
public string? Filename { get; set; }
public string? SeriesURL { get; set; }
public string? Abridged { get; set; }
public string? Language { get; set; }
public string? PDFURL { get; set; }
public string? ImageURL { get; set; }
public string? Region { get; set; }
public string FilePaths { get; set; }
public string? FilePaths { get; set; }
public bool AYCE { get; set; }
public string? ReadStatus { get; set; }
public string? UserID { get; set; }
public string? AudibleAAX { get; set; }
public string? Image { get; set; }
public string? M4B { get; set; }
public string? MP3 { get; set; }
public string? PDF { get; set; }
}
Loading

0 comments on commit 849d26e

Please sign in to comment.