Skip to content

Commit b5aa52a

Browse files
authored
Merge pull request #46 from Lukeuke/feature/add-kpoplyrics
Added KPopLyrics
2 parents 3dd874e + 3bd9d83 commit b5aa52a

31 files changed

+1258
-18
lines changed

LyricsScraperNET.Client/Program.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
using LyricsScraperNET;
2-
using LyricsScraperNET.Configuration;
3-
using LyricsScraperNET.Providers.Abstract;
4-
using LyricsScraperNET.Providers.AZLyrics;
5-
using LyricsScraperNET.Providers.Genius;
6-
using LyricsScraperNET.Providers.SongLyrics;
7-
using LyricsScraperNET.Providers.LyricFind;
8-
using LyricsScraperNET.Models.Requests;
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.Extensions.Logging;
96
using Microsoft.Extensions.Configuration;
107
using Microsoft.Extensions.DependencyInjection;
11-
using Microsoft.Extensions.Hosting;
8+
using LyricsScraperNET.Configuration;
9+
using LyricsScraperNET.Models.Requests;
1210
using LyricsScraperNET.Models.Responses;
13-
using System.Threading.Tasks;
14-
using System;
15-
using Microsoft.Extensions.Logging;
16-
using System.Threading;
1711

1812
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
1913

LyricsScraperNET.Client/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
"LyricFindOptions": {
2323
"SearchPriority": 1,
2424
"Enabled": true
25+
},
26+
"KPopLyricsOptions": {
27+
"SearchPriority": 5,
28+
"Enabled": true
2529
}
2630
}
2731
}

LyricsScraperNET/Configuration/ILyricScraperClientConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ public interface ILyricScraperClientConfig
2424
IExternalProviderOptions SongLyricsOptions { get; }
2525

2626
IExternalProviderOptions LyricFindOptions { get; }
27+
IExternalProviderOptions KPopLyricsOptions { get; }
2728
}
2829
}

LyricsScraperNET/Configuration/LyricScraperClientConfig.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using LyricsScraperNET.Providers.Musixmatch;
66
using LyricsScraperNET.Providers.SongLyrics;
77
using System.Text.Json.Serialization;
8+
using LyricsScraperNET.Providers.KPopLyrics;
89

910
namespace LyricsScraperNET.Configuration
1011
{
@@ -23,6 +24,8 @@ public sealed class LyricScraperClientConfig : ILyricScraperClientConfig
2324

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

27+
public IExternalProviderOptions KPopLyricsOptions { get; set; } = new KPopLyricsOptions();
28+
2629
/// <inheritdoc />
2730
public bool UseParallelSearch { get; set; } = false;
2831

@@ -31,6 +34,7 @@ public sealed class LyricScraperClientConfig : ILyricScraperClientConfig
3134
|| GeniusOptions.Enabled
3235
|| MusixmatchOptions.Enabled
3336
|| SongLyricsOptions.Enabled
34-
|| LyricFindOptions.Enabled;
37+
|| LyricFindOptions.Enabled
38+
|| KPopLyricsOptions.Enabled;
3539
}
3640
}

LyricsScraperNET/Configuration/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using LyricsScraperNET.Providers.LyricFind;
55
using LyricsScraperNET.Providers.Musixmatch;
66
using LyricsScraperNET.Providers.SongLyrics;
7+
using LyricsScraperNET.Providers.KPopLyrics;
78
using Microsoft.Extensions.Configuration;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Options;
@@ -24,6 +25,7 @@ public static IServiceCollection AddLyricScraperClientService(
2425
services.AddProvider<GeniusOptions, GeniusProvider>(lyricScraperClientConfig);
2526
services.AddProvider<SongLyricsOptions, SongLyricsProvider>(lyricScraperClientConfig);
2627
services.AddProvider<LyricFindOptions, LyricFindProvider>(lyricScraperClientConfig);
28+
services.AddProvider<KPopLyricsOptions, KPopLyricsProvider>(lyricScraperClientConfig);
2729

2830
services.AddMusixmatchService(lyricScraperClientConfig);
2931

LyricsScraperNET/Extensions/LyricsScraperClientExtensions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using LyricsScraperNET.Providers.AZLyrics;
22
using LyricsScraperNET.Providers.Genius;
3+
using LyricsScraperNET.Providers.KPopLyrics;
34
using LyricsScraperNET.Providers.LyricFind;
45
using LyricsScraperNET.Providers.Models;
56
using LyricsScraperNET.Providers.Musixmatch;
@@ -39,6 +40,12 @@ public static ILyricsScraperClient WithLyricFind(this ILyricsScraperClient lyric
3940
return lyricsScraperClient;
4041
}
4142

43+
public static ILyricsScraperClient WithKPopLyrics(this ILyricsScraperClient lyricsScraperClient)
44+
{
45+
lyricsScraperClient.AddProvider(new KPopLyricsProvider());
46+
return lyricsScraperClient;
47+
}
48+
4249
/// <summary>
4350
/// Configure LyricsScraperClient with all available providers in <seealso cref="ExternalProviderType"/>.
4451
/// Search lyrics enabled by default for all providers.
@@ -50,7 +57,8 @@ public static ILyricsScraperClient WithAllProviders(this ILyricsScraperClient ly
5057
.WithAZLyrics()
5158
.WithMusixmatch()
5259
.WithSongLyrics()
53-
.WithLyricFind();
60+
.WithLyricFind()
61+
.WithKPopLyrics();
5462
}
5563
}
5664
}

