| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Net.Http; |
| | | 5 | | using System.Net.Http.Json; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using MediaBrowser.Common.Net; |
| | | 9 | | using MediaBrowser.Providers.Plugins.ListenBrainz.Api.Models; |
| | | 10 | | using MediaBrowser.Providers.Plugins.ListenBrainz.Configuration; |
| | | 11 | | using Microsoft.Extensions.Logging; |
| | | 12 | | |
| | | 13 | | namespace MediaBrowser.Providers.Plugins.ListenBrainz.Api; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Client for the ListenBrainz Labs API. |
| | | 17 | | /// </summary> |
| | | 18 | | public class ListenBrainzLabsClient : IDisposable |
| | | 19 | | { |
| | | 20 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 21 | | private readonly ILogger<ListenBrainzLabsClient> _logger; |
| | 21 | 22 | | private readonly SemaphoreSlim _rateLimitLock = new(1, 1); |
| | | 23 | | |
| | 21 | 24 | | private DateTime _lastRequestTime = DateTime.MinValue; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="ListenBrainzLabsClient"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="httpClientFactory">The HTTP client factory.</param> |
| | | 30 | | /// <param name="logger">The logger.</param> |
| | | 31 | | public ListenBrainzLabsClient( |
| | | 32 | | IHttpClientFactory httpClientFactory, |
| | | 33 | | ILogger<ListenBrainzLabsClient> logger) |
| | | 34 | | { |
| | 21 | 35 | | _httpClientFactory = httpClientFactory; |
| | 21 | 36 | | _logger = logger; |
| | 21 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets similar artists for the given MusicBrainz artist ID. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="artistMbid">The MusicBrainz artist ID.</param> |
| | | 43 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 44 | | /// <returns>A list of similar artist MusicBrainz IDs ordered by similarity score.</returns> |
| | | 45 | | public async Task<IReadOnlyList<Guid>> GetSimilarArtistsAsync( |
| | | 46 | | Guid artistMbid, |
| | | 47 | | CancellationToken cancellationToken) |
| | | 48 | | { |
| | 0 | 49 | | var config = ListenBrainzPlugin.Instance?.Configuration; |
| | 0 | 50 | | var baseUrl = config?.LabsServer ?? PluginConfiguration.DefaultLabsServer; |
| | 0 | 51 | | var algorithm = config?.AlgorithmString ?? new PluginConfiguration().AlgorithmString; |
| | 0 | 52 | | var rateLimit = config?.RateLimit ?? PluginConfiguration.DefaultRateLimit; |
| | | 53 | | |
| | | 54 | | // Enforce rate limit |
| | 0 | 55 | | await EnforceRateLimitAsync(rateLimit, cancellationToken).ConfigureAwait(false); |
| | | 56 | | |
| | 0 | 57 | | var url = $"{baseUrl}/similar-artists/json?artist_mbids={artistMbid}&algorithm={algorithm}"; |
| | | 58 | | |
| | 0 | 59 | | _logger.LogDebug("Fetching similar artists from ListenBrainz Labs: {Url}", url); |
| | | 60 | | |
| | | 61 | | try |
| | | 62 | | { |
| | 0 | 63 | | var httpClient = _httpClientFactory.CreateClient(NamedClient.Default); |
| | 0 | 64 | | var response = await httpClient.GetFromJsonAsync<List<SimilarArtistData>>(url, cancellationToken).ConfigureA |
| | | 65 | | |
| | 0 | 66 | | if (response is null || response.Count == 0) |
| | | 67 | | { |
| | 0 | 68 | | _logger.LogDebug("No similar artists found for {ArtistMbid}", artistMbid); |
| | 0 | 69 | | return []; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | var similarMbids = response |
| | 0 | 73 | | .Where(a => !a.ArtistMbid.Equals(artistMbid)) // Exclude the source artist |
| | 0 | 74 | | .OrderByDescending(a => a.Score) |
| | 0 | 75 | | .Select(a => a.ArtistMbid) |
| | 0 | 76 | | .ToList(); |
| | | 77 | | |
| | 0 | 78 | | _logger.LogDebug("Found {Count} similar artists for {ArtistMbid}", similarMbids.Count, artistMbid); |
| | | 79 | | |
| | 0 | 80 | | return similarMbids; |
| | | 81 | | } |
| | 0 | 82 | | catch (HttpRequestException ex) |
| | | 83 | | { |
| | 0 | 84 | | _logger.LogWarning(ex, "Failed to fetch similar artists from ListenBrainz Labs for {ArtistMbid}", artistMbid |
| | 0 | 85 | | return []; |
| | | 86 | | } |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | | 90 | | public void Dispose() |
| | | 91 | | { |
| | 21 | 92 | | Dispose(true); |
| | 21 | 93 | | GC.SuppressFinalize(this); |
| | 21 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release onl |
| | | 100 | | protected virtual void Dispose(bool disposing) |
| | | 101 | | { |
| | 21 | 102 | | if (disposing) |
| | | 103 | | { |
| | 21 | 104 | | _rateLimitLock.Dispose(); |
| | | 105 | | } |
| | 21 | 106 | | } |
| | | 107 | | |
| | | 108 | | private async Task EnforceRateLimitAsync(double rateLimitSeconds, CancellationToken cancellationToken) |
| | | 109 | | { |
| | 0 | 110 | | await _rateLimitLock.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | | 111 | | try |
| | | 112 | | { |
| | 0 | 113 | | var timeSinceLastRequest = DateTime.UtcNow - _lastRequestTime; |
| | 0 | 114 | | var requiredDelay = TimeSpan.FromSeconds(rateLimitSeconds) - timeSinceLastRequest; |
| | | 115 | | |
| | 0 | 116 | | if (requiredDelay > TimeSpan.Zero) |
| | | 117 | | { |
| | 0 | 118 | | await Task.Delay(requiredDelay, cancellationToken).ConfigureAwait(false); |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | _lastRequestTime = DateTime.UtcNow; |
| | 0 | 122 | | } |
| | | 123 | | finally |
| | | 124 | | { |
| | 0 | 125 | | _rateLimitLock.Release(); |
| | | 126 | | } |
| | 0 | 127 | | } |
| | | 128 | | } |