Skip to content

Commit 79c5455

Browse files
committed
Fixed LyricsFreakUriConverter logic. Simlify SearchLyricAsync in LyricsFreakProvider
1 parent e448c3a commit 79c5455

11 files changed

Lines changed: 82 additions & 58 deletions

File tree

LyricsScraperNET/Extensions/StringExtensions.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ public static string CreateCombinedUrlSlug(string artist, string songTitle)
8888
case ' ':
8989
slug += '-';
9090
break;
91-
case >= 'a' and <= 'z':
92-
case >= 'A' and <= 'Z':
93-
case >= '0' and <= '9':
94-
slug += c;
95-
break;
9691
case '-':
9792
slug += '-';
9893
break;
94+
default:
95+
if (IsLatinAlphabetOrDigit(c))
96+
slug += c;
97+
break;
9998
}
10099
}
101100

@@ -105,24 +104,29 @@ public static string CreateCombinedUrlSlug(string artist, string songTitle)
105104
return slug.ToLower();
106105
}
107106

108-
public static string СonvertSpaceToPlusFormat(this string input, bool removeProhibitedSymbols = false)
107+
public static string СonvertToPlusFormat(this string input, bool removeProhibitedSymbols = false)
109108
{
110109
if (string.IsNullOrWhiteSpace(input))
111110
return input;
112111

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

115114
if (removeProhibitedSymbols)
116-
result = new string(result.Where(x => char.IsLetterOrDigit(x) || char.IsWhiteSpace(x) ).ToArray());
115+
result = new string(result.Where(x => IsLatinAlphabetOrDigit(x) || char.IsWhiteSpace(x) || x == '-').ToArray());
117116

118117
result = Regex.Replace(new string(result.Select(x =>
119118
{
120-
return (char.IsWhiteSpace(x))
119+
return (char.IsWhiteSpace(x) || x == '-')
121120
? '+'
122121
: x;
123-
}).ToArray()), "\\++", "+").Trim('+');
122+
}).ToArray()), "\\++", "+");
124123

125124
return result;
126125
}
126+
127+
private static bool IsLatinAlphabetOrDigit(char letter) =>
128+
(letter >= 'a' && letter <= 'z')
129+
|| (letter >= 'A' && letter <= 'Z')
130+
|| (letter >= '0' && letter <= '9');
127131
}
128132
}

LyricsScraperNET/Providers/AZLyrics/AZLyricsUriConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ internal sealed class AZLyricsUriConverter : IExternalUriConverter
88
{
99
private Uri _baseUri => new Uri("http://www.azlyrics.com/lyrics/");
1010

11+
public Uri GetArtistUri(string artist)
12+
{
13+
throw new NotImplementedException();
14+
}
15+
1116
public Uri GetLyricUri(string artist, string song)
1217
{
1318
// remove articles from artist on start. For example for band [The Devil Wears Prada]: https://www.azlyrics.com/d/devilwearsprada.html

LyricsScraperNET/Providers/Abstract/IExternalUriConverter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ namespace LyricsScraperNET.Providers.Abstract
55
internal interface IExternalUriConverter
66
{
77
Uri GetLyricUri(string artist, string song);
8+
9+
Uri GetArtistUri(string artist);
810
}
911
}

LyricsScraperNET/Providers/Genius/GeniusUriConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ private string GetApiSearchQuery(string artist, string song)
1414

1515
public Uri GetLyricUri(string artist, string song)
1616
=> new Uri(string.Format(GeniusApiSearchFormat, GetApiSearchQuery(artist, song)));
17+
18+
public Uri GetArtistUri(string artist)
19+
{
20+
throw new NotImplementedException();
21+
}
1722
}
1823
}

