From d9e347db35cc8ea3ac310c3da11db808adf5f7cb Mon Sep 17 00:00:00 2001
From: miyaji255 <84168445+miyaji255@users.noreply.github.com>
Date: Fri, 15 Mar 2024 23:32:36 +0900
Subject: [PATCH] =?UTF-8?q?#19=20=E3=82=B9=E3=82=AF=E3=83=AC=E3=82=A4?=
=?UTF-8?q?=E3=83=94=E3=83=B3=E3=82=B0=E7=94=A8=E3=82=AF=E3=83=A9=E3=82=A4?=
=?UTF-8?q?=E3=82=A2=E3=83=B3=E3=83=88=E3=82=92=E4=BD=9C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Services/IScrapingClientService.cs | 10 +++++++++
.../Services/ScrapingAozoraService.cs | 5 ++++-
.../Services/ScrapingClientService.cs | 22 +++++++++++++++++++
.../Services/ScrapingNaroService.cs | 7 ++++--
KoeBook/App.xaml.cs | 9 +++++++-
5 files changed, 49 insertions(+), 4 deletions(-)
create mode 100644 Epub/KoeBook.Epub/Contracts/Services/IScrapingClientService.cs
create mode 100644 Epub/KoeBook.Epub/Services/ScrapingClientService.cs
diff --git a/Epub/KoeBook.Epub/Contracts/Services/IScrapingClientService.cs b/Epub/KoeBook.Epub/Contracts/Services/IScrapingClientService.cs
new file mode 100644
index 0000000..44cff57
--- /dev/null
+++ b/Epub/KoeBook.Epub/Contracts/Services/IScrapingClientService.cs
@@ -0,0 +1,10 @@
+namespace KoeBook.Epub.Contracts.Services;
+
+public interface IScrapingClientService
+{
+ ///
+ /// スクレイピングでGETする用
+ /// APIは不要
+ ///
+ ValueTask GetAsync(string url, CancellationToken ct);
+}
diff --git a/Epub/KoeBook.Epub/Services/ScrapingAozoraService.cs b/Epub/KoeBook.Epub/Services/ScrapingAozoraService.cs
index 3516dd9..7ea75c7 100644
--- a/Epub/KoeBook.Epub/Services/ScrapingAozoraService.cs
+++ b/Epub/KoeBook.Epub/Services/ScrapingAozoraService.cs
@@ -5,13 +5,16 @@
using KoeBook.Core;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Models;
+using Microsoft.Extensions.DependencyInjection;
using static KoeBook.Epub.Utility.ScrapingHelper;
namespace KoeBook.Epub.Services
{
- public partial class ScrapingAozoraService : IScrapingService
+ public partial class ScrapingAozoraService([FromKeyedServices(nameof(ScrapingAozoraService))] IScrapingClientService scrapingClientService) : IScrapingService
{
+ private readonly IScrapingClientService _scrapingClientService = scrapingClientService;
+
public bool IsMatchSite(Uri uri)
{
return uri.Host == "www.aozora.gr.jp";
diff --git a/Epub/KoeBook.Epub/Services/ScrapingClientService.cs b/Epub/KoeBook.Epub/Services/ScrapingClientService.cs
new file mode 100644
index 0000000..9a0ab20
--- /dev/null
+++ b/Epub/KoeBook.Epub/Services/ScrapingClientService.cs
@@ -0,0 +1,22 @@
+using KoeBook.Epub.Contracts.Services;
+
+namespace KoeBook.Epub.Services;
+
+public sealed class ScrapingClientService(IHttpClientFactory httpClientFactory, TimeProvider timeProvider) : IScrapingClientService, IDisposable
+{
+ private readonly IHttpClientFactory _httpClientFactory = httpClientFactory;
+ private readonly PeriodicTimer _periodicTimer = new(TimeSpan.FromSeconds(10), timeProvider);
+
+ public void Dispose()
+ {
+ _periodicTimer.Dispose();
+ }
+
+ public async ValueTask GetAsync(string url, CancellationToken ct)
+ {
+ await _periodicTimer.WaitForNextTickAsync(ct).ConfigureAwait(false);
+
+ var httpClient = _httpClientFactory.CreateClient();
+ return await httpClient.GetAsync(url, ct).ConfigureAwait(false);
+ }
+}
diff --git a/Epub/KoeBook.Epub/Services/ScrapingNaroService.cs b/Epub/KoeBook.Epub/Services/ScrapingNaroService.cs
index c04694f..c631a3d 100644
--- a/Epub/KoeBook.Epub/Services/ScrapingNaroService.cs
+++ b/Epub/KoeBook.Epub/Services/ScrapingNaroService.cs
@@ -6,13 +6,15 @@
using KoeBook.Core;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Models;
+using Microsoft.Extensions.DependencyInjection;
using static KoeBook.Epub.Utility.ScrapingHelper;
namespace KoeBook.Epub.Services
{
- public partial class ScrapingNaroService(IHttpClientFactory httpClientFactory) : IScrapingService
+ public partial class ScrapingNaroService(IHttpClientFactory httpClientFactory, [FromKeyedServices(nameof(ScrapingNaroService))] IScrapingClientService scrapingClientService) : IScrapingService
{
private readonly IHttpClientFactory _httpCliantFactory = httpClientFactory;
+ private readonly IScrapingClientService _scrapingClientService = scrapingClientService;
public bool IsMatchSite(Uri uri)
{
@@ -22,6 +24,7 @@ public bool IsMatchSite(Uri uri)
public async ValueTask ScrapingAsync(string url, string coverFilePath, string imageDirectory, Guid id, CancellationToken ct)
{
var config = Configuration.Default.WithDefaultLoader();
+
using var context = BrowsingContext.New(config);
var doc = await context.OpenAsync(url, ct).ConfigureAwait(false);
@@ -133,7 +136,7 @@ public record BookInfo(int? allcount, int? noveltype, int? general_all_no);
private record SectionWithChapterTitle(string? title, Section section);
- private static async Task ReadPageAsync(string url, bool isRensai, string imageDirectory, CancellationToken ct)
+ private async Task ReadPageAsync(string url, bool isRensai, string imageDirectory, CancellationToken ct)
{
var config = Configuration.Default.WithDefaultLoader();
using var context = BrowsingContext.New(config);
diff --git a/KoeBook/App.xaml.cs b/KoeBook/App.xaml.cs
index 0ed636f..10b0523 100644
--- a/KoeBook/App.xaml.cs
+++ b/KoeBook/App.xaml.cs
@@ -60,6 +60,9 @@ public App()
.UseContentRoot(AppContext.BaseDirectory)
.ConfigureServices((context, services) =>
{
+ // System
+ services.AddSingleton(TimeProvider.System);
+
// Default Activation Handler
services.AddTransient, DefaultActivationHandler>();
@@ -99,7 +102,11 @@ public App()
services.AddSingleton();
services.AddSingleton();
- services.AddSingleton()
+ // Epub Services
+ services
+ .AddKeyedSingleton(nameof(ScrapingAozoraService))
+ .AddKeyedSingleton(nameof(ScrapingNaroService))
+ .AddSingleton()
.AddSingleton()
.AddSingleton();
services.AddSingleton();