Skip to content

Commit 43609bc

Browse files
committed
Refactoring. DRY in parsers logic
1 parent 68858ed commit 43609bc

8 files changed

Lines changed: 53 additions & 79 deletions

File tree

LyricsScraperNET/Extensions/HtmlExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,38 @@ public static class HtmlExtensions
1010
htmlDoc.LoadHtml(htmlBody);
1111
return htmlDoc.DocumentNode.SelectSingleNode(xPath);
1212
}
13+
14+
public static string RemoveAllHtmlTags(this string html)
15+
{
16+
html = html.RemoveHtmlTags();
17+
18+
// fix recursive white-spaces
19+
while (html.Contains(" "))
20+
{
21+
html = html.Replace(" ", " ");
22+
}
23+
24+
// fix recursive line-break
25+
while (html.Contains("\r\n\r\n\r\n"))
26+
{
27+
html = html.Replace("\r\n\r\n\r\n", "\r\n\r\n");
28+
}
29+
30+
return html;
31+
}
32+
33+
public static string UnescapeString(this string html)
34+
{
35+
if (!string.IsNullOrEmpty(html))
36+
{
37+
// replace entities with literal values
38+
html = html.Replace("'", "'");
39+
html = html.Replace(""", "\"");
40+
html = html.Replace(">", ">");
41+
html = html.Replace("&lt;", "<");
42+
html = html.Replace("&amp;", "&");
43+
}
44+
return html;
45+
}
1346
}
1447
}

LyricsScraperNET/Providers/AZLyrics/AZLyricsParser.cs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,10 @@ internal sealed class AZLyricsParser : IExternalProviderLyricParser
77
{
88
public string Parse(string lyric)
99
{
10-
return UnescapeString(RemoveAllHtmlTags(lyric))?.Trim() ?? string.Empty;
11-
}
12-
13-
private string RemoveAllHtmlTags(string html)
14-
{
15-
html = html.RemoveHtmlTags();
16-
17-
// fix recursive white-spaces
18-
while (html.Contains(" "))
19-
{
20-
html = html.Replace(" ", " ");
21-
}
22-
23-
// fix recursive line-break
24-
while (html.Contains("\r\n\r\n\r\n"))
25-
{
26-
html = html.Replace("\r\n\r\n\r\n", "\r\n\r\n");
27-
}
28-
29-
return html;
30-
}
31-
32-
private string UnescapeString(string lyric)
33-
{
34-
if (!string.IsNullOrEmpty(lyric))
35-
{
36-
// replace entities with literal values
37-
lyric = lyric.Replace("&apos;", "'");
38-
lyric = lyric.Replace("&quot;", "\"");
39-
lyric = lyric.Replace("&gt;", ">");
40-
lyric = lyric.Replace("&lt;", "<");
41-
lyric = lyric.Replace("&amp;", "&");
42-
}
43-
return lyric;
10+
return lyric.RemoveAllHtmlTags()
11+
.UnescapeString()?
12+
.Trim()
13+
?? string.Empty;
4414
}
4515
}
4616
}

LyricsScraperNET/Providers/KPopLyrics/KPopLyricsOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace LyricsScraperNET.Providers.KPopLyrics
66
{
7-
public class KPopLyricsOptions : IExternalProviderOptions
7+
public sealed class KPopLyricsOptions : IExternalProviderOptions
88
{
99
public ExternalProviderType ExternalProviderType => ExternalProviderType.KPopLyrics;
1010

LyricsScraperNET/Providers/KPopLyrics/KPopLyricsParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace LyricsScraperNET.Providers.KPopLyrics
77
{
8-
public class KPopLyricsParser : IExternalProviderLyricParser
8+
internal sealed class KPopLyricsParser : IExternalProviderLyricParser
99
{
1010
public string Parse(string lyric)
1111
{

LyricsScraperNET/Providers/KPopLyrics/KPopLyricsUriConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace LyricsScraperNET.Providers.KPopLyrics
66
{
7-
public class KPopLyricsUriConverter : IExternalUriConverter
7+
internal sealed class KPopLyricsUriConverter : IExternalUriConverter
88
{
99
private Uri _baseUri => new Uri("https://www.kpoplyrics.net/");
1010

LyricsScraperNET/Providers/LyricFind/LyricFindParser.cs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,11 @@ internal sealed class LyricFindParser : IExternalProviderLyricParser
77
{
88
public string Parse(string lyric)
99
{
10-
return UnescapeString(RemoveAllHtmlTags(lyric))?.Trim()?.Replace("\\n", "\r\n")
11-
?? string.Empty;
12-
}
13-
14-
private string RemoveAllHtmlTags(string html)
15-
{
16-
html = html.RemoveHtmlTags();
17-
18-
// fix recursive white-spaces
19-
while (html.Contains(" "))
20-
{
21-
html = html.Replace(" ", " ");
22-
}
23-
24-
// fix recursive line-break
25-
while (html.Contains("\r\n\r\n\r\n"))
26-
{
27-
html = html.Replace("\r\n\r\n\r\n", "\r\n\r\n");
28-
}
29-
30-
return html;
31-
}
32-
33-
private string UnescapeString(string lyric)
34-
{
35-
if (!string.IsNullOrEmpty(lyric))
36-
{
37-
// replace entities with literal values
38-
lyric = lyric.Replace("&apos;", "'");
39-
lyric = lyric.Replace("&quot;", "\"");
40-
lyric = lyric.Replace("&gt;", ">");
41-
lyric = lyric.Replace("&lt;", "<");
42-
lyric = lyric.Replace("&amp;", "&");
43-
}
44-
return lyric;
10+
return lyric.RemoveAllHtmlTags()
11+
.UnescapeString()?
12+
.Trim()?
13+
.Replace("\\n", "\r\n")
14+
?? string.Empty;
4515
}
4616
}
4717
}

LyricsScraperNET/Providers/LyricsFreak/LyricsFreakProvider.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using LyricsScraperNET.Models.Responses;
44
using LyricsScraperNET.Network;
55
using LyricsScraperNET.Providers.Abstract;
6+
using LyricsScraperNET.Providers.Models;
67
using Microsoft.Extensions.Logging;
78
using Microsoft.Extensions.Logging.Abstractions;
89
using Microsoft.Extensions.Options;
@@ -12,7 +13,7 @@
1213

1314
namespace LyricsScraperNET.Providers.LyricsFreak
1415
{
15-
internal class LyricsFreakProvider : ExternalProviderBase
16+
public sealed class LyricsFreakProvider : ExternalProviderBase
1617
{
1718
private ILogger<LyricsFreakProvider>? _logger;
1819
private readonly IExternalUriConverter _uriConverter;
@@ -83,7 +84,7 @@ protected override async Task<SearchResult> SearchLyricAsync(string artist, stri
8384
if (WebClient == null || Parser == null)
8485
{
8586
_logger?.LogWarning($"LyricsFreak. Please set up WebClient and Parser first");
86-
return new SearchResult(Models.ExternalProviderType.LyricsFreak);
87+
return new SearchResult(ExternalProviderType.LyricsFreak);
8788
}
8889

8990
// 1. Open the artist's page.
@@ -96,15 +97,15 @@ protected override async Task<SearchResult> SearchLyricAsync(string artist, stri
9697
if (htmlResponse.Contains(PageNotFoundText))
9798
{
9899
_logger?.LogWarning($"LyricsFreak. Artist's page not found (404). [{artist}]. Song name: [{song}]");
99-
return new SearchResult(Models.ExternalProviderType.LyricsFreak);
100+
return new SearchResult(ExternalProviderType.LyricsFreak);
100101
}
101102

102103
// 2. Find song on the artist page and get link to the web page.
103104
var songHref = GetSongHrefFromHtmlBody(htmlResponse, song);
104105
if (string.IsNullOrEmpty(songHref))
105106
{
106107
_logger?.LogWarning($"LyricsFreak. Can't find song Uri for artist: [{artist}]. Song name: [{song}]");
107-
return new SearchResult(Models.ExternalProviderType.LyricsFreak);
108+
return new SearchResult(ExternalProviderType.LyricsFreak);
108109
}
109110
var songUri = new Uri(LyricsFreakUriConverter.BaseUrl + songHref);
110111

@@ -121,12 +122,12 @@ protected async override Task<SearchResult> SearchLyricAsync(Uri uri, Cancellati
121122
if (string.IsNullOrEmpty(songLyrics))
122123
{
123124
_logger?.LogWarning($"LyricsFreak. Can't find lyrics for song's uri: [{uri}]");
124-
return new SearchResult(Models.ExternalProviderType.LyricsFreak);
125+
return new SearchResult(ExternalProviderType.LyricsFreak);
125126
}
126127

127128
var lyricsText = Parser.Parse(songLyrics);
128129

129-
return new SearchResult(lyricsText, Models.ExternalProviderType.LyricsFreak);
130+
return new SearchResult(lyricsText, ExternalProviderType.LyricsFreak);
130131
}
131132

132133
#endregion

LyricsScraperNET/Validations/RequestValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace LyricsScraperNET.Validations
88
{
9-
public class RequestValidator : IRequestValidator
9+
public sealed class RequestValidator : IRequestValidator
1010
{
1111
public bool IsValidSearchRequest(
1212
IProviderService providerService,

0 commit comments

Comments
 (0)