Skip to content

Commit

Permalink
#13 Add LyricFind external provider. #6 Add LyrciScraperClient UnitTests
Browse files Browse the repository at this point in the history
  • Loading branch information
skuill committed Dec 9, 2023
1 parent 9df1926 commit 496ee37
Show file tree
Hide file tree
Showing 42 changed files with 868 additions and 134 deletions.
4 changes: 3 additions & 1 deletion LyricsScraperNET.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using LyricsScraperNET.Providers.AZLyrics;
using LyricsScraperNET.Providers.Genius;
using LyricsScraperNET.Providers.SongLyrics;
using LyricsScraperNET.Providers.LyricFind;
using LyricsScraperNET.Models.Requests;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -101,7 +102,8 @@ ILyricsScraperClient lyricsScraperClient
.WithGenius()
.WithAZLyrics()
.WithMusixmatch()
.WithSongLyrics();
.WithSongLyrics()
.WithLyricFind();

//// Another way to configure:
//// 1. First create instance of LyricScraperClient.
Expand Down
8 changes: 6 additions & 2 deletions LyricsScraperNET.Client/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"LyricScraperClient": {
"GeniusOptions": {
"SearchPriority": 2,
"SearchPriority": 3,
"Enabled": true,
"ApiKey": ""
},
"AZLyricsOptions": {
"SearchPriority": 3,
"SearchPriority": 4,
"Enabled": true
},
"MusixmatchOptions": {
Expand All @@ -17,6 +17,10 @@
"SongLyricsOptions": {
"SearchPriority": 0,
"Enabled": true
},
"LyricFindOptions": {
"SearchPriority": 2,
"Enabled": true
}
}
}
2 changes: 2 additions & 0 deletions LyricsScraperNET/Configuration/ILyricScraperClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public interface ILyricScraperClientConfig
IExternalProviderOptions MusixmatchOptions { get; }

IExternalProviderOptions SongLyricsOptions { get; }

IExternalProviderOptions LyricFindOptions { get; }
}
}
6 changes: 5 additions & 1 deletion LyricsScraperNET/Configuration/LyricScraperClientConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using LyricsScraperNET.Providers.Abstract;
using LyricsScraperNET.Providers.AZLyrics;
using LyricsScraperNET.Providers.Genius;
using LyricsScraperNET.Providers.LyricFind;
using LyricsScraperNET.Providers.Musixmatch;
using LyricsScraperNET.Providers.SongLyrics;

Expand All @@ -18,9 +19,12 @@ public sealed class LyricScraperClientConfig : ILyricScraperClientConfig

public IExternalProviderOptions SongLyricsOptions { get; set; } = new SongLyricsOptions();

public IExternalProviderOptions LyricFindOptions { get; set; } = new LyricFindOptions();

public bool IsEnabled => AZLyricsOptions.Enabled
|| GeniusOptions.Enabled
|| MusixmatchOptions.Enabled
|| SongLyricsOptions.Enabled;
|| SongLyricsOptions.Enabled
|| LyricFindOptions.Enabled;
}
}
17 changes: 17 additions & 0 deletions LyricsScraperNET/Configuration/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using LyricsScraperNET.Providers.Abstract;
using LyricsScraperNET.Providers.AZLyrics;
using LyricsScraperNET.Providers.Genius;
using LyricsScraperNET.Providers.LyricFind;
using LyricsScraperNET.Providers.Musixmatch;
using LyricsScraperNET.Providers.SongLyrics;
using Microsoft.Extensions.Configuration;
Expand All @@ -22,6 +23,7 @@ public static IServiceCollection AddLyricScraperClientService(
services.AddGeniusClientService(lyricScraperClientConfig);
services.AddMusixmatchService(lyricScraperClientConfig);
services.AddSongLyricsService(lyricScraperClientConfig);
services.AddLyricFindService(lyricScraperClientConfig);

services.Configure<LyricScraperClientConfig>(lyricScraperClientConfig);
services.AddScoped<ILyricScraperClientConfig>(x => x.GetRequiredService<IOptionsSnapshot<LyricScraperClientConfig>>().Value);
Expand Down Expand Up @@ -91,5 +93,20 @@ public static IServiceCollection AddSongLyricsService(

return services;
}

public static IServiceCollection AddLyricFindService(
this IServiceCollection services,
IConfiguration configuration)
{
var configurationSection = configuration.GetSection(LyricFindOptions.ConfigurationSectionName);
if (configurationSection.Exists())
{
services.Configure<LyricFindOptions>(configurationSection);

services.AddScoped(typeof(IExternalProvider), typeof(LyricFindProvider));
}

return services;
}
}
}
7 changes: 7 additions & 0 deletions LyricsScraperNET/Extensions/LyricsScraperClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LyricsScraperNET.Providers.AZLyrics;
using LyricsScraperNET.Providers.Genius;
using LyricsScraperNET.Providers.LyricFind;
using LyricsScraperNET.Providers.Musixmatch;
using LyricsScraperNET.Providers.SongLyrics;

