-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
177 lines (153 loc) · 8.19 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using LyricsScraperNET;
using LyricsScraperNET.Configuration;
using LyricsScraperNET.Providers.Abstract;
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;
using Microsoft.Extensions.Hosting;
using LyricsScraperNET.Models.Responses;
using System.Threading.Tasks;
using System;
using Microsoft.Extensions.Logging;
using System.Threading;
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
namespace LyricsScraperNET.Client
{
class Program
{
static async Task Main()
{
//// Input parameters to search:
string artistToSearch = "Parkway Drive";
string songToSearch = "Idols And Anchors";
//// Uncomment this block to test for instrumental songs (without vocals):
//string artistToSearch = "Rush";
//string songToSearch = "YYZ";
//// How to configure for ASP.NET applications:
var result = ExampleWithHostConfiguration(artistToSearch, songToSearch);
//// How to configure for a certain external provider using explicit instantiation:
//var result = ExampleWithExplicitInstantiation(artistToSearch, songToSearch);
//// Checking that something was found. The response can be empty in two cases:
//// 1) A search error occurred. Detailed information can be found in the logs or in response fields like 'ResponseStatusCode' and 'ResponseMessage'.
//// 2) The requested song contains only the instrumental, no lyrics. In this case the flag 'Instrumental' will be true.
if (result.IsEmpty())
{
ConsoleExtensions.WriteLineDelimeter();
if (result.Instrumental)
{
$"This song [{artistToSearch} - {songToSearch}] is instrumental.\r\nIt does not contain any lyrics"
.WriteLineColored(ConsoleColor.Gray);
}
else
{
($"Can't find lyrics for: [{artistToSearch} - {songToSearch}]. " +
$"Status code: [{result.ResponseStatusCode}]. " +
$"Response message: [{result.ResponseMessage}].").WriteLineColored(ConsoleColor.Red);
}
ConsoleExtensions.WriteLineDelimeter();
"Press any key to exit..".WriteLineColored(ConsoleColor.DarkGray);
Console.ReadLine();
return;
}
//// Output result to console
//// Artist and song information
$"[{artistToSearch} - {songToSearch}]".WriteLineColored(ConsoleColor.Yellow);
ConsoleExtensions.WriteLineDelimeter();
//// Lyric text
result.LyricText.WriteLineColored();
ConsoleExtensions.WriteLineDelimeter();
//// Lyrics provider information
$"This lyric was found by [{result.ExternalProviderType}]\r\n".WriteLineColored(ConsoleColor.Magenta);
"Press any key to exit..".WriteLineColored(ConsoleColor.DarkGray);
Console.ReadLine();
return;
}
/// <summary>
/// How to configure LyricScraperClient and search lyrics for ASP.NET applications:
/// </summary>
/// <param name="artistToSearch">artist name to search</param>
/// <param name="songToSearch">song name to search</param>
/// <returns>lyrics text</returns>
private static SearchResult ExampleWithHostConfiguration(string artistToSearch, string songToSearch)
{
//// Application Configuration section.
//// LyricScraperClient configuration could be found in appsettings.json file in section with related name.
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
//// Host creation with LyricScraperClient service configuration
using IHost host = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
// Setting up LyricScraperClient from configuration that stored in appsettings.json.
// Only supported output type as string at the moment.
services.AddLyricScraperClientService(configuration: configuration);
})
.Build();
//// Get instance of LyricScraperClient service
var lyricsScraperClient = host.Services.GetRequiredService<ILyricsScraperClient>();
//// Create request and search
var searchRequest = new ArtistAndSongSearchRequest(artistToSearch, songToSearch);
CancellationToken cancellationToken = new CancellationToken();
var result = lyricsScraperClient.SearchLyric(searchRequest, cancellationToken);
return result;
}
/// <summary>
/// How to configure LyricScraperClient and search lyrics for a certain external provider:
/// </summary>
/// <param name="artistToSearch">artist name to search</param>
/// <param name="songToSearch">song name to search</param>
/// <returns>lyrics text</returns>
private static SearchResult ExampleWithExplicitInstantiation(string artistToSearch, string songToSearch)
{
//// Create instance of LyricScraperClient with all available lyrics providers
ILyricsScraperClient lyricsScraperClient
= new LyricsScraperClient()
.WithAllProviders();
//// To configure a specific provider, use a method like With[ProviderName]()
// ILyricsScraperClient lyricsScraperClient
// = new LyricsScraperClient()
// .WithGenius()
// .WithAZLyrics()
// .WithMusixmatch()
// .WithSongLyrics()
// .WithLyricFind();
//// To perform parallel searches across multiple external providers, enable the following option:
// lyricsScraperClient.UseParallelSearch = true;
// To enable library logging, the LoggerFactory must be configured and passed to the client.
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("LyricsScraperNET", LogLevel.Trace)
.AddConsole();
});
lyricsScraperClient.WithLogger(loggerFactory);
//// Another way to configure:
//// 1. First create instance of LyricScraperClient.
// ILyricsScraperClient lyricsScraperClient = new LyricsScraperClient();
//// 2. Create some external provider instanse with default settings. For example Genius:
// IExternalProvider externalProvider = new GeniusProvider();
//// 2. Or create provider with custom settings like:
// GeniusOptions geniusOptions = new GeniusOptions()
// {
// Enabled = true,
// SearchPriority = 1 // If there are multiple external providers, then the search will start from the provider with the highest priority.
// };
// IExternalProvider externalProvider = new GeniusProvider(geniusOptions);
//// 3. Add external provider to client:
// lyricsScraperClient.AddProvider(externalProvider);
//// Create request and search
var searchRequest = new ArtistAndSongSearchRequest(artistToSearch, songToSearch);
CancellationToken cancellationToken = new CancellationToken();
var result = lyricsScraperClient.SearchLyric(searchRequest, cancellationToken);
return result;
}
}
}
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously