Skip to content

Commit

Permalink
reduced the responsibilities of the download processor.
Browse files Browse the repository at this point in the history
  • Loading branch information
waylaa committed Nov 24, 2023
1 parent 234c805 commit 6c53fd8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 68 deletions.
75 changes: 7 additions & 68 deletions OsuCollectionDownloader/Processors/DownloadProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace OsuCollectionDownloader.Processors;

internal sealed class DownloadProcessor(DownloadProcessorOptions options, IHttpClientFactory httpClientFactory, ILogger<DownloadProcessor> logger)
internal sealed class DownloadProcessor(DownloadProcessorOptions options, OsuCollectorService service, IHttpClientFactory httpClientFactory, ILogger<DownloadProcessor> logger)
{
private const string OsuCollectorApiUrl = "https://osucollector.com/api";

Expand All @@ -28,15 +28,15 @@ internal async Task DownloadAsync(CancellationToken ct)
}

logger.OngoingCollectionFetch();
Result<FetchedCollectionMetadata?> metadataResult = await GetMetadataAsync(options.Id, ct);
Result<FetchedCollectionMetadata?> metadataResult = await service.GetMetadataAsync(options.Id, ct);

if (!metadataResult.IsSuccessful || !metadataResult.HasValue)
{
logger.UnsuccessfulCollectionFetch();
return;
}

Result<ImmutableList<Beatmap>> fetchedBeatmapsResult = await GetBeatmapsAsync(options.Id, ct);
Result<ImmutableList<Beatmap>> fetchedBeatmapsResult = await service.GetBeatmapsAsync(options.Id, ct);

if (!fetchedBeatmapsResult.IsSuccessful || !fetchedBeatmapsResult.HasValue || fetchedBeatmapsResult.Value.IsEmpty)
{
Expand All @@ -46,9 +46,9 @@ internal async Task DownloadAsync(CancellationToken ct)

MirrorChain mirrorChain =
[
new NerinyanMirrorHandler(new NerinyanMirrorService(_client)),
new ChimuMirrorHandler(new ChimuService(_client)),
new OsuDirectMirrorHandler(new OsuDirectService(_client)),
new NerinyanHandler(new NerinyanService(_client)),
new ChimuHandler(new ChimuService(_client)),
new OsuDirectHandler(new OsuDirectService(_client)),
];

List<Beatmap> downloadedBeatmaps = [];
Expand Down Expand Up @@ -103,74 +103,13 @@ internal async Task DownloadAsync(CancellationToken ct)
}
}

private async Task<Result<FetchedCollectionMetadata?>> GetMetadataAsync(int id, CancellationToken ct)
{
try
{
return await _client.GetFromJsonAsync
(
$"{OsuCollectorApiUrl}/collections/{id}",
FetchedCollectionMetadataSerializationContext.Default.FetchedCollectionMetadata,
ct
);
}
catch (Exception ex)
{
return ex;
}
}

private async Task<Result<ImmutableList<Beatmap>>> GetBeatmapsAsync(int id, CancellationToken ct)
{
try
{
List<Beatmap> beatmaps = [];

FetchedCollection? collection = await _client.GetFromJsonAsync
(
$"{OsuCollectorApiUrl}/collections/{id}/beatmapsv2?cursor=0&perPage=100",
FetchedCollectionSerializationContext.Default.FetchedCollection,
ct
);

if (collection is null)
{
return ImmutableList<Beatmap>.Empty;
}

do
{
beatmaps.AddRange(collection.Beatmaps);

collection = await _client.GetFromJsonAsync
(
$"{OsuCollectorApiUrl}/collections/{id}/beatmapsv2?cursor={collection.NextPageCursor}&perPage=100",
FetchedCollectionSerializationContext.Default.FetchedCollection,
ct
);

if (collection is null)
{
return ImmutableList<Beatmap>.Empty;
}

} while (collection.HasMore);

return beatmaps.ToImmutableList();
}
catch (Exception ex)
{
return ex;
}
}

private Result<bool> Extract(string sourceBeatmapFilePath)
{
try
{
ZipFile.ExtractToDirectory
(
sourceBeatmapFilePath,
sourceBeatmapFilePath,
Path.Combine(options.OsuSongsDirectory.FullName, Path.GetFileNameWithoutExtension(sourceBeatmapFilePath)),
overwriteFiles: true
);
Expand Down
3 changes: 3 additions & 0 deletions OsuCollectionDownloader/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OsuCollectionDownloader.Processors;
using OsuCollectionDownloader.Services;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Parsing;
Expand Down Expand Up @@ -38,6 +39,7 @@ private static Task<int> Main(string[] args)
ctx.ParseResult.GetValueForOption(osuSongsDirectory)!,
ctx.ParseResult.GetValueForOption(osdbFileDirectory)
),
ctx.BindingContext.GetRequiredService<OsuCollectorService>(),
ctx.BindingContext.GetRequiredService<IHttpClientFactory>(),
ctx.BindingContext.GetRequiredService<ILogger<DownloadProcessor>>()
);
Expand Down Expand Up @@ -65,6 +67,7 @@ private static Task<int> Main(string[] args)
client.DefaultRequestHeaders.Accept.Add(new("application/octet-stream"));
})
.Services
.AddSingleton<OsuCollectorService>()
.BuildServiceProvider();

middleware.BindingContext.AddService(_ => provider.GetRequiredService<IHttpClientFactory>());
Expand Down
78 changes: 78 additions & 0 deletions OsuCollectionDownloader/Services/OsuCollectorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using OsuCollectionDownloader.Json.Contexts;
using OsuCollectionDownloader.Json.Models;
using OsuCollectionDownloader.Objects;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;

namespace OsuCollectionDownloader.Services;

internal sealed class OsuCollectorService(HttpClient client)
{
private const string BaseApiUrl = "https://osucollector.com/api";

internal async Task<Result<FetchedCollectionMetadata?>> GetMetadataAsync(int collectionId, CancellationToken token)
{
try
{
return await client.GetFromJsonAsync
(
$"{BaseApiUrl}/collections/{collectionId}",
FetchedCollectionMetadataSerializationContext.Default.FetchedCollectionMetadata,
token
);
}
catch (Exception ex)
{
return ex;
}
}

internal async Task<Result<ImmutableList<Beatmap>>> GetBeatmapsAsync(int collectionId, CancellationToken ct)
{
try
{
List<Beatmap> beatmaps = [];

FetchedCollection? collection = await client.GetFromJsonAsync
(
$"{BaseApiUrl}/collections/{collectionId}/beatmapsv2?cursor=0&perPage=100",
FetchedCollectionSerializationContext.Default.FetchedCollection,
ct
);

if (collection is null)
{
return ImmutableList<Beatmap>.Empty;
}

do
{
beatmaps.AddRange(collection.Beatmaps);

collection = await client.GetFromJsonAsync
(
$"{BaseApiUrl}/collections/{collectionId}/beatmapsv2?cursor={collection.NextPageCursor}&perPage=100",
FetchedCollectionSerializationContext.Default.FetchedCollection,
ct
);

if (collection is null)
{
return ImmutableList<Beatmap>.Empty;
}

} while (collection.HasMore || collection.NextPageCursor is not null);

return beatmaps.ToImmutableList();
}
catch (Exception ex)
{
return ex;
}
}
}

0 comments on commit 6c53fd8

Please sign in to comment.