Expand Down Expand Up @@ -30,5 +31,11 @@ public static ILyricsScraperClient WithSongLyrics(this ILyricsScraperClient lyri
lyricsScraperClient.AddProvider(new SongLyricsProvider());
return lyricsScraperClient;
}

public static ILyricsScraperClient WithLyricFind(this ILyricsScraperClient lyricsScraperClient)
{
lyricsScraperClient.AddProvider(new LyricFindProvider());
return lyricsScraperClient;
}
}
}
24 changes: 20 additions & 4 deletions LyricsScraperNET/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace LyricsScraperNET.Extensions
{
Expand All @@ -12,7 +13,9 @@ public static string RemoveHtmlTags(this string text)
{
if (string.IsNullOrWhiteSpace(text))
return text;

int idx = text.IndexOf('<');

while (idx >= 0)
{
var endIdx = text.IndexOf('>', idx + 1);
Expand All @@ -23,14 +26,17 @@ public static string RemoveHtmlTags(this string text)
text = text.Remove(idx, endIdx - idx + 1);
idx = text.IndexOf('<', idx);
}

return text;
}

public static string StripRedundantChars(this string input, bool removeArticle = false)
{
if (string.IsNullOrWhiteSpace(input))
return input;

var result = input.ToLowerInvariant().Trim();

if (removeArticle)
foreach (var article in ARTICLES)
{
Expand All @@ -40,19 +46,29 @@ public static string StripRedundantChars(this string input, bool removeArticle =
break;
}
}

result = new string(result.Where(c => char.IsLetterOrDigit(c)).ToArray());

return result;
}

public static string СonvertToDashedFormat(this string input, bool useExceptionSymbols = true)
public static string СonvertToDashedFormat(this string input, bool useExceptionSymbols = true, bool removeProhibitedSymbols = false)
{
if (string.IsNullOrWhiteSpace(input))
return input;

var result = input.ToLowerInvariant().Trim();
result = new string(result.Select(x =>

if (removeProhibitedSymbols)
result = new string(result.Where(x => char.IsLetterOrDigit(x) || char.IsWhiteSpace(x) || x == '-' || x == '.').ToArray());

result = Regex.Replace(new string(result.Select(x =>
{
return (char.IsLetterOrDigit(x) || (useExceptionSymbols && EXCEPTION_SYMBOLS.Contains(x))) ? x : '-';
}).ToArray()).Replace("--", "-").Trim('-');
return (char.IsLetterOrDigit(x) || (useExceptionSymbols && EXCEPTION_SYMBOLS.Contains(x)))
? x
: '-';
}).ToArray()), "-+", "-").Trim('-');

return result;
}
}
Expand Down
35 changes: 35 additions & 0 deletions LyricsScraperNET/ILyricsScraperClient.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
using LyricsScraperNET.Models.Requests;
using LyricsScraperNET.Models.Responses;
using LyricsScraperNET.Providers.Abstract;
using LyricsScraperNET.Providers.Models;
using System.Threading.Tasks;

