| | 1 | | using System; |
| | 2 | | using System.Globalization; |
| | 3 | | using System.Threading.Tasks; |
| | 4 | | using Jellyfin.Database.Implementations.Entities; |
| | 5 | | using MediaBrowser.Controller.Entities; |
| | 6 | | using MediaBrowser.Controller.Entities.Audio; |
| | 7 | | using MediaBrowser.Controller.Events; |
| | 8 | | using MediaBrowser.Controller.Subtitles; |
| | 9 | | using MediaBrowser.Model.Activity; |
| | 10 | | using MediaBrowser.Model.Globalization; |
| | 11 | | using Episode = MediaBrowser.Controller.Entities.TV.Episode; |
| | 12 | |
|
| | 13 | | namespace Jellyfin.Server.Implementations.Events.Consumers.Library |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Creates an entry in the activity log whenever a subtitle download fails. |
| | 17 | | /// </summary> |
| | 18 | | public class SubtitleDownloadFailureLogger : IEventConsumer<SubtitleDownloadFailureEventArgs> |
| | 19 | | { |
| | 20 | | private readonly ILocalizationManager _localizationManager; |
| | 21 | | private readonly IActivityManager _activityManager; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Initializes a new instance of the <see cref="SubtitleDownloadFailureLogger"/> class. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="localizationManager">The localization manager.</param> |
| | 27 | | /// <param name="activityManager">The activity manager.</param> |
| | 28 | | public SubtitleDownloadFailureLogger(ILocalizationManager localizationManager, IActivityManager activityManager) |
| | 29 | | { |
| 0 | 30 | | _localizationManager = localizationManager; |
| 0 | 31 | | _activityManager = activityManager; |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <inheritdoc /> |
| | 35 | | public async Task OnEvent(SubtitleDownloadFailureEventArgs eventArgs) |
| | 36 | | { |
| | 37 | | await _activityManager.CreateAsync(new ActivityLog( |
| | 38 | | string.Format( |
| | 39 | | CultureInfo.InvariantCulture, |
| | 40 | | _localizationManager.GetLocalizedString("SubtitleDownloadFailureFromForItem"), |
| | 41 | | eventArgs.Provider, |
| | 42 | | GetItemName(eventArgs.Item)), |
| | 43 | | "SubtitleDownloadFailure", |
| | 44 | | Guid.Empty) |
| | 45 | | { |
| | 46 | | ItemId = eventArgs.Item.Id.ToString("N", CultureInfo.InvariantCulture), |
| | 47 | | ShortOverview = eventArgs.Exception.Message |
| | 48 | | }).ConfigureAwait(false); |
| | 49 | | } |
| | 50 | |
|
| | 51 | | private static string GetItemName(BaseItem item) |
| | 52 | | { |
| 0 | 53 | | var name = item.Name; |
| 0 | 54 | | if (item is Episode episode) |
| | 55 | | { |
| 0 | 56 | | if (episode.IndexNumber.HasValue) |
| | 57 | | { |
| 0 | 58 | | name = string.Format( |
| 0 | 59 | | CultureInfo.InvariantCulture, |
| 0 | 60 | | "Ep{0} - {1}", |
| 0 | 61 | | episode.IndexNumber.Value, |
| 0 | 62 | | name); |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | if (episode.ParentIndexNumber.HasValue) |
| | 66 | | { |
| 0 | 67 | | name = string.Format( |
| 0 | 68 | | CultureInfo.InvariantCulture, |
| 0 | 69 | | "S{0}, {1}", |
| 0 | 70 | | episode.ParentIndexNumber.Value, |
| 0 | 71 | | name); |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | if (item is IHasSeries hasSeries) |
| | 76 | | { |
| 0 | 77 | | name = hasSeries.SeriesName + " - " + name; |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | if (item is IHasAlbumArtist hasAlbumArtist) |
| | 81 | | { |
| 0 | 82 | | var artists = hasAlbumArtist.AlbumArtists; |
| | 83 | |
|
| 0 | 84 | | if (artists.Count > 0) |
| | 85 | | { |
| 0 | 86 | | name = artists[0] + " - " + name; |
| | 87 | | } |
| | 88 | | } |
| 0 | 89 | | else if (item is IHasArtist hasArtist) |
| | 90 | | { |
| 0 | 91 | | var artists = hasArtist.Artists; |
| | 92 | |
|
| 0 | 93 | | if (artists.Count > 0) |
| | 94 | | { |
| 0 | 95 | | name = artists[0] + " - " + name; |
| | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | return name; |
| | 100 | | } |
| | 101 | | } |
| | 102 | | } |