-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
222 lines (184 loc) · 8.76 KB
/
Copy pathProgram.cs
File metadata and controls
222 lines (184 loc) · 8.76 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HtmlAgilityPack;
class Program {
static async Task Main(string[] args) {
string baseUrl = "http://books.toscrape.com"; // Change this to the desired website URL
//string baseUrl = "https://wavezodium.dev";
//string baseUrl = "https://wavey.se";
await DownloadWebsite(baseUrl);
}
#nullable enable
public static string CreateValidFilePath(string url) {
// Define the regular expression pattern to match the URL components.
string pattern = @"^(https?://)?([^/]+)(/.*)?$";
// Match the URL against the pattern.
Match match = Regex.Match(url, pattern);
// Extract the FQDN and subdirectories.
if (match.Success) {
string fqdn = match.Groups[2].Value;
string subdirectories = match.Groups[3].Value.TrimStart('/');
// Find the index of "?"
int idx_query_string_start = subdirectories.IndexOf("?");
// Remove potential query string(s)
subdirectories = idx_query_string_start >= 0 ? subdirectories.Substring(0, idx_query_string_start) : subdirectories;
// Define a list of characters not allowed in filenames or folder names
char[] invalidCharacters = { '/', '\\', ':', '*', '?', '"', '<', '>', '|' };
// TODO: Implement Path.GetInvalidPathChars() instead of manual regex replace list.
// Replace invalid filename characters.
subdirectories = Regex.Replace(subdirectories, @"[\\:*?""<>|]", "_");
// Split the URL by "/"
string[] subdirectoriesParts = subdirectories.Split('/');
// Join the string array with the FQDN to create a full filepath.
subdirectories = Path.Combine(subdirectoriesParts);
string fullPath = Path.Combine(fqdn, subdirectories);
return fullPath;
}
else {
Console.WriteLine("Invalid URL format.");
return string.Empty;
}
}
public static string? GetFqdn(string url) {
// Define the regular expression pattern to match the URL components.
string pattern = @"^(https?://)?([^/]+)(/.*)?$";
// Match the URL against the pattern.
Match match = Regex.Match(url, pattern);
// Extract the FQDN and subdirectories.
if (match.Success) {
return match.Groups[2].Value;
}
else {
Console.WriteLine("Invalid URL format.");
return null;
}
}
static async Task DownloadWebsite(string baseUrl) {
var httpClient = new HttpClient();
var visitedUrls = new HashSet<string>();
var queue = new Queue<string>();
queue.Enqueue(baseUrl);
int max_loop = 10;
int loop_no = 0;
while (queue.Count > 0 && loop_no < max_loop) {
string url = queue.Dequeue();
if (!visitedUrls.Contains(url)) {
visitedUrls.Add(url);
Console.WriteLine("Visiting: " + url);
try {
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
string htmlContent = await response.Content.ReadAsStringAsync();
// Save the HTML content
await SaveHtml(url, htmlContent);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(htmlContent);
// Extract links from the HTML
var links = htmlDocument.DocumentNode.SelectNodes("//a[@href]");
if (links != null) {
foreach (var link in links) {
string href = link.GetAttributeValue("href", "");
string absoluteUrl = MakeAbsoluteUrl(url, href);
if (IsInDomain(absoluteUrl, baseUrl)) {
queue.Enqueue(absoluteUrl);
}
}
}
// Download resources (images, stylesheets, scripts, etc.)
await DownloadResources(htmlDocument, url);
}
catch (Exception ex) {
Console.WriteLine($"Failed to download {url}: {ex.Message}");
}
}
loop_no++;
}
}
static async Task SaveHtml(string url, string htmlContent) {
string fileName1 = Path.GetFileName(url);
if (fileName1 != GetFqdn(url)) {
try {
string directoryUrl = url.Substring(0, url.LastIndexOf('/') + 1);
//Console.WriteLine("[0] directoryUrl: " + directoryUrl);
string fullPath = Path.Combine("downloaded_sites", CreateValidFilePath(directoryUrl));
//Console.WriteLine("[1] fullPath: " + fullPath);
//Console.WriteLine("[2] fileName1: " + fileName1);
//Console.WriteLine("[3] Is this directory valid?: " + directoryUrl);
//Console.WriteLine("[4] Is this path valid?: " + fullPath);
string filePath1 = Path.Combine(fullPath, fileName1);
//Console.WriteLine("[5] filePath1: " + filePath1);
Console.WriteLine("Creating " + fullPath + " ...");
Directory.CreateDirectory(fullPath);
Console.WriteLine("Saving " + filePath1 + " ...");
await File.WriteAllTextAsync(filePath1, htmlContent);
}
catch (Exception ex) {
Console.WriteLine($"Failed to download {url}: {ex.Message}");
}
}
}
static async Task DownloadResources(HtmlDocument htmlDocument, string baseUrl) {
Console.WriteLine("Base URL: " + baseUrl);
var httpClient = new HttpClient();
var resources = new HashSet<string>();
// Extract resource URLs (images, stylesheets, scripts, etc.)
//var imageUrls = htmlDocument.DocumentNode.SelectNodes("//img[@src]");
//var styleUrls = htmlDocument.DocumentNode.SelectNodes("//link[@rel='stylesheet']");
//var scriptUrls = htmlDocument.DocumentNode.SelectNodes("//script[@src]");
/*if (imageUrls != null) {
foreach (var imageUrl in imageUrls) {
string url = MakeAbsoluteUrl(baseUrl, imageUrl.GetAttributeValue("src", ""));
resources.Add(url);
Console.WriteLine("Image url: " + url);
}
}*/
/*if (styleUrls != null) {
foreach (var styleUrl in styleUrls) {
string url = MakeAbsoluteUrl(baseUrl, styleUrl.GetAttributeValue("href", ""));
resources.Add(url);
Console.WriteLine("Style url: " + url);
}
}*/
/*if (scriptUrls != null) {
foreach (var scriptUrl in scriptUrls) {
string url = MakeAbsoluteUrl(baseUrl, scriptUrl.GetAttributeValue("src", ""));
resources.Add(url);
Console.WriteLine("Script url: " + url);
}
}*/
// Download resources
/*foreach (var resourceUrl in resources) {
try {
HttpResponseMessage response = await httpClient.GetAsync(resourceUrl);
response.EnsureSuccessStatusCode();
byte[] data = await response.Content.ReadAsByteArrayAsync();
string fileName = Path.GetFileName(resourceUrl);
string directory = Path.Combine("downloaded_sites", baseUrl.Substring(baseUrl.IndexOf("://") + 3).Split('/')[0]);
string directory2 = Path.Combine("downloaded_sites", CreateValidFilePath(resourceUrl));
Console.WriteLine("[0] Is this valid?: " + directory);
Console.WriteLine("[0] resourceUrl: " + resourceUrl);
Console.WriteLine("[1] Is this valid?: " + directory2);
Directory.CreateDirectory(directory);
string filePath = Path.Combine(directory, fileName);
await File.WriteAllBytesAsync(filePath, data);
}
catch (Exception ex) {
Console.WriteLine($"Failed to download {resourceUrl}: {ex.Message}");
}
}*/
}
static string MakeAbsoluteUrl(string baseUrl, string href) {
Uri baseUri = new Uri(baseUrl);
Uri absoluteUri = new Uri(baseUri, href);
return absoluteUri.ToString();
}
static bool IsInDomain(string url, string baseUrl) {
Uri baseUri = new Uri(baseUrl);
Uri uri = new Uri(url);
return uri.Host == baseUri.Host;
}
}