-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.cs
102 lines (85 loc) · 3.9 KB
/
Config.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
namespace twot
{
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.Extensions.Configuration;
using Tweetinvi;
using Tweetinvi.Events;
using static System.ConsoleColor;
using static ConsoleHelper;
internal class Config
{
private Config()
{
// no-op
}
[ConfigInfo(DisplayName = "API Key", ConfigProperty = "apikey")]
public string APIKey { get; private set; } = string.Empty;
[ConfigInfo(DisplayName = "API Secret", ConfigProperty = "apisecret")]
public string APISecret { get; private set; } = string.Empty;
[ConfigInfo(DisplayName = "Access Token", ConfigProperty = "accesstoken")]
public string AccessToken { get; private set; } = string.Empty;
[ConfigInfo(DisplayName = "Access Secret", ConfigProperty = "accesssecret")]
public string AccessSecret { get; private set; } = string.Empty;
public static (bool Success, Config Config) Load()
{
var configuration = new ConfigurationBuilder()
.AddUserSecrets(typeof(Program).Assembly)
.AddJsonFile("./secrets.json", true, false)
.Build();
var section = configuration.GetSection("twot");
var result = new Config();
var propertiesToSet = result.GetType().GetProperties()
.Select(propertyInfo =>
(PropertyInfo: propertyInfo, Config: propertyInfo.GetCustomAttribute<ConfigInfoAttribute>()))
.Where(property => property.Config != null);
foreach (var propInfo in propertiesToSet)
{
var configValue = section.GetValue<string>(propInfo.Config!.ConfigProperty);
if (string.IsNullOrWhiteSpace(configValue))
{
Writeln(Red, $"🚨 {propInfo.Config.DisplayName} not set");
return (false, new Config());
}
propInfo.PropertyInfo.SetValue(result, configValue);
}
RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly;
TweetinviEvents.QueryBeforeExecute += RateLimitCheck;
Auth.SetUserCredentials(result.APIKey, result.APISecret, result.AccessToken, result.AccessSecret);
return (true, result);
}
public override string ToString()
{
return string.Join(Environment.NewLine, this.GetType().GetProperties()
.Select(propertyInfo =>
(PropertyInfo: propertyInfo, Config: propertyInfo.GetCustomAttribute<ConfigInfoAttribute>()))
.Where(property => property.Config != null)
.Select(property => $"{property.Config!.DisplayName} = {property.PropertyInfo.GetValue(this)}"));
}
private static void RateLimitCheck(object? sender, QueryBeforeExecuteEventArgs args)
{
var queryRateLimits = RateLimit.GetQueryRateLimit(args.QueryURL);
if (queryRateLimits != null)
{
if (queryRateLimits.Remaining > 0)
{
return;
}
using (var spinner = new Spinner("You have been rate limited by Twitter until " +
$"{queryRateLimits.ResetDateTime.ToLongTimeString()}. Please wait or Ctrl+C to quit"))
{
Thread.Sleep((int)queryRateLimits.ResetDateTimeInMilliseconds);
spinner.Done();
}
}
}
}
[System.AttributeUsage(System.AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
internal sealed class ConfigInfoAttribute : System.Attribute
{
public string DisplayName { get; set; } = string.Empty;
public string ConfigProperty { get; set; } = string.Empty;
}
}