Skip to content

Commit

Permalink
Add ConvertAllBookTypes setting (default false)
Browse files Browse the repository at this point in the history
  • Loading branch information
ToofDerling committed Dec 15, 2022
1 parent c912d12 commit 4584ae7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
13 changes: 11 additions & 2 deletions Source/AzwConverter/AzwConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,17 @@ public async Task ConvertOrScanAsync()
var syncer = new TitleSyncer();

// Number of books is stable after title syncing.
var added = await syncer.SyncBooksToTitlesAsync(books, titles, archive);
Console.WriteLine($"Added {added} missing title{added.SIf1()}");
var (added, skipped) = await syncer.SyncBooksToTitlesAsync(books, titles, archive);

Console.Write($"Added {added} missing title{added.SIf1()}");
if (!Settings.ConvertAllBookTypes)
{
Console.WriteLine($" (skipped {skipped})");
}
else
{
Console.WriteLine();
}

var archived = syncer.SyncAndArchiveTitles(titles, convertedTitles, archive, books);
Console.WriteLine($"Archived {archived} title{archived.SIf1()}");
Expand Down
5 changes: 5 additions & 0 deletions Source/AzwConverter/AzwSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
// Note: SaveCover must also be true for SaveCoverOnly to work.
"SaveCoverOnly": false,

// Normally CbzMage only scans and converts books where the BookType is "comic".
// If you run into books that are ignored and you're sure they're comics try
// changing ConvertAllBookTypes to true.
"ConvertAllBookTypes": false,

// The converted titles directory is a subdirectory of TitlesDir. The default
// name is "Converted Titles", you can change it to something else below. Don't
// forget to rename the actual directory if you do.
Expand Down
2 changes: 2 additions & 0 deletions Source/AzwConverter/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static void SetConvertedTitlesDir(string dir)

public static string CbzDir { get; set; }

public static bool ConvertAllBookTypes { get; set; }

public static bool SaveCover { get; set; }
public static bool SaveCoverOnly { get; set; }
public static string? SaveCoverDir { get; set; }
Expand Down
32 changes: 23 additions & 9 deletions Source/AzwConverter/TitleSyncer.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using AzwConverter.Engine;
using CbzMage.Shared.Extensions;
using MobiMetadata;
using System.Collections.Concurrent;

namespace AzwConverter
{
public class TitleSyncer
{
public async Task<int> SyncBooksToTitlesAsync(IDictionary<string, FileInfo[]> books,
IDictionary<string, FileInfo> titles, ArchiveDb archive)
public async Task<(int skippedBooks, int ignoredBooks)> SyncBooksToTitlesAsync(
IDictionary<string, FileInfo[]> books, IDictionary<string, FileInfo> titles, ArchiveDb archive)
{
var booksWithErrors = new ConcurrentBag<string>();
var newOrArchivedTitles = new ConcurrentDictionary<string, FileInfo>();
var skippedBooks = new ConcurrentBag<string>();

var addedTitles = new ConcurrentDictionary<string, FileInfo>();

await Parallel.ForEachAsync(books, Settings.ParallelOptions, async (book, ct) =>
{
Expand All @@ -36,6 +39,12 @@ await Parallel.ForEachAsync(books, Settings.ParallelOptions, async (book, ct) =>
var engine = new MetadataEngine();
(metadata, disposables) = await engine.ReadMetadataAsync(bookFiles);
if (!metadata.MobiHeader.ExthHeader.BookType.EqualsIgnoreCase("comic"))
{
skippedBooks.Add(bookId);
return;
}
MetadataManager.CacheMetadata(bookId, metadata, disposables);
}
catch (MobiMetadataException)
Expand Down Expand Up @@ -63,7 +72,7 @@ async Task SyncAsync(string titleFile)
await File.WriteAllTextAsync(file, bookId, CancellationToken.None);
// Add archived/scanned title to list of current titles
newOrArchivedTitles[bookId] = new FileInfo(file);
addedTitles[bookId] = new FileInfo(file);
}
}
});
Expand All @@ -73,12 +82,17 @@ async Task SyncAsync(string titleFile)
books.Remove(bookId);
}

foreach (var title in newOrArchivedTitles)
foreach (var bookId in skippedBooks)
{
books.Remove(bookId);
}

foreach (var title in addedTitles)
{
titles.Add(title);
titles.Add(title);
}

return newOrArchivedTitles.Count;
return (skippedBooks: addedTitles.Count, ignoredBooks: skippedBooks.Count);
}

private static string TrimPublisher(string publisher)
Expand All @@ -95,7 +109,7 @@ private static string TrimPublisher(string publisher)
return publisher;
}

public int SyncAndArchiveTitles(IDictionary<string, FileInfo> titles,
public int SyncAndArchiveTitles(IDictionary<string, FileInfo> titles,
IDictionary<string, FileInfo> convertedTitles,
ArchiveDb archive, IDictionary<string, FileInfo[]> books)
{
Expand All @@ -118,7 +132,7 @@ public int SyncAndArchiveTitles(IDictionary<string, FileInfo> titles,
// Sync title -> converted title
if (convertedTitles.TryGetValue(bookId, out var convertedTitleFile)
&& convertedTitleFile.Name != titleFile.Name)
{
{
var newConvertedTitleFile = Path.Combine(convertedTitleFile.DirectoryName!, titleFile.Name);
convertedTitleFile.MoveTo(newConvertedTitleFile);
}
Expand Down

0 comments on commit 4584ae7

Please sign in to comment.