From 63a9986811a2d15a6347b5938e0dde00d1e7d484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=9A=D0=B2?= =?UTF-8?q?=D0=B0=D1=88=D0=B8=D0=BD?= Date: Wed, 25 Dec 2024 22:30:36 +0400 Subject: [PATCH] Refactoring. Fixed warning. Made some fields nullable. Return string.Empty instead of null. --- LyricsScraperNET.Client/Program.cs | 4 ++++ LyricsScraperNET/ILyricsScraperClient.cs | 2 +- LyricsScraperNET/LyricsScraperClient.cs | 14 +++++++++----- .../Network/HtmlAgilityWebClient.cs | 4 ++-- LyricsScraperNET/Network/NetHttpClient.cs | 2 +- .../Providers/AZLyrics/AZLyricsParser.cs | 2 +- .../Providers/AZLyrics/AZLyricsProvider.cs | 5 +++-- .../Providers/Abstract/ExternalProviderBase.cs | 2 +- .../Providers/Genius/GeniusParser.cs | 2 +- .../Providers/Genius/GeniusProvider.cs | 11 ++++++----- LyricsScraperNET/Providers/IProviderService.cs | 2 +- .../Providers/LyricFind/LyricFindParser.cs | 3 ++- .../Providers/LyricFind/LyricFindProvider.cs | 7 ++++--- .../Musixmatch/MusixmatchClientWrapper.cs | 3 ++- .../Providers/Musixmatch/MusixmatchProvider.cs | 7 ++++--- .../Musixmatch/MusixmatchTokenCache.cs | 18 ++++++++++++++---- LyricsScraperNET/Providers/ProviderService.cs | 8 ++++---- .../Providers/SongLyrics/SongLyricsParser.cs | 2 +- .../Providers/SongLyrics/SongLyricsProvider.cs | 7 ++++--- 19 files changed, 65 insertions(+), 40 deletions(-) diff --git a/LyricsScraperNET.Client/Program.cs b/LyricsScraperNET.Client/Program.cs index a7541af..082ce1a 100644 --- a/LyricsScraperNET.Client/Program.cs +++ b/LyricsScraperNET.Client/Program.cs @@ -15,6 +15,8 @@ using Microsoft.Extensions.Logging; using System.Threading; +#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously + class Program { static async Task Main() @@ -167,4 +169,6 @@ ILyricsScraperClient lyricsScraperClient return result; } + +#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously } diff --git a/LyricsScraperNET/ILyricsScraperClient.cs b/LyricsScraperNET/ILyricsScraperClient.cs index 812bcf8..9c4ac3b 100644 --- a/LyricsScraperNET/ILyricsScraperClient.cs +++ b/LyricsScraperNET/ILyricsScraperClient.cs @@ -24,7 +24,7 @@ public interface ILyricsScraperClient /// bool UseParallelSearch { get; set; } - IExternalProvider this[ExternalProviderType providerType] { get; } + IExternalProvider? this[ExternalProviderType providerType] { get; } /// /// Search lyric by different search requests: diff --git a/LyricsScraperNET/LyricsScraperClient.cs b/LyricsScraperNET/LyricsScraperClient.cs index c6b78fb..a66d2ee 100644 --- a/LyricsScraperNET/LyricsScraperClient.cs +++ b/LyricsScraperNET/LyricsScraperClient.cs @@ -18,12 +18,12 @@ namespace LyricsScraperNET { public sealed class LyricsScraperClient : ILyricsScraperClient { - private ILoggerFactory _loggerFactory; - private ILogger _logger; + private ILoggerFactory? _loggerFactory; + private ILogger? _logger; private IProviderService _providerService; private IRequestValidator _requestValidator; - private readonly ILyricScraperClientConfig _lyricScraperClientConfig; + private readonly ILyricScraperClientConfig? _lyricScraperClientConfig; private bool? _useParallelSearch; /// @@ -37,7 +37,7 @@ public bool UseParallelSearch public bool IsEnabled => _providerService.AnyEnabled(); /// - public IExternalProvider this[ExternalProviderType providerType] + public IExternalProvider? this[ExternalProviderType providerType] { get => _providerService[providerType]; } @@ -221,6 +221,10 @@ public void RemoveProvider(ExternalProviderType providerType) public void Disable() => _providerService.DisableAllProviders(); /// - public void WithLogger(ILoggerFactory loggerFactory) => _providerService.WithLogger(loggerFactory); + public void WithLogger(ILoggerFactory loggerFactory) + { + _loggerFactory = loggerFactory; + _providerService.WithLogger(loggerFactory); + } } } \ No newline at end of file diff --git a/LyricsScraperNET/Network/HtmlAgilityWebClient.cs b/LyricsScraperNET/Network/HtmlAgilityWebClient.cs index 656a38d..c8828ea 100644 --- a/LyricsScraperNET/Network/HtmlAgilityWebClient.cs +++ b/LyricsScraperNET/Network/HtmlAgilityWebClient.cs @@ -14,7 +14,7 @@ namespace LyricsScraperNET.Network /// internal sealed class HtmlAgilityWebClient : IWebClient { - private readonly ILogger _logger; + private readonly ILogger? _logger; private readonly HtmlWeb _htmlWeb; public HtmlAgilityWebClient() @@ -49,7 +49,7 @@ public async Task LoadAsync(Uri uri, CancellationToken cancellationToken } CheckDocument(document, uri); - return document?.ParsedText; + return document?.ParsedText ?? string.Empty; } private void CheckDocument(HtmlDocument document, Uri uri) diff --git a/LyricsScraperNET/Network/NetHttpClient.cs b/LyricsScraperNET/Network/NetHttpClient.cs index 10b9185..fd21064 100644 --- a/LyricsScraperNET/Network/NetHttpClient.cs +++ b/LyricsScraperNET/Network/NetHttpClient.cs @@ -14,7 +14,7 @@ namespace LyricsScraperNET.Network /// internal sealed class NetHttpClient : IWebClient { - private readonly ILogger _logger; + private readonly ILogger? _logger; // HttpClient is declared as static to prevent frequent creation and disposal of instances, // which can lead to socket exhaustion due to delays in releasing resources. diff --git a/LyricsScraperNET/Providers/AZLyrics/AZLyricsParser.cs b/LyricsScraperNET/Providers/AZLyrics/AZLyricsParser.cs index 581f183..c50d300 100644 --- a/LyricsScraperNET/Providers/AZLyrics/AZLyricsParser.cs +++ b/LyricsScraperNET/Providers/AZLyrics/AZLyricsParser.cs @@ -7,7 +7,7 @@ internal sealed class AZLyricsParser : IExternalProviderLyricParser { public string Parse(string lyric) { - return UnescapeString(RemoveAllHtmlTags(lyric))?.Trim(); + return UnescapeString(RemoveAllHtmlTags(lyric))?.Trim() ?? string.Empty; } private string RemoveAllHtmlTags(string html) diff --git a/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs b/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs index 83082d5..f47f518 100644 --- a/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs +++ b/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs @@ -3,6 +3,7 @@ using LyricsScraperNET.Network; using LyricsScraperNET.Providers.Abstract; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using System; using System.Threading; @@ -43,13 +44,13 @@ public AZLyricsProvider(ILogger logger, IOptionsSnapshot.Instance, options) { Ensure.ArgumentNotNull(options, nameof(options)); } public AZLyricsProvider(IOptionsSnapshot options) - : this(null, options.Value) + : this(NullLogger.Instance, options.Value) { Ensure.ArgumentNotNull(options, nameof(options)); } diff --git a/LyricsScraperNET/Providers/Abstract/ExternalProviderBase.cs b/LyricsScraperNET/Providers/Abstract/ExternalProviderBase.cs index e8153a6..6534d30 100644 --- a/LyricsScraperNET/Providers/Abstract/ExternalProviderBase.cs +++ b/LyricsScraperNET/Providers/Abstract/ExternalProviderBase.cs @@ -8,7 +8,7 @@ namespace LyricsScraperNET.Providers.Abstract { - public class ExternalProviderBase : IExternalProvider + public abstract class ExternalProviderBase : IExternalProvider { internal IExternalProviderLyricParser Parser { get; set; } internal IWebClient WebClient { get; set; } diff --git a/LyricsScraperNET/Providers/Genius/GeniusParser.cs b/LyricsScraperNET/Providers/Genius/GeniusParser.cs index c6f90d0..f720b4c 100644 --- a/LyricsScraperNET/Providers/Genius/GeniusParser.cs +++ b/LyricsScraperNET/Providers/Genius/GeniusParser.cs @@ -13,7 +13,7 @@ public string Parse(string lyric) lyric = CleanEnding(lyric); lyric = WebUtility.HtmlDecode(lyric); - return lyric?.Trim(); + return lyric?.Trim() ?? string.Empty; } public static string StripTagsRegex(string source) diff --git a/LyricsScraperNET/Providers/Genius/GeniusProvider.cs b/LyricsScraperNET/Providers/Genius/GeniusProvider.cs index 4178165..f5be658 100644 --- a/LyricsScraperNET/Providers/Genius/GeniusProvider.cs +++ b/LyricsScraperNET/Providers/Genius/GeniusProvider.cs @@ -7,6 +7,7 @@ using LyricsScraperNET.Network; using LyricsScraperNET.Providers.Abstract; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using System; using System.Linq; @@ -18,7 +19,7 @@ namespace LyricsScraperNET.Providers.Genius { public sealed class GeniusProvider : ExternalProviderBase { - private ILogger _logger; + private ILogger? _logger; private readonly IExternalUriConverter _uriConverter; // Format: "artist song". Example: "Parkway Drive Carrion". @@ -56,13 +57,13 @@ public GeniusProvider(ILogger logger, IOptionsSnapshot.Instance, options) { Ensure.ArgumentNotNull(options, nameof(options)); } public GeniusProvider(IOptionsSnapshot options) - : this(null, options.Value) + : this(NullLogger.Instance, options.Value) { Ensure.ArgumentNotNull(options, nameof(options)); } @@ -155,7 +156,7 @@ private string GetLyricUrlWithoutApiKey(string artist, string song, Cancellation if (hitJsonProperty.TryGetProperty("result", out var resultJsonElement)) { if (resultJsonElement.TryGetProperty("url", out var lyricUrl)) - return lyricUrl.GetString(); + return lyricUrl.GetString() ?? string.Empty; } } @@ -192,7 +193,7 @@ private string GetParsedLyricFromHtmlPageBody(string htmlPageBody, out bool inst _logger?.LogWarning($"Genius. Can't parse lyric from the page."); } - return Parser.Parse(string.Join("", lyricNodes.Select(node => node.InnerHtml))); + return Parser.Parse(string.Join("", lyricNodes!.Select(node => node.InnerHtml))); } private string GetLyricUrlFromSearchResponse(SearchResponse searchResponse, string artist, string song) diff --git a/LyricsScraperNET/Providers/IProviderService.cs b/LyricsScraperNET/Providers/IProviderService.cs index 7fbc48c..c44f0dc 100644 --- a/LyricsScraperNET/Providers/IProviderService.cs +++ b/LyricsScraperNET/Providers/IProviderService.cs @@ -9,7 +9,7 @@ namespace LyricsScraperNET.Providers public interface IProviderService { IEnumerable GetAvailableProviders(SearchRequest searchRequest); - IExternalProvider this[ExternalProviderType providerType] { get; } + IExternalProvider? this[ExternalProviderType providerType] { get; } bool AnyEnabled(); bool AnyAvailable(); bool IsProviderAvailable(ExternalProviderType providerType); diff --git a/LyricsScraperNET/Providers/LyricFind/LyricFindParser.cs b/LyricsScraperNET/Providers/LyricFind/LyricFindParser.cs index d4bd12e..d7fcb20 100644 --- a/LyricsScraperNET/Providers/LyricFind/LyricFindParser.cs +++ b/LyricsScraperNET/Providers/LyricFind/LyricFindParser.cs @@ -7,7 +7,8 @@ internal sealed class LyricFindParser : IExternalProviderLyricParser { public string Parse(string lyric) { - return UnescapeString(RemoveAllHtmlTags(lyric))?.Trim()?.Replace("\\n", "\r\n"); + return UnescapeString(RemoveAllHtmlTags(lyric))?.Trim()?.Replace("\\n", "\r\n") + ?? string.Empty; } private string RemoveAllHtmlTags(string html) diff --git a/LyricsScraperNET/Providers/LyricFind/LyricFindProvider.cs b/LyricsScraperNET/Providers/LyricFind/LyricFindProvider.cs index fe26eb2..7f8e462 100644 --- a/LyricsScraperNET/Providers/LyricFind/LyricFindProvider.cs +++ b/LyricsScraperNET/Providers/LyricFind/LyricFindProvider.cs @@ -4,6 +4,7 @@ using LyricsScraperNET.Network; using LyricsScraperNET.Providers.Abstract; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using System; using System.Text.RegularExpressions; @@ -14,7 +15,7 @@ namespace LyricsScraperNET.Providers.LyricFind { public sealed class LyricFindProvider : ExternalProviderBase { - private ILogger _logger; + private ILogger? _logger; private readonly IExternalUriConverter _uriConverter; /// @@ -57,13 +58,13 @@ public LyricFindProvider(ILogger logger, IOptionsSnapshot.Instance, options) { Ensure.ArgumentNotNull(options, nameof(options)); } public LyricFindProvider(IOptionsSnapshot options) - : this(null, options.Value) + : this(NullLogger.Instance, options.Value) { Ensure.ArgumentNotNull(options, nameof(options)); } diff --git a/LyricsScraperNET/Providers/Musixmatch/MusixmatchClientWrapper.cs b/LyricsScraperNET/Providers/Musixmatch/MusixmatchClientWrapper.cs index bb7f178..b6cccfe 100644 --- a/LyricsScraperNET/Providers/Musixmatch/MusixmatchClientWrapper.cs +++ b/LyricsScraperNET/Providers/Musixmatch/MusixmatchClientWrapper.cs @@ -12,11 +12,12 @@ namespace LyricsScraperNET.Providers.Musixmatch { public sealed class MusixmatchClientWrapper : IMusixmatchClientWrapper { - private ILogger _logger; + private ILogger? _logger; private IMusixmatchTokenCache _tokenCache; public MusixmatchClientWrapper() { + _tokenCache = new MusixmatchTokenCache(); } public MusixmatchClientWrapper(IMusixmatchTokenCache tokenCache) : this() diff --git a/LyricsScraperNET/Providers/Musixmatch/MusixmatchProvider.cs b/LyricsScraperNET/Providers/Musixmatch/MusixmatchProvider.cs index da4c527..8787967 100644 --- a/LyricsScraperNET/Providers/Musixmatch/MusixmatchProvider.cs +++ b/LyricsScraperNET/Providers/Musixmatch/MusixmatchProvider.cs @@ -2,6 +2,7 @@ using LyricsScraperNET.Models.Responses; using LyricsScraperNET.Providers.Abstract; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using MusixmatchClientLib.API.Model.Exceptions; using MusixmatchClientLib.API.Model.Types; @@ -13,7 +14,7 @@ namespace LyricsScraperNET.Providers.Musixmatch { public sealed class MusixmatchProvider : ExternalProviderBase { - private ILogger _logger; + private ILogger? _logger; private IMusixmatchClientWrapper _clientWrapper; private readonly int _searchRetryAmount = 2; @@ -45,13 +46,13 @@ public MusixmatchProvider(ILogger logger, IOptionsSnapshot.Instance, options, clientWrapper) { Ensure.ArgumentNotNull(options, nameof(options)); } public MusixmatchProvider(IOptionsSnapshot options, IMusixmatchClientWrapper clientWrapper) - : this(null, options.Value, clientWrapper) + : this(NullLogger.Instance, options.Value, clientWrapper) { Ensure.ArgumentNotNull(options, nameof(options)); } diff --git a/LyricsScraperNET/Providers/Musixmatch/MusixmatchTokenCache.cs b/LyricsScraperNET/Providers/Musixmatch/MusixmatchTokenCache.cs index 013b4d7..6158997 100644 --- a/LyricsScraperNET/Providers/Musixmatch/MusixmatchTokenCache.cs +++ b/LyricsScraperNET/Providers/Musixmatch/MusixmatchTokenCache.cs @@ -6,17 +6,23 @@ namespace LyricsScraperNET.Providers.Musixmatch { public sealed class MusixmatchTokenCache : IMusixmatchTokenCache { - private ILogger _logger; + private ILogger? _logger; // Musixmatch Token memory cache - private static IMemoryCache _memoryCache; - private static MemoryCacheEntryOptions _memoryCacheEntryOptions; + private static IMemoryCache? _memoryCache; + private static MemoryCacheEntryOptions? _memoryCacheEntryOptions; private static readonly object _syncLock = new object(); private readonly string MusixmatchTokenKey = "MusixmatchToken"; public MusixmatchTokenCache() + { + InitializeMemoryCache(); + InitializeMemoryCacheEntryOptions(); + } + + private void InitializeMemoryCache() { if (_memoryCache == null) { @@ -31,6 +37,10 @@ public MusixmatchTokenCache() } } } + } + + private void InitializeMemoryCacheEntryOptions() + { if (_memoryCacheEntryOptions == null) { lock (_syncLock) @@ -54,7 +64,7 @@ public MusixmatchTokenCache(ILogger logger) : this() public string GetOrCreateToken(bool regenerate = false) { if (regenerate) - _memoryCache.Remove(MusixmatchTokenKey); + _memoryCache!.Remove(MusixmatchTokenKey); _logger?.LogDebug("Musixmatch. Use default MusixmatchToken."); string musixmatchTokenValue; diff --git a/LyricsScraperNET/Providers/ProviderService.cs b/LyricsScraperNET/Providers/ProviderService.cs index 014cce0..52b1aaa 100644 --- a/LyricsScraperNET/Providers/ProviderService.cs +++ b/LyricsScraperNET/Providers/ProviderService.cs @@ -20,11 +20,11 @@ public IEnumerable GetAvailableProviders(SearchRequest search : _providers.Where(p => p.IsEnabled && p.Options.ExternalProviderType == providerType).OrderByDescending(p => p.SearchPriority); } - public IExternalProvider this[ExternalProviderType providerType] + public IExternalProvider? this[ExternalProviderType providerType] { get => IsProviderAvailable(providerType) - ? _providers.First(p => p.Options.ExternalProviderType == providerType) - : null; + ? _providers.FirstOrDefault(p => p.Options.ExternalProviderType == providerType) + : default; } public void AddProvider(IExternalProvider provider) @@ -55,6 +55,6 @@ public bool IsProviderAvailable(ExternalProviderType providerType) public bool IsProviderEnabled(ExternalProviderType providerType) => IsProviderAvailable(providerType) - && this[providerType].IsEnabled; + && (this[providerType]?.IsEnabled ?? false); } } diff --git a/LyricsScraperNET/Providers/SongLyrics/SongLyricsParser.cs b/LyricsScraperNET/Providers/SongLyrics/SongLyricsParser.cs index 86bb59d..1fb5238 100644 --- a/LyricsScraperNET/Providers/SongLyrics/SongLyricsParser.cs +++ b/LyricsScraperNET/Providers/SongLyrics/SongLyricsParser.cs @@ -9,7 +9,7 @@ public string Parse(string lyric) { lyric = WebUtility.HtmlDecode(lyric); - return lyric?.Trim(); + return lyric?.Trim() ?? string.Empty; } } } diff --git a/LyricsScraperNET/Providers/SongLyrics/SongLyricsProvider.cs b/LyricsScraperNET/Providers/SongLyrics/SongLyricsProvider.cs index 94cd818..da906a9 100644 --- a/LyricsScraperNET/Providers/SongLyrics/SongLyricsProvider.cs +++ b/LyricsScraperNET/Providers/SongLyrics/SongLyricsProvider.cs @@ -5,6 +5,7 @@ using LyricsScraperNET.Network; using LyricsScraperNET.Providers.Abstract; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using System; using System.Text.RegularExpressions; @@ -15,7 +16,7 @@ namespace LyricsScraperNET.Providers.SongLyrics { public sealed class SongLyricsProvider : ExternalProviderBase { - private ILogger _logger; + private ILogger? _logger; private readonly IExternalUriConverter _uriConverter; /// @@ -58,13 +59,13 @@ public SongLyricsProvider(ILogger logger, IOptionsSnapshot.Instance, options) { Ensure.ArgumentNotNull(options, nameof(options)); } public SongLyricsProvider(IOptionsSnapshot options) - : this(null, options.Value) + : this(NullLogger.Instance, options.Value) { Ensure.ArgumentNotNull(options, nameof(options)); }