LyricsScraperNET/Extensions/StringExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,38 @@ public static string СonvertToDashedFormat(this string input, bool useException
7171

7272
return result;
7373
}
74+
75+
public static string CreateCombinedUrlSlug(string artist, string songTitle)
76+
{
77+
artist = Regex.Replace(artist, @"\([^a-zA-Z0-9\s]*\)", "").Trim();
78+
songTitle = Regex.Replace(songTitle, @"\([^a-zA-Z0-9\s]*\)", "").Trim();
79+
80+
var combined = $"{artist} {songTitle}";
81+
82+
var slug = string.Empty;
83+
84+
foreach (var c in combined)
85+
{
86+
switch (c)
87+
{
88+
case ' ':
89+
slug += '-';
90+
break;
91+
case >= 'a' and <= 'z':
92+
case >= 'A' and <= 'Z':
93+
case >= '0' and <= '9':
94+
slug += c;
95+
break;
96+
case '-':
97+
slug += '-';
98+
break;
99+
}
100+
}
101+
102+
slug = Regex.Replace(slug, @"-+", "-");
103+
slug = slug.Trim('-');
104+
105+
return slug.ToLower();
106+
}
74107
}
75108
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using LyricsScraperNET.Providers.Abstract;
2+
using LyricsScraperNET.Providers.Models;
3+
4+
namespace LyricsScraperNET.Providers.KPopLyrics
5+
{
6+
public class KPopLyricsOptions : IExternalProviderOptions
7+
{
8+
public ExternalProviderType ExternalProviderType => ExternalProviderType.KPopLyrics;
9+
public bool Enabled { get; set; }
10+
public int SearchPriority { get; set; } = 5;
11+
public string ConfigurationSectionName => "KPopLyricsOptions";
12+
13+
public override bool Equals(object? obj)
14+
{
15+
return obj is KPopLyricsOptions options &&
16+
ExternalProviderType == options.ExternalProviderType;
17+
}
18+
19+
public override int GetHashCode()
20+
{
21+
unchecked
22+
{
23+
int hash = 17;
24+
hash = (hash * 31) + ExternalProviderType.GetHashCode();
25+
return hash;
26+
}
27+
}
28+
}
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Linq;
2+
using System.Text.RegularExpressions;
3+
using HtmlAgilityPack;
4+
using LyricsScraperNET.Providers.Abstract;
5+
6+
namespace LyricsScraperNET.Providers.KPopLyrics
7+
{
8+
public class KPopLyricsParser : IExternalProviderLyricParser
9+
{
10+
public string Parse(string lyric)
11+
{
12+
var htmlDoc = new HtmlDocument();
13+
htmlDoc.LoadHtml(lyric);
14+
15+
var deEntitizedText = string.Join("\n\n", // <p> -> \n\n
16+
htmlDoc.DocumentNode.SelectNodes("//p")
17+
.Select(node => HtmlEntity.DeEntitize(node.InnerHtml
18+
.Replace("<br> ", "\n") // the trailing whitespace after <br> is necessary
19+
.Trim()
20+
)));
21+
22+
// remove the tags that are left and trim the text
23+
return Regex.Replace(deEntitizedText, "<.*?>", string.Empty).Trim();
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)