-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from BeatLeader/replay-browser-downloader
Replay Browser beatmaps downloader
- Loading branch information
Showing
18 changed files
with
707 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using JetBrains.Annotations; | ||
|
||
namespace BeatLeader.Models.BeatSaver { | ||
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)] | ||
internal class MapDetail { | ||
public string? id; | ||
public MapDetailMetadata? metadata; | ||
public UserDetail? uploader; | ||
public MapVersion[]? versions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using JetBrains.Annotations; | ||
|
||
namespace BeatLeader.Models.BeatSaver { | ||
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)] | ||
public class MapDetailMetadata { | ||
public string? songName; | ||
public string? levelAuthorName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using JetBrains.Annotations; | ||
|
||
namespace BeatLeader.Models.BeatSaver { | ||
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)] | ||
internal class MapVersion { | ||
public string? hash; | ||
public string? coverURL; | ||
public string? downloadURL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using JetBrains.Annotations; | ||
|
||
namespace BeatLeader.Models.BeatSaver { | ||
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)] | ||
internal class UserDetail { | ||
public string? name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace BeatLeader.BeatSaverAPI { | ||
internal static class BeatSaverConstants { | ||
public const string BEATSAVER_API_URL = "https://api.beatsaver.com"; | ||
public const string BEATSAVER_WEBSITE_URL = "https://beatsaver.com"; | ||
public const string BEATSAVER_CDN_URL = "https://cdn.beatsaver.com/"; | ||
|
||
public const string MAPS_HASH_ENDPOINT = "/maps/hash/"; | ||
public const string MAPS_ENDPOINT = "/maps/"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Threading.Tasks; | ||
using BeatLeader.Models.BeatSaver; | ||
using Newtonsoft.Json; | ||
using static BeatLeader.BeatSaverAPI.BeatSaverConstants; | ||
|
||
namespace BeatLeader.Utils { | ||
internal static class BeatSaverUtils { | ||
public static async Task<MapDetail?> GetMapByHashAsync(string hash) { | ||
return await WebUtils.SendAsync(BEATSAVER_API_URL + MAPS_HASH_ENDPOINT + hash) is { IsSuccessStatusCode: true } res ? | ||
JsonConvert.DeserializeObject<MapDetail>(await res.Content.ReadAsStringAsync()) : null; | ||
} | ||
|
||
public static string CreateDownloadMapUrl(string mapHash) { | ||
return $"{BEATSAVER_CDN_URL}{mapHash.ToLower()}.zip"; | ||
} | ||
|
||
public static string CreateMapPageUrl(string bsr) { | ||
return $"{BEATSAVER_WEBSITE_URL}{MAPS_ENDPOINT}{bsr}"; | ||
} | ||
|
||
public static string FormatBeatmapFolderName(string? id, string? songName, string? authorName, string? hash) { | ||
return $"{id} ({songName} - {authorName}) [{hash}]"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
|
||
namespace BeatLeader.Utils { | ||
internal static class WebUtils { | ||
public static readonly HttpClient HttpClient = new(); | ||
|
||
public static async Task<byte[]?> SendRawDataRequestAsync( | ||
string url, | ||
Action<HttpRequestHeaders>? headersCallback = null | ||
) { | ||
return await SendRawDataRequestAsync(new Uri(url), headersCallback); | ||
} | ||
|
||
public static async Task<byte[]?> SendRawDataRequestAsync( | ||
Uri uri, | ||
Action<HttpRequestHeaders>? headersCallback = null | ||
) { | ||
return await SendAsync(uri) is { IsSuccessStatusCode: true } res | ||
? await res.Content.ReadAsByteArrayAsync() : null; | ||
} | ||
|
||
public static async Task<HttpResponseMessage> SendAsync( | ||
string url, | ||
string method = "GET", | ||
Action<HttpRequestHeaders>? headersCallback = null | ||
) { | ||
return await SendAsync(new Uri(url, UriKind.Absolute), method, headersCallback); | ||
} | ||
|
||
public static async Task<HttpResponseMessage> SendAsync( | ||
Uri uri, | ||
string method = "GET", | ||
Action<HttpRequestHeaders>? headersCallback = null | ||
) { | ||
var request = new HttpRequestMessage { | ||
RequestUri = uri, | ||
Method = new(method) | ||
}; | ||
headersCallback?.Invoke(request.Headers); | ||
return await HttpClient.SendAsync(request); | ||
} | ||
|
||
public static async Task<HttpResponseMessage> SendAsync( | ||
string url, | ||
IDictionary<string, string> headers, | ||
string method = "GET" | ||
) { | ||
return await SendAsync(new Uri(url, UriKind.Absolute), headers, method); | ||
} | ||
|
||
public static async Task<HttpResponseMessage> SendAsync( | ||
Uri uri, | ||
IDictionary<string, string> headers, | ||
string method = "GET" | ||
) { | ||
return await SendAsync(uri, method, x => { | ||
foreach (var item in headers) x.Add(item.Key, item.Value); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.