Move reading time to a service.
MODIFY
XRayBuilder.Core/src/Bootstrap/BootstrapXRayBuilder.cs
XRayBuilder.Core/src/Bootstrap/BootstrapXRayBuilder.cs
using XRayBuilder.Core.Libraries.Language.Localization;
using XRayBuilder.Core.Libraries.SimpleInjector.Extensions;
using XRayBuilder.Core.Logic;
+using XRayBuilder.Core.Logic.ReadingTime;
namespace XRayBuilder.Core.Bootstrap
{
@@ -17,6 +18,7 @@ public void Register(Container container)
{
container.RegisterSingleton<IDirectoryService, DirectoryService>();
container.RegisterSingleton<IBookSearchService, BookSearchService>();
+ container.RegisterSingleton<IReadingTimeService, ReadingTimeService>();
container.AutoregisterConcreteCollectionFromInterface<ILanguage>(Lifestyle.Singleton);
}
}

MODIFY
XRayBuilder.Core/src/DataSources/Secondary/SecondarySourceGoodreads.cs
XRayBuilder.Core/src/DataSources/Secondary/SecondarySourceGoodreads.cs
using XRayBuilder.Core.Libraries.Logging;
using XRayBuilder.Core.Libraries.Primitives.Extensions;
using XRayBuilder.Core.Libraries.Progress;
+using XRayBuilder.Core.Logic.ReadingTime;
using XRayBuilder.Core.Model;
using XRayBuilder.Core.Unpack;
using XRayBuilder.Core.XRay.Artifacts;
@@ -30,6 +31,7 @@ public sealed class SecondarySourceGoodreads : SecondarySource
private readonly ILogger _logger;
private readonly IHttpClient _httpClient;
private readonly IAmazonClient _amazonClient;
+ private readonly IReadingTimeService _readingTimeService;
private const int MaxConcurrentRequests = 10;
@@ -42,11 +44,12 @@ public sealed class SecondarySourceGoodreads : SecondarySource
private readonly Regex _regexEditions = new(@"editions/(?<editions>[0-9]*)-", RegexOptions.Compiled);
private readonly Regex _regexPages = new(@"(?<pages>(\d+)|(\d+,\d+)) pages", RegexOptions.Compiled);
- public SecondarySourceGoodreads(ILogger logger, IHttpClient httpClient, IAmazonClient amazonClient)
+ public SecondarySourceGoodreads(ILogger logger, IHttpClient httpClient, IAmazonClient amazonClient, IReadingTimeService readingTimeService)
{
_logger = logger;
_httpClient = httpClient;
_amazonClient = amazonClient;
+ _readingTimeService = readingTimeService;
}
private string ParseBookIdFromUrl(string input) => _regexBookId.Match(input).Groups["id"].Value;
@@ -248,15 +251,14 @@ public override async Task<bool> GetPageCountAsync(BookInfo curBook, Cancellatio
if (!match.Success)
return false;
- var minutes = int.Parse(match.Groups[1].Value, NumberStyles.AllowThousands) * 1.098507462686567;
- var span = TimeSpan.FromMinutes(minutes);
- var p = match.Groups["pages"].Value;
+ var pages = int.Parse(match.Groups["pages"].Value, NumberStyles.AllowThousands);
+ var readingTime = _readingTimeService.GetReadingTime(pages);
- curBook.PageCount = int.Parse(p, NumberStyles.AllowThousands, CultureInfo.InvariantCulture);
- curBook.ReadingHours = span.Hours;
- curBook.ReadingMinutes = span.Minutes;
+ curBook.PageCount = pages;
+ curBook.ReadingHours = readingTime.Hours;
+ curBook.ReadingMinutes = readingTime.Minutes;
- _logger.Log(Functions.GetReadingTime(curBook));
+ _logger.Log(_readingTimeService.GetFormattedReadingTime(pages));
return true;
}

