Skip to content

Commit

Permalink
#19 スクレイピング用クライアントを作成
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaji255 committed Mar 16, 2024
1 parent 059eaf5 commit d9e347d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Epub/KoeBook.Epub/Contracts/Services/IScrapingClientService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace KoeBook.Epub.Contracts.Services;

public interface IScrapingClientService
{
/// <summary>
/// スクレイピングでGETする用
/// APIは不要
/// </summary>
ValueTask<HttpResponseMessage> GetAsync(string url, CancellationToken ct);
}
5 changes: 4 additions & 1 deletion Epub/KoeBook.Epub/Services/ScrapingAozoraService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
22 changes: 22 additions & 0 deletions Epub/KoeBook.Epub/Services/ScrapingClientService.cs
Original file line number Diff line number Diff line change
@@ -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<HttpResponseMessage> GetAsync(string url, CancellationToken ct)
{
await _periodicTimer.WaitForNextTickAsync(ct).ConfigureAwait(false);

var httpClient = _httpClientFactory.CreateClient();
return await httpClient.GetAsync(url, ct).ConfigureAwait(false);
}
}
7 changes: 5 additions & 2 deletions Epub/KoeBook.Epub/Services/ScrapingNaroService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -22,6 +24,7 @@ public bool IsMatchSite(Uri uri)
public async ValueTask<EpubDocument> 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);

Expand Down Expand Up @@ -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<SectionWithChapterTitle> ReadPageAsync(string url, bool isRensai, string imageDirectory, CancellationToken ct)
private async Task<SectionWithChapterTitle> ReadPageAsync(string url, bool isRensai, string imageDirectory, CancellationToken ct)
{
var config = Configuration.Default.WithDefaultLoader();
using var context = BrowsingContext.New(config);
Expand Down
9 changes: 8 additions & 1 deletion KoeBook/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public App()
.UseContentRoot(AppContext.BaseDirectory)
.ConfigureServices((context, services) =>
{
// System
services.AddSingleton(TimeProvider.System);

// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();

Expand Down Expand Up @@ -99,7 +102,11 @@ public App()
services.AddSingleton<ILlmAnalyzerService, ChatGptAnalyzerService>();
services.AddSingleton<OpenAI.Interfaces.IOpenAIService, MyOpenAiService>();

services.AddSingleton<IScraperSelectorService, ScraperSelectorService>()
// Epub Services
services
.AddKeyedSingleton<IScrapingClientService, ScrapingClientService>(nameof(ScrapingAozoraService))
.AddKeyedSingleton<IScrapingClientService, ScrapingClientService>(nameof(ScrapingNaroService))
.AddSingleton<IScraperSelectorService, ScraperSelectorService>()
.AddSingleton<IScrapingService, ScrapingAozoraService>()
.AddSingleton<IScrapingService, ScrapingNaroService>();
services.AddSingleton<IEpubCreateService, EpubCreateService>();
Expand Down

0 comments on commit d9e347d

Please sign in to comment.