|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace EmoteScavenger |
| 10 | +{ |
| 11 | + public class MultiDownloader |
| 12 | + { |
| 13 | + private const string SOURCE_URL = "https://cdn.discordapp.com/emojis/{0}.png"; |
| 14 | + |
| 15 | + private HttpClient Http { get; } |
| 16 | + private SemaphoreSlim DownloadSemaphore { get; } |
| 17 | + private DirectoryInfo TargetDirectory { get; } |
| 18 | + |
| 19 | + public MultiDownloader(DirectoryInfo target, int concurrencyLevel) |
| 20 | + { |
| 21 | + this.Http = new HttpClient() |
| 22 | + { |
| 23 | + BaseAddress = new Uri("https://cdn.discordapp.com/emojis/") |
| 24 | + }; |
| 25 | + |
| 26 | + this.DownloadSemaphore = new SemaphoreSlim(concurrencyLevel, concurrencyLevel); |
| 27 | + this.TargetDirectory = target; |
| 28 | + } |
| 29 | + |
| 30 | + public Task DownloadAllAsync(IEnumerable<Emoji> emojis) |
| 31 | + { |
| 32 | + var ivs = Path.GetInvalidFileNameChars(); |
| 33 | + var dns = emojis |
| 34 | + .GroupBy(xe => xe.GuildId) |
| 35 | + .Select(xg => (xg.Key, xg.First().GuildName)); |
| 36 | + var dirs = new Dictionary<ulong, DirectoryInfo>(); |
| 37 | + |
| 38 | + foreach (var xt in dns) |
| 39 | + { |
| 40 | + var s = $"{xt.Key} - {xt.GuildName}"; |
| 41 | + s = this.NormalizeString(s, ivs); |
| 42 | + s = Path.Combine(this.TargetDirectory.FullName, s); |
| 43 | + |
| 44 | + var di = new DirectoryInfo(s); |
| 45 | + dirs[xt.Key] = di; |
| 46 | + di.Create(); |
| 47 | + } |
| 48 | + |
| 49 | + var tasks = new List<Task>(); |
| 50 | + foreach (var emoji in emojis) |
| 51 | + { |
| 52 | + var td = dirs[emoji.GuildId]; |
| 53 | + var tf = Path.Combine(td.FullName, $"{emoji.Name}.png"); |
| 54 | + |
| 55 | + tasks.Add(DownloadAsync(emoji, new FileInfo(tf))); |
| 56 | + } |
| 57 | + |
| 58 | + return Task.WhenAll(tasks); |
| 59 | + } |
| 60 | + |
| 61 | + private async Task DownloadAsync(Emoji emoji, FileInfo targetFile) |
| 62 | + { |
| 63 | + await this.DownloadSemaphore.WaitAsync(); |
| 64 | + |
| 65 | + try |
| 66 | + { |
| 67 | + using (var res = await this.Http.GetAsync(string.Format(SOURCE_URL, emoji.Id))) |
| 68 | + using (var str = await res.Content.ReadAsStreamAsync()) |
| 69 | + using (var fs = targetFile.Create()) |
| 70 | + await str.CopyToAsync(fs); |
| 71 | + |
| 72 | + this.LogCompletion(emoji); |
| 73 | + } |
| 74 | + catch (Exception ex) |
| 75 | + { |
| 76 | + this.LogFailure(emoji, ex); |
| 77 | + } |
| 78 | + finally |
| 79 | + { |
| 80 | + this.DownloadSemaphore.Release(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private void LogCompletion(Emoji e) |
| 85 | + { |
| 86 | + if (this.MessageLogged == null) |
| 87 | + return; |
| 88 | + |
| 89 | + this.MessageLogged($"Emoji download completed: {e.Name}/{e.Id}"); |
| 90 | + } |
| 91 | + |
| 92 | + private void LogFailure(Emoji e, Exception ex) |
| 93 | + { |
| 94 | + if (this.MessageLogged == null) |
| 95 | + return; |
| 96 | + |
| 97 | + this.MessageLogged($"FAIL: {e.Name}/{e.Id} ({ex.GetType()}: {ex.Message})"); |
| 98 | + } |
| 99 | + |
| 100 | + private string NormalizeString(string val, char[] illegals) |
| 101 | + { |
| 102 | + var x = val.ToCharArray(); |
| 103 | + var c = false; |
| 104 | + for (var i = 0; i < x.Length; i++) |
| 105 | + { |
| 106 | + if (illegals.Contains(x[i])) |
| 107 | + { |
| 108 | + c = true; |
| 109 | + x[i] = '_'; |
| 110 | + } |
| 111 | + } |
| 112 | + if (c) |
| 113 | + return new string(x); |
| 114 | + return val; |
| 115 | + } |
| 116 | + |
| 117 | + public event MessageLoggedEventHandler MessageLogged; |
| 118 | + } |
| 119 | +} |
0 commit comments