Create ILyricManager

This commit is contained in:
1hitsong 2022-09-15 20:49:25 -04:00
parent d9be3874ba
commit f4fd908f8d
9 changed files with 175 additions and 71 deletions

View File

@ -583,8 +583,6 @@ namespace Emby.Server.Implementations
serviceCollection.AddTransient(provider => new Lazy<ILibraryMonitor>(provider.GetRequiredService<ILibraryMonitor>));
serviceCollection.AddTransient(provider => new Lazy<IProviderManager>(provider.GetRequiredService<IProviderManager>));
serviceCollection.AddTransient(provider => new Lazy<IUserViewManager>(provider.GetRequiredService<IUserViewManager>));
serviceCollection.AddTransient<ILyricsProvider, TxtLyricsProvider>();
serviceCollection.AddTransient<ILyricsProvider, LrcLyricsProvider>();
serviceCollection.AddSingleton<ILibraryManager, LibraryManager>();
serviceCollection.AddSingleton<NamingOptions>();
@ -603,6 +601,7 @@ namespace Emby.Server.Implementations
serviceCollection.AddSingleton<IMediaSourceManager, MediaSourceManager>();
serviceCollection.AddSingleton<ISubtitleManager, SubtitleManager>();
serviceCollection.AddSingleton<ILyricManager, LyricManager>();
serviceCollection.AddSingleton<IProviderManager, ProviderManager>();
@ -790,6 +789,7 @@ namespace Emby.Server.Implementations
Resolve<ILiveTvManager>().AddParts(GetExports<ILiveTvService>(), GetExports<ITunerHost>(), GetExports<IListingsProvider>());
Resolve<ISubtitleManager>().AddParts(GetExports<ISubtitleProvider>());
Resolve<ILyricManager>().AddParts(GetExports<ILyricProvider>());
Resolve<IChannelManager>().AddParts(GetExports<IChannel>());

View File

@ -52,7 +52,7 @@ namespace Emby.Server.Implementations.Dto
private readonly IMediaSourceManager _mediaSourceManager;
private readonly Lazy<ILiveTvManager> _livetvManagerFactory;
private readonly IEnumerable<ILyricsProvider> _lyricProviders;
private readonly ILyricManager _lyricManager;
public DtoService(
ILogger<DtoService> logger,
@ -64,7 +64,7 @@ namespace Emby.Server.Implementations.Dto
IApplicationHost appHost,
IMediaSourceManager mediaSourceManager,
Lazy<ILiveTvManager> livetvManagerFactory,
IEnumerable<ILyricsProvider> lyricProviders)
ILyricManager lyricManager)
{
_logger = logger;
_libraryManager = libraryManager;
@ -75,7 +75,7 @@ namespace Emby.Server.Implementations.Dto
_appHost = appHost;
_mediaSourceManager = mediaSourceManager;
_livetvManagerFactory = livetvManagerFactory;
_lyricProviders = lyricProviders;
_lyricManager = lyricManager;
}
private ILiveTvManager LivetvManager => _livetvManagerFactory.Value;
@ -147,7 +147,7 @@ namespace Emby.Server.Implementations.Dto
}
else if (item is Audio)
{
dto.HasLyrics = MediaBrowser.Controller.Lyrics.LyricInfo.HasLyricFile(_lyricProviders, item.Path);
dto.HasLyrics = _lyricManager.HasLyricFile(item);
}
if (item is IItemByName itemByName

View File

@ -38,7 +38,7 @@ namespace Jellyfin.Api.Controllers
private readonly IDtoService _dtoService;
private readonly IUserViewManager _userViewManager;
private readonly IFileSystem _fileSystem;
private readonly IEnumerable<ILyricsProvider> _lyricProviders;
private readonly ILyricManager _lyricManager;
/// <summary>
/// Initializes a new instance of the <see cref="UserLibraryController"/> class.
@ -49,7 +49,7 @@ namespace Jellyfin.Api.Controllers
/// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
/// <param name="userViewManager">Instance of the <see cref="IUserViewManager"/> interface.</param>
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
/// <param name="lyricProviders">Collection of all registered <see cref="ILyricsProvider"/> interfaces.</param>
/// <param name="lyricManager">Instance of the <see cref="ILyricManager"/> interface.</param>
public UserLibraryController(
IUserManager userManager,
IUserDataManager userDataRepository,
@ -57,7 +57,7 @@ namespace Jellyfin.Api.Controllers
IDtoService dtoService,
IUserViewManager userViewManager,
IFileSystem fileSystem,
IEnumerable<ILyricsProvider> lyricProviders)
ILyricManager lyricManager)
{
_userManager = userManager;
_userDataRepository = userDataRepository;
@ -65,7 +65,7 @@ namespace Jellyfin.Api.Controllers
_dtoService = dtoService;
_userViewManager = userViewManager;
_fileSystem = fileSystem;
_lyricProviders = lyricProviders;
_lyricManager = lyricManager;
}
/// <summary>
@ -415,14 +415,10 @@ namespace Jellyfin.Api.Controllers
return NotFound();
}
var result = MediaBrowser.Controller.Lyrics.LyricInfo.GetLyricData(_lyricProviders, item);
var result = _lyricManager.GetLyric(item);
if (result is not null)
{
provider.Process(item);
if (provider.HasData)
{
return Ok(provider.Data);
}
return Ok(result);
}
return NotFound();