namespace LyricsScraperNET
{
public interface ILyricsScraperClient
{
/// <summary>
/// Checking that there is some enabled external provider available for search.
/// </summary>
bool IsEnabled { get; }

IExternalProvider this[ExternalProviderType providerType] { get; }

/// <summary>
/// Search lyric by different search requests:
/// 1) Search by Uri: <see cref="UriSearchRequest"/>
/// 2) Search by Artist and Song name: <see cref="ArtistAndSongSearchRequest"/>
/// </summary>
SearchResult SearchLyric(SearchRequest searchRequest);

/// <summary>
/// Async search lyric by different search requests:
/// 1) Search by Uri: <see cref="UriSearchRequest"/>
/// 2) Search by Artist and Song name: <see cref="ArtistAndSongSearchRequest"/>
/// </summary>
Task<SearchResult> SearchLyricAsync(SearchRequest searchRequest);

/// <summary>
/// Adding a new external provider that will be used to search for lyrics.
/// </summary>
void AddProvider(IExternalProvider provider);

/// <summary>
/// Removing an external provider by <paramref name="providerType"/> from the list of search providers.
/// </summary>
void RemoveProvider(ExternalProviderType providerType);

/// <summary>
/// Enable lyrics search. All external providers will be enabled in this case.
/// </summary>
void Enable();

/// <summary>
/// Disable lyrics search. All external providers will be enabled in this case.
/// Calling the lyrics search method will return an empty result.
/// </summary>
void Disable();
}
}
42 changes: 40 additions & 2 deletions LyricsScraperNET/LyricsScraperClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LyricsScraperNET.Models.Requests;
using LyricsScraperNET.Models.Responses;
using LyricsScraperNET.Providers.Abstract;
using LyricsScraperNET.Providers.Models;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -15,11 +16,18 @@ public sealed class LyricsScraperClient : ILyricsScraperClient

private readonly ILogger<LyricsScraperClient> _logger;

private IList<IExternalProvider> _externalProviders;
private List<IExternalProvider> _externalProviders;
private readonly ILyricScraperClientConfig _lyricScraperClientConfig;

public bool IsEnabled => _externalProviders != null && _externalProviders.Any(x => x.IsEnabled);

public IExternalProvider this[ExternalProviderType providerType]
{
get => !IsEmptyProviders()
? _externalProviders.FirstOrDefault(p => p.Options.ExternalProviderType == providerType)
: null;
}

public LyricsScraperClient() { }

public LyricsScraperClient(ILyricScraperClientConfig lyricScraperClientConfig,
Expand Down Expand Up @@ -88,7 +96,7 @@ private bool ValidateRequest()
else if (!IsEnabled)
{
error = "All external providers is disabled. Searching lyrics is disabled.";
logLevel = LogLevel.Warning;
logLevel = LogLevel.Debug;
}

if (!string.IsNullOrWhiteSpace(error))
Expand All @@ -110,5 +118,35 @@ public void AddProvider(IExternalProvider provider)
}

private bool IsEmptyProviders() => _externalProviders == null || !_externalProviders.Any();

public void RemoveProvider(ExternalProviderType providerType)
{
if (IsEmptyProviders())
return;

_externalProviders.RemoveAll(x => x.Options.ExternalProviderType == providerType);
}

public void Enable()
{
if (IsEmptyProviders())
return;

foreach (var provider in _externalProviders)
{
provider.Enable();
}
}

public void Disable()
{
if (IsEmptyProviders())
return;

foreach (var provider in _externalProviders)
{
provider.Disable();
}
}
}
}
2 changes: 1 addition & 1 deletion LyricsScraperNET/Providers/AZLyrics/AZLyricsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class AZLyricsOptions : IExternalProviderOptions

public ExternalProviderType ExternalProviderType => ExternalProviderType.AZLyrics;

public int SearchPriority { get; set; } = 3;
public int SearchPriority { get; set; } = 4;

public const string ConfigurationSectionName = "AZLyricsOptions";

Expand Down
Loading

0 comments on commit 496ee37

Please sign in to comment.