aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Providers/Plugins/MusicBrainz/MusicBrainzQueryExtensions.cs
blob: f3df41e942ad6e63c0861e5e34720da68d0d56a3 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using MetaBrainz.Common;
using MetaBrainz.MusicBrainz;
using MetaBrainz.MusicBrainz.Interfaces.Entities;
using Microsoft.Extensions.Logging;

namespace MediaBrowser.Providers.Plugins.MusicBrainz;

/// <summary>
/// Helpers for talking to MusicBrainz with identifiers that are not guaranteed to be valid.
/// </summary>
internal static class MusicBrainzQueryExtensions
{
    /// <summary>
    /// Parses a MusicBrainz identifier, which may come from user-supplied tags or NFO files and is therefore not
    /// guaranteed to be a valid GUID.
    /// </summary>
    /// <param name="id">The identifier to parse.</param>
    /// <param name="entityType">The type of entity the identifier refers to, used for logging.</param>
    /// <param name="logger">The logger.</param>
    /// <returns>The parsed identifier, or <see langword="null"/> if it is missing or malformed.</returns>
    public static Guid? ParseMusicBrainzId(string? id, string entityType, ILogger logger)
    {
        if (string.IsNullOrWhiteSpace(id))
        {
            return null;
        }

        if (!Guid.TryParse(id, out var parsedId))
        {
            logger.LogDebug("Ignoring malformed MusicBrainz {EntityType} id {Id}", entityType, id);
            return null;
        }

        return parsedId;
    }

    /// <summary>
    /// Looks up a release, treating an unknown identifier as missing data rather than an error.
    /// </summary>
    /// <param name="query">The MusicBrainz query client.</param>
    /// <param name="releaseId">The release identifier.</param>
    /// <param name="include">The additional data to include in the lookup.</param>
    /// <param name="logger">The logger.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    /// <returns>The release, or <see langword="null"/> if MusicBrainz does not have it.</returns>
    public static Task<IRelease?> LookupReleaseOrNullAsync(this Query query, Guid releaseId, Include include, ILogger logger, CancellationToken cancellationToken)
        => NotFoundAsNullAsync(
            () => query.LookupReleaseAsync(releaseId, include, cancellationToken),
            "release",
            releaseId,
            logger);

    /// <summary>
    /// Looks up a release group, treating an unknown identifier as missing data rather than an error.
    /// </summary>
    /// <param name="query">The MusicBrainz query client.</param>
    /// <param name="releaseGroupId">The release group identifier.</param>
    /// <param name="include">The additional data to include in the lookup.</param>
    /// <param name="logger">The logger.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    /// <returns>The release group, or <see langword="null"/> if MusicBrainz does not have it.</returns>
    public static Task<IReleaseGroup?> LookupReleaseGroupOrNullAsync(this Query query, Guid releaseGroupId, Include include, ILogger logger, CancellationToken cancellationToken)
        => NotFoundAsNullAsync(
            () => query.LookupReleaseGroupAsync(releaseGroupId, include, null, cancellationToken),
            "release group",
            releaseGroupId,
            logger);

    /// <summary>
    /// Looks up an artist, treating an unknown identifier as missing data rather than an error.
    /// </summary>
    /// <param name="query">The MusicBrainz query client.</param>
    /// <param name="artistId">The artist identifier.</param>
    /// <param name="include">The additional data to include in the lookup.</param>
    /// <param name="logger">The logger.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    /// <returns>The artist, or <see langword="null"/> if MusicBrainz does not have it.</returns>
    public static Task<IArtist?> LookupArtistOrNullAsync(this Query query, Guid artistId, Include include, ILogger logger, CancellationToken cancellationToken)
        => NotFoundAsNullAsync(
            () => query.LookupArtistAsync(artistId, include, null, null, cancellationToken),
            "artist",
            artistId,
            logger);

    /// <summary>
    /// Runs a lookup, mapping a "not found" response to <see langword="null"/>. Identifiers stored on a library item
    /// can refer to entities that no longer exist in MusicBrainz, which is not an error worth failing a refresh over.
    /// </summary>
    /// <typeparam name="T">The type of entity being looked up.</typeparam>
    /// <param name="lookup">The lookup to run.</param>
    /// <param name="entityType">The type of entity being looked up, used for logging.</param>
    /// <param name="id">The identifier being looked up, used for logging.</param>
    /// <param name="logger">The logger.</param>
    /// <returns>The entity, or <see langword="null"/> if MusicBrainz does not have it.</returns>
    private static async Task<T?> NotFoundAsNullAsync<T>(Func<Task<T>> lookup, string entityType, Guid id, ILogger logger)
        where T : class
    {
        try
        {
            return await lookup().ConfigureAwait(false);
        }
        catch (HttpError ex) when (ex.Status == HttpStatusCode.NotFound)
        {
            logger.LogDebug("MusicBrainz has no {EntityType} with id {Id}", entityType, id);
            return null;
        }
    }
}