Skip to content

Commit

Permalink
Refactoring. Fixed warning. Made some fields nullable. Return string.…
Browse files Browse the repository at this point in the history
…Empty instead of null.
  • Loading branch information
skuill committed Dec 25, 2024
1 parent b0937e8 commit 63a9986
Show file tree
Hide file tree
Showing 19 changed files with 65 additions and 40 deletions.
4 changes: 4 additions & 0 deletions LyricsScraperNET.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -167,4 +169,6 @@ ILyricsScraperClient lyricsScraperClient

return result;
}

#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
}
2 changes: 1 addition & 1 deletion LyricsScraperNET/ILyricsScraperClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface ILyricsScraperClient
/// </summary>
bool UseParallelSearch { get; set; }

IExternalProvider this[ExternalProviderType providerType] { get; }
IExternalProvider? this[ExternalProviderType providerType] { get; }

/// <summary>
/// Search lyric by different search requests:
Expand Down
14 changes: 9 additions & 5 deletions LyricsScraperNET/LyricsScraperClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ namespace LyricsScraperNET
{
public sealed class LyricsScraperClient : ILyricsScraperClient
{
private ILoggerFactory _loggerFactory;
private ILogger<LyricsScraperClient> _logger;
private ILoggerFactory? _loggerFactory;
private ILogger<LyricsScraperClient>? _logger;

private IProviderService _providerService;
private IRequestValidator _requestValidator;
private readonly ILyricScraperClientConfig _lyricScraperClientConfig;
private readonly ILyricScraperClientConfig? _lyricScraperClientConfig;

private bool? _useParallelSearch;
/// <inheritdoc />
Expand All @@ -37,7 +37,7 @@ public bool UseParallelSearch
public bool IsEnabled => _providerService.AnyEnabled();

/// <inheritdoc />
public IExternalProvider this[ExternalProviderType providerType]
public IExternalProvider? this[ExternalProviderType providerType]
{
get => _providerService[providerType];
}
Expand Down Expand Up @@ -221,6 +221,10 @@ public void RemoveProvider(ExternalProviderType providerType)
public void Disable() => _providerService.DisableAllProviders();

/// <inheritdoc />
public void WithLogger(ILoggerFactory loggerFactory) => _providerService.WithLogger(loggerFactory);
public void WithLogger(ILoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory;
_providerService.WithLogger(loggerFactory);
}
}
}
4 changes: 2 additions & 2 deletions LyricsScraperNET/Network/HtmlAgilityWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace LyricsScraperNET.Network
/// </summary>
internal sealed class HtmlAgilityWebClient : IWebClient
{
private readonly ILogger<HtmlAgilityWebClient> _logger;
private readonly ILogger<HtmlAgilityWebClient>? _logger;
private readonly HtmlWeb _htmlWeb;

public HtmlAgilityWebClient()
Expand Down Expand Up @@ -49,7 +49,7 @@ public async Task<string> LoadAsync(Uri uri, CancellationToken cancellationToken
}

CheckDocument(document, uri);
return document?.ParsedText;
return document?.ParsedText ?? string.Empty;
}

