-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWindowsIsoDownloader.cs
166 lines (142 loc) · 5.15 KB
/
WindowsIsoDownloader.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
using Microsoft.Playwright;
using System.Text.Json;
using WindowsIsoDownloader;
using WindowsIsoDownloader.Extension;
var jsonConfig = File.ReadAllText("config.json");
Config? config = null;
try
{
config = JsonSerializer.Deserialize<Config>(jsonConfig);
}
catch(Exception e)
{
ErrorLoadingConfig();
return -1;
}
if (null == config
|| string.IsNullOrWhiteSpace(config.DownloadFolder)
|| string.IsNullOrWhiteSpace(config.DownloadFilename)
|| !config.Actions.Any())
{
ErrorLoadingConfig();
return -1;
}
Directory.CreateDirectory(config.DownloadFolder);
var exitCode = InstallPlaywrightDependencies();
if (exitCode != 0)
{
Console.WriteLine("[Fatal] Failed to install Playwright dependencies.");
return -1;
}
Console.WriteLine("[Success] Playwright dependencies installed.");
Console.WriteLine("[Info] Trying to obtain Windows 11 iso download link...");
using var playwright = await Playwright.CreateAsync();
var firefox = playwright.Firefox;
var browser = await firefox.LaunchAsync(new BrowserTypeLaunchOptions());
var page = await browser.NewPageAsync();
var actions = config.Actions.OrderBy(x => x.Order);
IElementHandle? downloadButton = null;
foreach(var action in actions)
{
if(action.WaitBeforeAction.HasValue && action.WaitBeforeAction.Value > 0)
{
await page.WaitForTimeoutAsync(action.WaitBeforeAction.Value);
}
if(action.Kind == "Goto")
{
if (string.IsNullOrEmpty(action.Parameters.Url))
{
throw new ArgumentNullException("The Url parameter for the Goto action is not specified.", innerException: null);
}
await page.GotoAsync(action.Parameters.Url);
}
else if (action.Kind == "SelectOption")
{
if (string.IsNullOrEmpty(action.Parameters.Selector) || string.IsNullOrEmpty(action.Parameters.Values))
{
throw new ArgumentNullException("The Selector and/or the Values parameter(s) for the SelectOption action is/are not specified.", innerException: null);
}
await page.SelectOptionAsync(action.Parameters.Selector, action.Parameters.Values);
}
else if (action.Kind == "Click")
{
if (string.IsNullOrEmpty(action.Parameters.Selector))
{
throw new ArgumentNullException("The Selector parameter for the Click action is not specified.", innerException: null);
}
await page.ClickAsync(action.Parameters.Selector);
}
else if (action.Kind == "QuerySelector")
{
if (string.IsNullOrEmpty(action.Parameters.Selector))
{
throw new ArgumentNullException("The Selector parameter for the QuerySelector action is not specified.", innerException: null);
}
downloadButton = await page.QuerySelectorAsync(action.Parameters.Selector);
}
}
float currentProgress = 0;
if(null == downloadButton)
{
Console.WriteLine("[Fatal] Download button was not found.");
return -1;
}
else
{
var isoFileUrl = await downloadButton.EvaluateAsync<string>("element => element.href");
if (isoFileUrl.Contains(".iso"))
{
Console.WriteLine($"[Success] Download link found: {isoFileUrl}");
Console.WriteLine("[Info] Windows 11 iso download in progress...");
using(var httpClient = new HttpClient())
{
httpClient.Timeout = TimeSpan.FromHours(2);
try
{
using (var filestream = new FileStream(Path.Combine(config.DownloadFolder, config.DownloadFilename), FileMode.Create, FileAccess.Write, FileShare.None))
{
var progress = new Progress<float>();
progress.ProgressChanged += ProgressChanged;
await httpClient.DownloadAsync(isoFileUrl, filestream, progress);
}
}
catch(UnauthorizedAccessException e)
{
Console.WriteLine($"[Fatal] Can't write at path: {Path.Combine(config.DownloadFolder, config.DownloadFilename)}. Try again from a terminal running as administrator.");
return -1;
}
}
}
else
{
Console.WriteLine($"[Fatal] Found a download link but it seems incorrect: {isoFileUrl}");
return -1;
}
Console.WriteLine();
Console.WriteLine("[Success] ISO Downloaded successfully! Exiting with code 0.");
return 0;
}
void ProgressChanged(object? sender, float e)
{
// The progress in the e variable is in the range 0.000 (0%) to 1.000 (100%)
float reportedProgress = (float)Math.Round(e, 3);
if(reportedProgress > currentProgress)
{
currentProgress = reportedProgress;
for(int i = 0; i < 100; i++)
{
Console.Write((i < (int)(currentProgress * 100)) ? "█" : "░");
}
Console.Write($" {currentProgress.ToString("p1")}");
Console.SetCursorPosition(0, Console.GetCursorPosition().Top);
}
}
void ErrorLoadingConfig()
{
Console.WriteLine("[Fatal] Unable to load the configuration. Exiting with code -1.");
}
int InstallPlaywrightDependencies()
{
Console.WriteLine("[Info] Installing dependencies for Playwright...");
return Microsoft.Playwright.Program.Main(new[] { "install", "firefox" });
}