aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Events/Consumers/Library/LyricDownloadFailureLogger.cs
blob: 5f4864e953661ad86ea6de8dde8bc87c016b1923 (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
using System;
using System.Globalization;
using System.Threading.Tasks;
using Jellyfin.Database.Implementations.Entities;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Events;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Globalization;
using Episode = MediaBrowser.Controller.Entities.TV.Episode;

namespace Jellyfin.Server.Implementations.Events.Consumers.Library;

/// <summary>
/// Creates an entry in the activity log whenever a lyric download fails.
/// </summary>
public class LyricDownloadFailureLogger : IEventConsumer<LyricDownloadFailureEventArgs>
{
    private readonly ILocalizationManager _localizationManager;
    private readonly IActivityManager _activityManager;

    /// <summary>
    /// Initializes a new instance of the <see cref="LyricDownloadFailureLogger"/> class.
    /// </summary>
    /// <param name="localizationManager">The localization manager.</param>
    /// <param name="activityManager">The activity manager.</param>
    public LyricDownloadFailureLogger(ILocalizationManager localizationManager, IActivityManager activityManager)
    {
        _localizationManager = localizationManager;
        _activityManager = activityManager;
    }

    /// <inheritdoc />
    public async Task OnEvent(LyricDownloadFailureEventArgs eventArgs)
    {
        await _activityManager.CreateAsync(new ActivityLog(
            string.Format(
                CultureInfo.InvariantCulture,
                _localizationManager.GetLocalizedString("LyricDownloadFailureFromForItem"),
                eventArgs.Provider,
                GetItemName(eventArgs.Item)),
            "LyricDownloadFailure",
            Guid.Empty)
        {
            ItemId = eventArgs.Item.Id.ToString("N", CultureInfo.InvariantCulture),
            ShortOverview = eventArgs.Exception.Message
        }).ConfigureAwait(false);
    }

    private static string GetItemName(BaseItem item)
    {
        var name = item.Name;
        if (item is Episode episode)
        {
            if (episode.IndexNumber.HasValue)
            {
                name = string.Format(
                    CultureInfo.InvariantCulture,
                    "Ep{0} - {1}",
                    episode.IndexNumber.Value,
                    name);
            }

            if (episode.ParentIndexNumber.HasValue)
            {
                name = string.Format(
                    CultureInfo.InvariantCulture,
                    "S{0}, {1}",
                    episode.ParentIndexNumber.Value,
                    name);
            }
        }

        if (item is IHasSeries hasSeries)
        {
            name = hasSeries.SeriesName + " - " + name;
        }

        if (item is IHasAlbumArtist hasAlbumArtist)
        {
            var artists = hasAlbumArtist.AlbumArtists;

            if (artists.Count > 0)
            {
                name = artists[0] + " - " + name;
            }
        }
        else if (item is IHasArtist hasArtist)
        {
            var artists = hasArtist.Artists;

            if (artists.Count > 0)
            {
                name = artists[0] + " - " + name;
            }
        }

        return name;
    }
}