private void CheckDocument(HtmlDocument document, Uri uri)
Expand Down
2 changes: 1 addition & 1 deletion LyricsScraperNET/Network/NetHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace LyricsScraperNET.Network
/// </summary>
internal sealed class NetHttpClient : IWebClient
{
private readonly ILogger<NetHttpClient> _logger;
private readonly ILogger<NetHttpClient>? _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.
Expand Down
2 changes: 1 addition & 1 deletion LyricsScraperNET/Providers/AZLyrics/AZLyricsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,13 +44,13 @@ public AZLyricsProvider(ILogger<AZLyricsProvider> logger, IOptionsSnapshot<AZLyr
}

public AZLyricsProvider(AZLyricsOptions options)
: this(null, options)
: this(NullLogger<AZLyricsProvider>.Instance, options)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

public AZLyricsProvider(IOptionsSnapshot<AZLyricsOptions> options)
: this(null, options.Value)
: this(NullLogger<AZLyricsProvider>.Instance, options.Value)
{
Ensure.ArgumentNotNull(options, nameof(options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 1 addition & 1 deletion LyricsScraperNET/Providers/Genius/GeniusParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions LyricsScraperNET/Providers/Genius/GeniusProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +19,7 @@ namespace LyricsScraperNET.Providers.Genius
{
public sealed class GeniusProvider : ExternalProviderBase
{
private ILogger<GeniusProvider> _logger;
private ILogger<GeniusProvider>? _logger;
private readonly IExternalUriConverter _uriConverter;

// Format: "artist song". Example: "Parkway Drive Carrion".
Expand Down Expand Up @@ -56,13 +57,13 @@ public GeniusProvider(ILogger<GeniusProvider> logger, IOptionsSnapshot<GeniusOpt
}

public GeniusProvider(GeniusOptions options)
: this(null, options)
: this(NullLogger<GeniusProvider>.Instance, options)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

public GeniusProvider(IOptionsSnapshot<GeniusOptions> options)
: this(null, options.Value)
: this(NullLogger<GeniusProvider>.Instance, options.Value)
{
Ensure.ArgumentNotNull(options, nameof(options));
}
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion LyricsScraperNET/Providers/IProviderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace LyricsScraperNET.Providers
public interface IProviderService
{
IEnumerable<IExternalProvider> GetAvailableProviders(SearchRequest searchRequest);
IExternalProvider this[ExternalProviderType providerType] { get; }
IExternalProvider? this[ExternalProviderType providerType] { get; }
bool AnyEnabled();
bool AnyAvailable();
bool IsProviderAvailable(ExternalProviderType providerType);
Expand Down
3 changes: 2 additions & 1 deletion LyricsScraperNET/Providers/LyricFind/LyricFindParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions LyricsScraperNET/Providers/LyricFind/LyricFindProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +15,7 @@ namespace LyricsScraperNET.Providers.LyricFind
{
public sealed class LyricFindProvider : ExternalProviderBase
{
private ILogger<LyricFindProvider> _logger;
private ILogger<LyricFindProvider>? _logger;
private readonly IExternalUriConverter _uriConverter;

/// <summary>
Expand Down Expand Up @@ -57,13 +58,13 @@ public LyricFindProvider(ILogger<LyricFindProvider> logger, IOptionsSnapshot<Lyr
}

public LyricFindProvider(LyricFindOptions options)
: this(null, options)
: this(NullLogger<LyricFindProvider>.Instance, options)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

public LyricFindProvider(IOptionsSnapshot<LyricFindOptions> options)
: this(null, options.Value)
: this(NullLogger<LyricFindProvider>.Instance, options.Value)
{
Ensure.ArgumentNotNull(options, nameof(options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ namespace LyricsScraperNET.Providers.Musixmatch
{
public sealed class MusixmatchClientWrapper : IMusixmatchClientWrapper
{
private ILogger<MusixmatchClientWrapper> _logger;
private ILogger<MusixmatchClientWrapper>? _logger;
private IMusixmatchTokenCache _tokenCache;

public MusixmatchClientWrapper()
{
_tokenCache = new MusixmatchTokenCache();
}

public MusixmatchClientWrapper(IMusixmatchTokenCache tokenCache) : this()
Expand Down
7 changes: 4 additions & 3 deletions LyricsScraperNET/Providers/Musixmatch/MusixmatchProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +14,7 @@ namespace LyricsScraperNET.Providers.Musixmatch
{
public sealed class MusixmatchProvider : ExternalProviderBase
{
private ILogger<MusixmatchProvider> _logger;
private ILogger<MusixmatchProvider>? _logger;
private IMusixmatchClientWrapper _clientWrapper;

private readonly int _searchRetryAmount = 2;
Expand Down Expand Up @@ -45,13 +46,13 @@ public MusixmatchProvider(ILogger<MusixmatchProvider> logger, IOptionsSnapshot<M
}

public MusixmatchProvider(MusixmatchOptions options, IMusixmatchClientWrapper clientWrapper)
: this(null, options, clientWrapper)
: this(NullLogger<MusixmatchProvider>.Instance, options, clientWrapper)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

public MusixmatchProvider(IOptionsSnapshot<MusixmatchOptions> options, IMusixmatchClientWrapper clientWrapper)
: this(null, options.Value, clientWrapper)
: this(NullLogger<MusixmatchProvider>.Instance, options.Value, clientWrapper)
{
Ensure.ArgumentNotNull(options, nameof(options));
}
Expand Down
18 changes: 14 additions & 4 deletions LyricsScraperNET/Providers/Musixmatch/MusixmatchTokenCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ namespace LyricsScraperNET.Providers.Musixmatch
{
public sealed class MusixmatchTokenCache : IMusixmatchTokenCache
{
private ILogger<MusixmatchTokenCache> _logger;
private ILogger<MusixmatchTokenCache>? _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)
{
Expand All @@ -31,6 +37,10 @@ public MusixmatchTokenCache()
}
}
}
}

private void InitializeMemoryCacheEntryOptions()
{
if (_memoryCacheEntryOptions == null)
{
lock (_syncLock)
Expand All @@ -54,7 +64,7 @@ public MusixmatchTokenCache(ILogger<MusixmatchTokenCache> logger) : this()
public string GetOrCreateToken(bool regenerate = false)
{
if (regenerate)
_memoryCache.Remove(MusixmatchTokenKey);
_memoryCache!.Remove(MusixmatchTokenKey);

_logger?.LogDebug("Musixmatch. Use default MusixmatchToken.");
string musixmatchTokenValue;
Expand Down
8 changes: 4 additions & 4 deletions LyricsScraperNET/Providers/ProviderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public IEnumerable<IExternalProvider> 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)
Expand Down Expand Up @@ -55,6 +55,6 @@ public bool IsProviderAvailable(ExternalProviderType providerType)

public bool IsProviderEnabled(ExternalProviderType providerType)
=> IsProviderAvailable(providerType)
&& this[providerType].IsEnabled;
&& (this[providerType]?.IsEnabled ?? false);
}
}
2 changes: 1 addition & 1 deletion LyricsScraperNET/Providers/SongLyrics/SongLyricsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public string Parse(string lyric)
{
lyric = WebUtility.HtmlDecode(lyric);

return lyric?.Trim();
return lyric?.Trim() ?? string.Empty;
}
}
}
Loading

0 comments on commit 63a9986

Please sign in to comment.