Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

なろうServiceのリファクタリング #24

Merged
merged 18 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading