summary refs log tree commit diff
path: root/ReferenceClientProxyImplementation/Services/BuildDownloadService.cs
blob: 364c6c51e98ccc58e1718691175ddf010b041317 (plain) (blame)
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
// using System.Net;
// using AngleSharp.Html.Parser;
//
// namespace ReferenceClientProxyImplementation.Services;
//
// public class BuildDownloadService(ILogger<BuildDownloadService> logger) {
//     private static readonly HttpClient hc = new();
//
//     public async Task DownloadBuildFromArchiveOrg(string outputDirectory, DateTime timestamp) {
//         // 20150906025145
//         var paddedTimestamp = timestamp.ToString("yyyyMMddHHmmss");
//         await DownloadBuildFromUrl(outputDirectory, $"https://web.archive.org/web/{paddedTimestamp}im_/https://discordapp.com/login");
//     }
//
//     public async Task DownloadBuildFromUrl(string outputDirectory, string url) {
//         logger.LogInformation("Downloading build from {url} to {outDir}", url, outputDirectory);
//         var response = await hc.GetAsync(url);
//         if (!response.IsSuccessStatusCode)
//             throw new Exception($"Failed to download build from {url}");
//         var html = await response.Content.ReadAsStringAsync();
//         File.WriteAllText(outputDirectory + "/index.html", html);
//         var parser = new HtmlParser();
//         var document = parser.ParseDocument(html);
//         var assets = document.QuerySelectorAll("link[rel=stylesheet], link[rel=icon], script, img");
//         foreach (var asset in assets) {
//             var assetUrl = asset.GetAttribute("href") ?? asset.GetAttribute("src");
//             if (assetUrl == null)
//                 continue;
//             if (assetUrl.StartsWith("//")) {
//                 logger.LogWarning("Skipping asset {assetUrl} as it is a protocol-relative URL", assetUrl);
//                 continue;
//             }
//
//             var assetStream = await GetAssetStream(assetUrl);
//             var assetPath = Path.Combine(outputDirectory, assetUrl.TrimStart('/'));
//             Console.WriteLine($"Downloading asset {assetUrl} to {assetPath}");
//             Directory.CreateDirectory(Path.GetDirectoryName(assetPath));
//             await using var fs = File.Create(assetPath);
//             await assetStream.CopyToAsync(fs);
//         }
//
//         logger.LogInformation("Downloading build from {url} complete!", url);
//     }
//
//     public async Task<Stream> GetAssetStream(string asset) {
//         asset = asset.Replace("/assets/", "");
//         var urlsToTry = new Stack<string>(new[] {
//             $"https://web.archive.org/web/0id_/https://discordapp.com/assets/{asset}",
//             $"https://web.archive.org/web/0id_/https://discord.com/assets/{asset}",
//             $"https://discord.com/assets/{asset}"
//         });
//         while (urlsToTry.TryPop(out var urlToTry)) {
//             if (string.IsNullOrWhiteSpace(urlToTry)) continue;
//             try {
//                 var response = await hc.GetAsync(urlToTry, HttpCompletionOption.ResponseHeadersRead);
//                 if (response.IsSuccessStatusCode) {
//                     Console.WriteLine($"Got success for asset {asset} from {urlToTry}");
//                     return await response.Content.ReadAsStreamAsync();
//                 }
//                 //redirect
//
//                 if (response.StatusCode == HttpStatusCode.Found) {
//                     var redirectUrl = response.Headers.Location?.ToString();
//                     if (string.IsNullOrWhiteSpace(redirectUrl)) continue;
//                     urlsToTry.Push(redirectUrl);
//                 }
//                 else logger.LogWarning("Failed to download asset {asset} from {urlToTry}", asset, urlToTry);
//             }
//             catch {
//                 // ignored
//             }
//         }
//
//         throw new Exception($"Failed to download asset {asset}");
//     }
// }