-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
138 lines (112 loc) · 5.02 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
using CommandLine;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using TwitchClipper.GitHub_Updater;
using TwitchClipper.Helpers;
using TwitchClipper.Models;
using TwitchClipper.Services;
namespace TwitchClipper
{
public class Program
{
public static IConfiguration Configuration { get; private set; }
public static async Task Main(string[] args)
{
Console.Clear();
try
{
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddCommandLine(args)
.AddEnvironmentVariables()
.Build();
var services = ConfigureServices();
var serviceProvider = services.BuildServiceProvider();
Options options = null;
var result = await Parser.Default.ParseArguments<Options>(args).WithParsedAsync(async o =>
{
await Task.Run(() => options = o);
});
await result.WithNotParsedAsync(async errors =>
{
await Task.Run(() => Environment.Exit(-1));
});
if (!string.IsNullOrWhiteSpace(options.DateFrom) || !string.IsNullOrWhiteSpace(options.DateTo))
{
var filtering = await CreateFiltering(options);
await serviceProvider.GetService<IFilteringService>().SetFiltering(filtering);
}
await LogHelper.Log($"Downloading clips made by {options.Username}");
await serviceProvider.GetService<Application>().Run(options);
await LogHelper.Log("Done!");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private static async Task<Filtering> CreateFiltering(Options o)
{
//if from is set, but to is not, and wise versa
if ((!string.IsNullOrWhiteSpace(o.DateFrom) && string.IsNullOrWhiteSpace(o.DateTo)) || (!string.IsNullOrWhiteSpace(o.DateTo) && string.IsNullOrWhiteSpace(o.DateFrom)))
{
await ErrorHelper.LogAndExit("If you specify --from or --to, the other one has to be present as well");
}
var filter = new Filtering();
DateTime dateFrom, dateTo;
if (!DateTime.TryParseExact(o.DateFrom, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateFrom))
{
await ErrorHelper.LogAndExit($"Unable to parse {o.DateFrom} to a date. You must specify the dates as yyyy-MM-dd (e.g. 2021-05-15");
}
if (!DateTime.TryParseExact(o.DateTo, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTo))
{
await ErrorHelper.LogAndExit($"Unable to parse {o.DateTo} to a date. You must specify the dates as yyyy-MM-dd (e.g. 2021-05-15");
}
if(dateTo <= dateFrom)
{
await ErrorHelper.LogAndExit("To date must be after from date");
}
if(dateTo > DateTime.Today)
{
await ErrorHelper.LogAndExit("To date cannot be in the future");
}
if(dateFrom < new DateTime(2016, 05, 26))
{
await ErrorHelper.LogAndExit("Date from cannot be before 2016-05-26 because that's when Twitch announced clips");
}
filter.DateFrom = dateFrom;
filter.DateTo = dateTo;
return filter;
}
private static IServiceCollection ConfigureServices()
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton(Configuration);
services.AddOptions();
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
services.AddTransient<Application>();
services.AddTransient<ITwitchAPIService, TwitchAPIService>();
services.AddScoped<IConfigurationService, ConfigurationService>();
services.AddScoped<ITwitchConfigurationService, TwitchConfigurationService>();
services.AddTransient<IDownloaderService, DownloaderService>();
services.AddScoped<IHostService, HostService>();
services.AddSingleton<IFilteringService, FilteringService>();
services.AddTransient<IGitHubUpdater, GitHubUpdater>();
services.AddSingleton<IArchivingService, ArchivingService>();
return services;
}
}
}