Lots of Bugfixes (#2960)
Co-authored-by: Samuel Martins <s@smartins.ch> Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
This commit is contained in:
parent
97ffdd0975
commit
b50fa0fd1e
45 changed files with 563 additions and 282 deletions
|
@ -33,17 +33,19 @@ public class WordCountAnalyzerService : IWordCountAnalyzerService
|
|||
private readonly IEventHub _eventHub;
|
||||
private readonly ICacheHelper _cacheHelper;
|
||||
private readonly IReaderService _readerService;
|
||||
private readonly IMediaErrorService _mediaErrorService;
|
||||
|
||||
private const int AverageCharactersPerWord = 5;
|
||||
|
||||
public WordCountAnalyzerService(ILogger<WordCountAnalyzerService> logger, IUnitOfWork unitOfWork, IEventHub eventHub,
|
||||
ICacheHelper cacheHelper, IReaderService readerService)
|
||||
ICacheHelper cacheHelper, IReaderService readerService, IMediaErrorService mediaErrorService)
|
||||
{
|
||||
_logger = logger;
|
||||
_unitOfWork = unitOfWork;
|
||||
_eventHub = eventHub;
|
||||
_cacheHelper = cacheHelper;
|
||||
_readerService = readerService;
|
||||
_mediaErrorService = mediaErrorService;
|
||||
}
|
||||
|
||||
|
||||
|
@ -188,7 +190,7 @@ public class WordCountAnalyzerService : IWordCountAnalyzerService
|
|||
await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress,
|
||||
MessageFactory.WordCountAnalyzerProgressEvent(series.LibraryId, progress,
|
||||
ProgressEventType.Updated, useFileName ? filePath : series.Name));
|
||||
sum += await GetWordCountFromHtml(bookPage);
|
||||
sum += await GetWordCountFromHtml(bookPage, filePath);
|
||||
pageCounter++;
|
||||
}
|
||||
|
||||
|
@ -245,13 +247,23 @@ public class WordCountAnalyzerService : IWordCountAnalyzerService
|
|||
}
|
||||
|
||||
|
||||
private static async Task<int> GetWordCountFromHtml(EpubLocalTextContentFileRef bookFile)
|
||||
private async Task<int> GetWordCountFromHtml(EpubLocalTextContentFileRef bookFile, string filePath)
|
||||
{
|
||||
var doc = new HtmlDocument();
|
||||
doc.LoadHtml(await bookFile.ReadContentAsync());
|
||||
try
|
||||
{
|
||||
var doc = new HtmlDocument();
|
||||
doc.LoadHtml(await bookFile.ReadContentAsync());
|
||||
|
||||
var textNodes = doc.DocumentNode.SelectNodes("//body//text()[not(parent::script)]");
|
||||
return textNodes?.Sum(node => node.InnerText.Count(char.IsLetter)) / AverageCharactersPerWord ?? 0;
|
||||
var textNodes = doc.DocumentNode.SelectNodes("//body//text()[not(parent::script)]");
|
||||
return textNodes?.Sum(node => node.InnerText.Count(char.IsLetter)) / AverageCharactersPerWord ?? 0;
|
||||
}
|
||||
catch (EpubContentException ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error when counting words in epub {EpubPath}", filePath);
|
||||
await _mediaErrorService.ReportMediaIssueAsync(filePath, MediaErrorProducer.BookService,
|
||||
$"Invalid Epub Metadata, {bookFile.FilePath} does not exist", ex.Message);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue