-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
107 lines (96 loc) · 3.82 KB
/
Startup.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
using Lyrics.Json;
using Lyrics.Models;
using Microsoft.Extensions.Configuration;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
namespace Lyrics;
public static class Startup
{
static void ProcessExit(object? sender, EventArgs e)
=> JsonFileProcessor.WriteLyrics();
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = $"{nameof(SourceGenerationContext)} is set.")]
public static void Configure(out int MAX_COUNT,
out bool RETRY_FAILED_LYRICS,
out List<(string, int)> excludeSongs,
out List<string> excludeTitles,
out List<ILyric> lyricsFromENV)
{
AppDomain.CurrentDomain.ProcessExit += ProcessExit;
Console.CancelKeyPress += ProcessExit;
IOptions option = ReadOptions();
if (!int.TryParse(Environment.GetEnvironmentVariable("MAX_COUNT"), out MAX_COUNT))
{
MAX_COUNT = 2000;
}
if (!bool.TryParse(Environment.GetEnvironmentVariable("RETRY_FAILED_LYRICS"), out RETRY_FAILED_LYRICS))
{
RETRY_FAILED_LYRICS = false;
}
excludeSongs = option.ExcludeVideos.SelectMany(x => x.StartTimes.Select(y => (x.VideoId, y)))
.Concat(option.ExcludeVideos.Where(p => p.StartTimes.Length == 0)
.Select(p => (p.VideoId, -1)))
.ToList();
excludeTitles = [.. option.ExcludeTitles];
string? lyricString = Environment.GetEnvironmentVariable("LYRICS");
lyricsFromENV = [];
if (!string.IsNullOrEmpty(lyricString))
{
try
{
lyricsFromENV = JsonSerializer.Deserialize<List<ILyric>>(lyricString, options: new()
{
TypeInfoResolver = SourceGenerationContext.Default
}) ?? [];
Console.WriteLine($"Get {lyricsFromENV.Count} lyrics from ENV.");
}
catch (Exception e)
{
switch (e)
{
case JsonException:
case NotSupportedException:
Console.Error.WriteLine("Failed to parse lyric json from ENV.");
break;
default:
throw;
}
}
}
#if DEBUG
Directory.CreateDirectory("Lyrics");
Directory.CreateDirectory("Playlists");
File.Create("Lyrics/0.lrc").Close();
#endif
}
private static IOptions ReadOptions()
{
try
{
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
#if DEBUG
.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
#endif
.AddEnvironmentVariables()
.Build();
IOptions? option = configuration.Get<Options>();
if (null == option
|| null == option.ExcludeVideos)
{
throw new ApplicationException("Settings file is not valid.");
}
Console.WriteLine($"Get {option.ExcludeVideos.Length} exclude videos.");
return option;
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine("ERROR_BAD_CONFIGURATION");
Environment.Exit(1610); // ERROR_BAD_CONFIGURATION
return default;
}
}
}