MODIFY
XRayBuilder.Core/src/DataSources/Secondary/SecondarySourceShelfari.cs
XRayBuilder.Core/src/DataSources/Secondary/SecondarySourceShelfari.cs
using XRayBuilder.Core.Libraries.Http;
using XRayBuilder.Core.Libraries.Logging;
using XRayBuilder.Core.Libraries.Progress;
+using XRayBuilder.Core.Logic.ReadingTime;
using XRayBuilder.Core.Model;
using XRayBuilder.Core.Unpack;
using XRayBuilder.Core.XRay.Artifacts;
@@ -23,13 +24,15 @@ public sealed class SecondarySourceShelfari : SecondarySource
{
private readonly ILogger _logger;
private readonly IHttpClient _httpClient;
+ private readonly IReadingTimeService _readingTimeService;
private HtmlDocument sourceHtmlDoc;
- public SecondarySourceShelfari(ILogger logger, IHttpClient httpClient)
+ public SecondarySourceShelfari(ILogger logger, IHttpClient httpClient, IReadingTimeService readingTimeService)
{
_logger = logger;
_httpClient = httpClient;
+ _readingTimeService = readingTimeService;
}
public override string Name => "Shelfari";
@@ -106,12 +109,14 @@ public override async Task<bool> GetPageCountAsync(BookInfo curBook, Cancellatio
var match1 = Regex.Match(node1.InnerText, @"Page Count: ((\d+)|(\d+,\d+))");
if (match1.Success)
{
- var minutes = int.Parse(match1.Groups[1].Value, NumberStyles.AllowThousands) * 1.098507462686567;
- var span = TimeSpan.FromMinutes(minutes);
- curBook.PageCount = int.Parse(match1.Groups[1].Value);
- curBook.ReadingHours = span.Hours;
- curBook.ReadingMinutes = span.Minutes;
- _logger.Log(Functions.GetReadingTime(curBook));
+ var pages = int.Parse(match1.Groups[1].Value, NumberStyles.AllowThousands);
+ var readingTime = _readingTimeService.GetReadingTime(pages);
+
+ curBook.PageCount = pages;
+ curBook.ReadingHours = readingTime.Hours;
+ curBook.ReadingMinutes = readingTime.Minutes;
+
+ _logger.Log(_readingTimeService.GetFormattedReadingTime(pages));
return true;
}
return false;

MODIFY
XRayBuilder.Core/src/Libraries/Functions.cs
XRayBuilder.Core/src/Libraries/Functions.cs
using HtmlAgilityPack;
using JetBrains.Annotations;
using XRayBuilder.Core.Libraries.Language.Pluralization;
+using XRayBuilder.Core.Logic.ReadingTime;
using XRayBuilder.Core.Model;
namespace XRayBuilder.Core.Libraries
{
public static class Functions
{
+ private static readonly IReadingTimeService _readingTimeService;
+
public static string ReadFromFile(string file)
{
using var streamReader = new StreamReader(file, Encoding.UTF8);
@@ -76,26 +79,6 @@ public static string TimeStamp()
return $"Running X-Ray Builder GUI v{version}. Log started on {date} at {time}.\r\n";
}
- public static string GetReadingTime(BookInfo bookInfo)
- {
- if (bookInfo.PageCount == 0)
- return null;
-
- var minutes = bookInfo.PageCount * 1.098507462686567;
- var span = TimeSpan.FromMinutes(minutes);
-
- var d = PluralUtil.Pluralize($"{span.Days:day}");
- var h = PluralUtil.Pluralize($"{span.Hours:hour}");
- var m = PluralUtil.Pluralize($"{span.Minutes:minute}");
- var p = bookInfo.PageCount;
-
- bookInfo.ReadingHours = span.Hours;
- bookInfo.ReadingMinutes = span.Minutes;
-
- var formatted = $"Typical time to read: {(span.Days > 1 ? $"{d}, " : string.Empty)}{(span.Hours > 1 ? $"{h}" : string.Empty)}{(span.Hours > 1 ? " and " : ", ")}{(span.Minutes > 1 ? $"{m}" : string.Empty)} ({p} pages)";
- return formatted.StartsWith(", ") ? formatted.Substring(2, formatted.Length) : formatted;
- }
-
public static string GetPageCount(string rawMl, BookInfo bookInfo)
{
string output;
@@ -129,12 +112,14 @@ public static string GetPageCount(string rawMl, BookInfo bookInfo)
output = "An error occurred while estimating page count!";
return output;
}
- var minutes = pageCount * 1.098507462686567;
- var span = TimeSpan.FromMinutes(minutes);
+
+ var readingTime = _readingTimeService.GetReadingTime(pageCount);
+
bookInfo.PageCount = pageCount;
- bookInfo.ReadingHours = span.Hours;
- bookInfo.ReadingMinutes = span.Minutes;
- return GetReadingTime(bookInfo);
+ bookInfo.ReadingHours = readingTime.Hours;
+ bookInfo.ReadingMinutes = readingTime.Minutes;
+
+ return _readingTimeService.GetFormattedReadingTime(pageCount);
}
public static void RunNotepad(string filename)

Move reading time to a service.