Skip to content

Commit

Permalink
Merge pull request #24 from OUCC/feat/#1-2-naro
Browse files Browse the repository at this point in the history
なろうServiceのリファクタリング
  • Loading branch information
miyaji255 authored Apr 2, 2024
2 parents ec1e0a7 + f7269be commit 78e58aa
Show file tree
Hide file tree
Showing 25 changed files with 4,612 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ namespace KoeBook.Epub.Contracts.Services;
/// </summary>
public interface IScraperSelectorService
{
/// <summary>
/// 外部URLが処理の対象か調べます
/// </summary>
public bool IsMatchSites(string url);

public ValueTask<EpubDocument> ScrapingAsync(string url, string coverFillePath, string tempDirectory, Guid id, CancellationToken ct);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public interface IScrapingClientService
/// APIを叩く際は不要
/// </summary>
Task<ContentDispositionHeaderValue?> GetAsStreamAsync(string url, Stream destination, CancellationToken ct);

/// <summary>
/// ファイルダウンロードを行います
/// </summary>
Task DownloadToFileAsync(string url, string destPath, CancellationToken ct);
}
4 changes: 3 additions & 1 deletion Epub/KoeBook.Epub/Contracts/Services/IScrapingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace KoeBook.Epub.Contracts.Services;

public interface IScrapingService : IScraperSelectorService
public interface IScrapingService
{
public bool IsMatchSite(Uri url);

public ValueTask<EpubDocument> ScrapingAsync(string url, string coverFillePath, string tempDirectory, Guid id, CancellationToken ct);
}
2 changes: 1 addition & 1 deletion Epub/KoeBook.Epub/Models/Chapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class Chapter
{
public List<Section> Sections = [];
public List<Section> Sections { get; init; } = [];
public string? Title { get; set; }
}
13 changes: 13 additions & 0 deletions Epub/KoeBook.Epub/Services/ScraperSelectorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ public class ScraperSelectorService(IEnumerable<IScrapingService> scrapingServic
{
private readonly ImmutableArray<IScrapingService> _scrapingServices = scrapingServices.ToImmutableArray();

public bool IsMatchSites(string url)
{
try
{
var uri = new Uri(url);
return _scrapingServices.Any(service => service.IsMatchSite(uri));
}
catch (UriFormatException)
{
return false;
}
}

public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFillePath, string tempDirectory, Guid id, CancellationToken ct)
{
var uri = new Uri(url);
Expand Down
33 changes: 32 additions & 1 deletion Epub/KoeBook.Epub/Services/ScrapingClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ public Task<string> GetAsStringAsync(string url, CancellationToken ct)
return taskCompletion.Task;
}

public Task DownloadToFileAsync(string url, string destPath, CancellationToken ct)
{
var taskCompletion = new TaskCompletionSource();

lock (_actionQueue)
_actionQueue.Enqueue(async httpClient =>
{
if (ct.IsCancellationRequested)
taskCompletion.SetCanceled(ct);

try
{
using var fileStream = new FileStream(destPath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, useAsync: true);
var response = await httpClient.GetAsync(url, ct).ConfigureAwait(false);
await response.Content.CopyToAsync(fileStream, ct).ConfigureAwait(false);
await fileStream.FlushAsync(ct).ConfigureAwait(false);
taskCompletion.SetResult();
}
catch (Exception ex)
{
taskCompletion.SetException(ex);
}
});

EnsureWorkerActivated();

return taskCompletion.Task;
}

/// <summary>
/// <see cref="Worker"/>が起動していない場合は起動します
/// </summary>
Expand All @@ -92,14 +121,16 @@ private async void Worker()

try
{
var httpClient = _httpClientFactory.CreateClient();
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36");
while (await _periodicTimer.WaitForNextTickAsync().ConfigureAwait(false) && _actionQueue.Count > 0)
{
Func<HttpClient, Task>? action;
lock (_actionQueue)
if (!_actionQueue.TryDequeue(out action))
continue;

await action(_httpClientFactory.CreateClient()).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
await action(httpClient).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
}
}
finally
Expand Down
Loading

0 comments on commit 78e58aa

Please sign in to comment.