LyricsScraperNET/Providers/KPopLyrics/KPopLyricsUriConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public class KPopLyricsUriConverter : IExternalUriConverter
88
{
99
private Uri _baseUri => new Uri("https://www.kpoplyrics.net/");
1010

11+
public Uri GetArtistUri(string artist)
12+
{
13+
throw new NotImplementedException();
14+
}
15+
1116
public Uri GetLyricUri(string artist, string song)
1217
{
1318
return new Uri(_baseUri, $"{StringExtensions.CreateCombinedUrlSlug(artist, song)}.html");

LyricsScraperNET/Providers/LyricFind/LyricFindUriConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ internal sealed class LyricFindUriConverter : IExternalUriConverter
99
// 0 - artist, 1 - song
1010
private const string uriPathFormat = "https://lyrics.lyricfind.com/lyrics/{0}-{1}";
1111

12+
public Uri GetArtistUri(string artist)
13+
{
14+
throw new NotImplementedException();
15+
}
16+
1217
public Uri GetLyricUri(string artist, string song)
1318
{
1419
var artistFormatted = artist.ToLowerInvariant().СonvertToDashedFormat(useExceptionSymbols: false, removeProhibitedSymbols: true);

LyricsScraperNET/Providers/LyricsFreak/LyricsFreakProvider.cs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,46 +77,29 @@ protected override SearchResult SearchLyric(Uri uri, CancellationToken cancellat
7777

7878
protected override async Task<SearchResult> SearchLyricAsync(string artist, string song, CancellationToken cancellationToken = default)
7979
{
80-
try
80+
if (WebClient == null || Parser == null)
8181
{
82-
var artistUri = _uriConverter.GetLyricUri(artist, song);
83-
84-
if (WebClient == null || Parser == null)
85-
{
86-
_logger?.LogWarning($"LyricsFreak. Please set up WebClient and Parser first");
87-
return new SearchResult(Models.ExternalProviderType.LyricsFreak);
88-
}
89-
90-
var htmlResponse = await WebClient.LoadAsync(artistUri, cancellationToken);
91-
92-
cancellationToken.ThrowIfCancellationRequested();
93-
94-
var songHref = GetSongHrefFromHtmlBody(htmlResponse, song);
95-
if (string.IsNullOrEmpty(songHref))
96-
{
97-
_logger?.LogWarning($"LyricsFreak. Can't find song Uri for song: [{song}]");
98-
return new SearchResult(Models.ExternalProviderType.LyricsFreak);
99-
}
100-
101-
var songUri = new Uri(LyricsFreakUriConverter.BaseUrl + songHref);
82+
_logger?.LogWarning($"LyricsFreak. Please set up WebClient and Parser first");
83+
return new SearchResult(Models.ExternalProviderType.LyricsFreak);
84+
}
10285

103-
var songUriSearchResult = await SearchLyricAsync(songUri, cancellationToken);
86+
// 1. Open the artist's page.
87+
var artistUri = _uriConverter.GetArtistUri(artist);
10488

105-
cancellationToken.ThrowIfCancellationRequested();
89+
var htmlResponse = await WebClient.LoadAsync(artistUri, cancellationToken);
10690

107-
if (songUriSearchResult is null || string.IsNullOrEmpty(songUriSearchResult?.LyricText))
108-
{
109-
_logger?.LogWarning($"LyricsFreak. Can't find song lyrics for song : [{song}]");
110-
return new SearchResult(Models.ExternalProviderType.LyricsFreak);
111-
}
91+
cancellationToken.ThrowIfCancellationRequested();
11292

113-
return new SearchResult(songUriSearchResult!.LyricText, Models.ExternalProviderType.LyricsFreak);
114-
}
115-
catch (Exception ex)
93+
// 2. Find song on the artist page and get link to the web page.
94+
var songHref = GetSongHrefFromHtmlBody(htmlResponse, song);
95+
if (string.IsNullOrEmpty(songHref))
11696
{
117-
_logger?.LogError(ex, $"LyricsFreak. Error searching for lyrics for artist: [{artist}], song: [{song}]");
97+
_logger?.LogWarning($"LyricsFreak. Can't find song Uri for song: [{song}]");
11898
return new SearchResult(Models.ExternalProviderType.LyricsFreak);
11999
}
100+
var songUri = new Uri(LyricsFreakUriConverter.BaseUrl + songHref);
101+
102+
return await SearchLyricAsync(songUri, cancellationToken);
120103
}
121104

122105
protected async override Task<SearchResult> SearchLyricAsync(Uri uri, CancellationToken cancellationToken = default)
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
using LyricsScraperNET.Extensions;
22
using LyricsScraperNET.Providers.Abstract;
33
using System;
4+
using System.Linq;
45

56
namespace LyricsScraperNET.Providers.LyricsFreak
67
{
78
internal sealed class LyricsFreakUriConverter : IExternalUriConverter
89
{
910
public const string BaseUrl = "https://www.lyricsfreak.com";
1011

11-
// 0 - artist, 1 - song
12+
// 0 - artist subgroup (first latin letter in artist's name), 1 - artist name
1213
private const string uriArtistPathFormat = BaseUrl + "/{0}/{1}";
1314

14-
public Uri GetLyricUri(string artist, string song)
15+
// Example for Artist parkway drive https://www.lyricsfreak.com/p/parkway+drive/
16+
public Uri GetArtistUri(string artist)
1517
{
16-
var artistFormatted = artist.ToLowerInvariant().СonvertSpaceToPlusFormat(removeProhibitedSymbols: true);
17-
return GetArtistUri(artistFormatted);
18+
var artistFormatted = artist.ToLowerInvariant().СonvertToPlusFormat(removeProhibitedSymbols: true);
19+
return new Uri(string.Format(uriArtistPathFormat, artistFormatted.First(c => c != '+'), artistFormatted));
20+
1821
}
1922

20-
// Example for Artist parkway drive https://www.lyricsfreak.com/p/parkway+drive/
21-
private static Uri GetArtistUri(string artist)
23+
public Uri GetLyricUri(string artist, string song)
2224
{
23-
return new Uri(string.Format(uriArtistPathFormat, artist.Length > 0 ? artist[0] : string.Empty, artist));
24-
25+
throw new NotImplementedException();
2526
}
2627
}
2728
}

LyricsScraperNET/Providers/SongLyrics/SongLyricsUriConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ internal sealed class SongLyricsUriConverter : IExternalUriConverter
99
// 0 - artist, 1 - song
1010
private const string uriPathFormat = "https://www.songlyrics.com/{0}/{1}-lyrics/";
1111

12+
public Uri GetArtistUri(string artist)
13+
{
14+
throw new NotImplementedException();
15+
}
16+
1217
public Uri GetLyricUri(string artist, string song)
1318
{
1419
var artistFormatted = artist.ToLowerInvariant().СonvertToDashedFormat();

Tests/LyricsScraperNET.UnitTest/Extensions/StringExtensionsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void GenerateCombinedUrlSlug_Tests(string artist, string title, string ex
8484
public void СonvertToPlusFormat_MultipleInputs_ShouldBeParse(string input, string expected)
8585
{
8686
// Act
87-
var actual = StringExtensions.СonvertSpaceToPlusFormat(input, true);
87+
var actual = StringExtensions.СonvertToPlusFormat(input, true);
8888

8989
// Assert
9090
Assert.Equal(expected, actual);

0 commit comments

Comments
 (0)