Selector/Selector.CLI/Options.cs

135 lines
5.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2021-10-11 23:19:19 +01:00
namespace Selector.CLI
{
static class OptionsHelper {
public static void ConfigureOptions(RootOptions options, IConfiguration config)
{
config.GetSection(RootOptions.Key).Bind(options);
config.GetSection(FormatKeys( new[] { RootOptions.Key, WatcherOptions.Key})).Bind(options.WatcherOptions);
config.GetSection(FormatKeys( new[] { RootOptions.Key, DatabaseOptions.Key})).Bind(options.DatabaseOptions);
config.GetSection(FormatKeys( new[] { RootOptions.Key, RedisOptions.Key})).Bind(options.RedisOptions);
2022-02-25 21:07:22 +00:00
config.GetSection(FormatKeys( new[] { RootOptions.Key, JobsOptions.Key})).Bind(options.JobOptions);
config.GetSection(FormatKeys( new[] { RootOptions.Key, JobsOptions.Key, ScrobbleWatcherJobOptions.Key })).Bind(options.JobOptions.Scrobble);
}
public static RootOptions ConfigureOptions(this IConfiguration config)
{
var options = config.GetSection(RootOptions.Key).Get<RootOptions>();
ConfigureOptions(options, config);
return options;
}
public static RootOptions ConfigureOptionsInjection(this IConfiguration config, IServiceCollection services)
{
var options = config.GetSection(RootOptions.Key).Get<RootOptions>();
services.Configure<DatabaseOptions>(config.GetSection(FormatKeys(new[] { RootOptions.Key, DatabaseOptions.Key })));
services.Configure<RedisOptions>(config.GetSection(FormatKeys(new[] { RootOptions.Key, RedisOptions.Key })));
services.Configure<WatcherOptions>(config.GetSection(FormatKeys(new[] { RootOptions.Key, WatcherOptions.Key })));
services.Configure<JobsOptions>(config.GetSection(FormatKeys(new[] { RootOptions.Key, JobsOptions.Key })));
services.Configure<ScrobbleWatcherJobOptions>(config.GetSection(FormatKeys(new[] { RootOptions.Key, JobsOptions.Key, ScrobbleWatcherJobOptions.Key })));
return options;
}
public static string FormatKeys(string[] args) => string.Join(":", args);
}
public class RootOptions
{
public const string Key = "Selector";
2021-10-11 23:19:19 +01:00
/// <summary>
/// Spotify client ID
/// </summary>
public string ClientId { get; set; }
/// <summary>
/// Spotify app secret
/// </summary>
public string ClientSecret { get; set; }
/// <summary>
/// Service account refresh token for tool spotify usage
/// </summary>
public string RefreshToken { get; set; }
public string LastfmClient { get; set; }
public string LastfmSecret { get; set; }
2021-10-11 23:19:19 +01:00
public WatcherOptions WatcherOptions { get; set; } = new();
2022-02-25 21:07:22 +00:00
public JobsOptions JobOptions { get; set; } = new();
public DatabaseOptions DatabaseOptions { get; set; } = new();
public RedisOptions RedisOptions { get; set; } = new();
2021-10-11 23:19:19 +01:00
public EqualityChecker Equality { get; set; } = EqualityChecker.Uri;
}
public enum EqualityChecker
2021-10-11 23:19:19 +01:00
{
Uri, String
}
public class WatcherOptions
2021-10-11 23:19:19 +01:00
{
public const string Key = "Watcher";
public bool Enabled { get; set; } = true;
public bool LocalEnabled { get; set; } = true;
2021-10-11 23:19:19 +01:00
public List<WatcherInstanceOptions> Instances { get; set; } = new();
}
public class WatcherInstanceOptions
2021-10-11 23:19:19 +01:00
{
public const string Key = "Instances";
public string Name { get; set; }
public string AccessKey { get; set; }
public string RefreshKey { get; set; }
public string LastFmUsername { get; set; }
2021-10-11 23:19:19 +01:00
public int PollPeriod { get; set; } = 5000;
public WatcherType Type { get; set; } = WatcherType.Player;
public List<Consumers> Consumers { get; set; } = default;
2021-10-12 20:23:25 +01:00
#nullable enable
2021-10-11 23:19:19 +01:00
public string? PlaylistUri { get; set; }
public string? WatcherCollection { get; set; }
2021-10-12 20:23:25 +01:00
#nullable disable
2021-10-11 23:19:19 +01:00
}
public enum Consumers
{
AudioFeatures, AudioFeaturesCache, CacheWriter, Publisher, PlayCounter
}
public class RedisOptions
{
public const string Key = "Redis";
public bool Enabled { get; set; } = false;
public string ConnectionString { get; set; }
}
2022-02-25 21:07:22 +00:00
public class JobsOptions
{
2022-02-25 21:07:22 +00:00
public const string Key = "Job";
public bool Enabled { get; set; } = true;
2022-02-25 21:07:22 +00:00
public ScrobbleWatcherJobOptions Scrobble { get; set; } = new();
}
public class ScrobbleWatcherJobOptions
{
public const string Key = "Scrobble";
public bool Enabled { get; set; } = true;
2022-03-02 19:12:16 +00:00
public string FullScrobbleCron { get; set; } = "0 0 2 * * *";
2022-02-25 21:07:22 +00:00
public TimeSpan InterJobDelay { get; set; } = TimeSpan.FromMinutes(5);
public TimeSpan InterRequestDelay { get; set; } = TimeSpan.FromMilliseconds(100);
public DateTime? From { get; set; } = DateTime.UtcNow.AddDays(-14);
public int PageSize { get; set; } = 200;
public int Simultaneous { get; set; } = 3;
}
}