about summary refs log tree commit diff
path: root/OsuFederatedBeatmapApi/Services/BeatmapFetcherService.cs
blob: ae108fb4b5da24e7c965002df81e883778d8201d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System.Net.Http.Headers;
using OsuFederatedBeatmapApi.Events.State;
using OsuFederatedBeatmapApi.UtilityClasses;

namespace OsuFederatedBeatmapApi.Services;

public class BeatmapFetcherService(FederatedBeatmapApiBotAccountDataService ads) {

    public async Task<BeatmapSetInfo> FetchBeatmapSetInfo(int id) {
        var hc = new HttpClient();
        hc.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("OsuFederatedBeatmapApi", "1.0"));
        var res = await hc.GetFromJsonAsync<OsuDirectApiResponses.BeatmapSet>($"https://osu.direct/api/s/{id}");
        var info = new BeatmapSetInfo {
            Title = res.Title,
            Artist = res.Artist,
            Creator = res.Creator,
            Beatmaps = res.ChildrenBeatmaps.ToDictionary(b => b.BeatmapID, b => new BeatmapSetInfo.BeatmapInfo {
                Difficulty = b.DiffName,
                Mode = b.Mode,
                DifficultyRating = b.DifficultyRating,
                TotalLength = b.TotalLength,
                BPM = b.BPM,
                MaxCombo = b.MaxCombo
            })
        };

        await ads.LoadAccountDataAsync();

        await ads.OwnBeatmapRepositoryRoom.SendStateEventAsync(BeatmapSetInfo.EventName, id.ToString(), info);

        return info;
    }

}