View File

@ -0,0 +1,37 @@
#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Providers;
namespace MediaBrowser.Controller.Lyrics
{
public interface ILyricManager
{
/// <summary>
/// Adds the parts.
/// </summary>
/// <param name="lyricProviders">The lyric providers.</param>
void AddParts(IEnumerable<ILyricProvider> lyricProviders);
/// <summary>
/// Gets the lyrics.
/// </summary>
/// <param name="item">The media item.</param>
/// <returns>Lyrics for passed item.</returns>
LyricResponse GetLyric(BaseItem item);
/// <summary>
/// Checks if requested item has a matching local lyric file.
/// </summary>
/// <param name="item">The media item.</param>
/// <returns>True if item has a matching lyrics file; otherwise false.</returns>
bool HasLyricFile(BaseItem item);
}
}

View File

@ -6,8 +6,13 @@ namespace MediaBrowser.Controller.Lyrics
/// <summary>
/// Interface ILyricsProvider.
/// </summary>
public interface ILyricsProvider
public interface ILyricProvider
{
/// <summary>
/// Gets a value indicating the provider name.
/// </summary>
string Name { get; }
/// <summary>
/// Gets the supported media types for this provider.
/// </summary>

View File

@ -13,42 +13,15 @@ namespace MediaBrowser.Controller.Lyrics
/// <summary>
/// Item helper.
/// </summary>
public class LyricInfo
public static class LyricInfo
{
/// <summary>
/// Opens lyrics file, converts to a List of Lyrics, and returns it.
/// </summary>
/// <param name="lyricProviders">Collection of all registered <see cref="ILyricsProvider"/> interfaces.</param>
/// <param name="item">Requested Item.</param>
/// <returns>Collection of Lyrics.</returns>
public static LyricResponse? GetLyricData(IEnumerable<ILyricsProvider> lyricProviders, BaseItem item)
{
foreach (var provider in lyricProviders)
{
var result = provider.GetLyrics(item);
if (result is not null)
{
return result;
}
}
return new LyricResponse
{
Lyrics = new List<Lyric>
{
new Lyric { Start = 0, Text = "Test" }
}
};
}
/// <summary>
/// Checks if requested item has a matching lyric file.
/// </summary>
/// <param name="lyricProvider">The current lyricProvider interface.</param>
/// <param name="itemPath">Path of requested item.</param>
/// <returns>True if item has a matching lyrics file.</returns>
public static string? GetLyricFilePath(ILyricsProvider lyricProvider, string itemPath)
public static string? GetLyricFilePath(ILyricProvider lyricProvider, string itemPath)
{
if (lyricProvider.SupportedMediaTypes.Any())
{
@ -64,24 +37,5 @@ namespace MediaBrowser.Controller.Lyrics
return null;
}
/// <summary>
/// Checks if requested item has a matching local lyric file.
/// </summary>
/// <param name="lyricProviders">Collection of all registered <see cref="ILyricsProvider"/> interfaces.</param>
/// <param name="itemPath">Path of requested item.</param>
/// <returns>True if item has a matching lyrics file; otherwise false.</returns>
public static bool HasLyricFile(IEnumerable<ILyricsProvider> lyricProviders, string itemPath)
{
foreach (var provider in lyricProviders)
{
if (GetLyricFilePath(provider, itemPath) is not null)
{
return true;
}
}
return false;
}
}
}

View File

@ -16,19 +16,26 @@ namespace MediaBrowser.Providers.Lyric
/// <summary>
/// LRC File Lyric Provider.
/// </summary>
public class LrcLyricsProvider : ILyricsProvider
public class LrcLyricProvider : ILyricProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="LrcLyricsProvider"/> class.
/// Initializes a new instance of the <see cref="LrcLyricProvider"/> class.
/// </summary>
public LrcLyricsProvider()
public LrcLyricProvider()
{
Name = "LrcLyricProvider";
SupportedMediaTypes = new Collection<string>
{
"lrc"
};
}
/// <summary>
/// Gets a value indicating the provider name.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets a value indicating the File Extenstions this provider works with.
/// </summary>

View File

@ -0,0 +1,97 @@
#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Extensions;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Subtitles;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.Lyric
{
public class LyricManager : ILyricManager
{
private readonly ILogger<LyricManager> _logger;
private readonly IFileSystem _fileSystem;
private readonly ILibraryMonitor _monitor;
private readonly IMediaSourceManager _mediaSourceManager;
private readonly ILocalizationManager _localization;
private ILyricProvider[] _lyricProviders;
public LyricManager(
ILogger<LyricManager> logger,
IFileSystem fileSystem,
ILibraryMonitor monitor,
IMediaSourceManager mediaSourceManager,
ILocalizationManager localizationManager)
{
_logger = logger;
_fileSystem = fileSystem;
_monitor = monitor;
_mediaSourceManager = mediaSourceManager;
_localization = localizationManager;
}
/// <inheritdoc />
public void AddParts(IEnumerable<ILyricProvider> lyricProviders)
{
_lyricProviders = lyricProviders
.OrderBy(i => i is IHasOrder hasOrder ? hasOrder.Order : 0)
.ToArray();
}
/// <inheritdoc />
public LyricResponse GetLyric(BaseItem item)
{
foreach (ILyricProvider provider in _lyricProviders)
{
var results = provider.GetLyrics(item);
if (results is not null)
{
return results;
}
}
return null;
}
/// <inheritdoc />
public bool HasLyricFile(BaseItem item)
{
foreach (ILyricProvider provider in _lyricProviders)
{
if (item is null)
{
continue;
}
if (LyricInfo.GetLyricFilePath(provider, item.Path) is not null)
{
return true;
}
}
return false;
}
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Jellyfin.Api.Helpers;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
@ -12,19 +13,26 @@ namespace MediaBrowser.Providers.Lyric
/// <summary>
/// TXT File Lyric Provider.
/// </summary>
public class TxtLyricsProvider : ILyricsProvider
public class TxtLyricProvider : ILyricProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="TxtLyricsProvider"/> class.
/// Initializes a new instance of the <see cref="TxtLyricProvider"/> class.
/// </summary>
public TxtLyricsProvider()
public TxtLyricProvider()
{
Name = "TxtLyricProvider";
SupportedMediaTypes = new Collection<string>
{
"lrc", "txt"
};
}
/// <summary>
/// Gets a value indicating the provider name.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets a value indicating the File Extenstions this provider works with.
/// </summary>