From c6cbafbf6198ad9d7e55a97ddef6d9df54570ce6 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Sat, 28 Jan 2017 22:28:23 +0100 Subject: [PATCH] First Try Download --- Discord Media Loader/MainForm.Designer.cs | 23 +++-- Discord Media Loader/MainForm.cs | 96 ++++++++++++++++++- .../Properties/AssemblyInfo.cs | 4 +- 3 files changed, 107 insertions(+), 16 deletions(-) diff --git a/Discord Media Loader/MainForm.Designer.cs b/Discord Media Loader/MainForm.Designer.cs index 431204a..86d84ba 100644 --- a/Discord Media Loader/MainForm.Designer.cs +++ b/Discord Media Loader/MainForm.Designer.cs @@ -38,7 +38,7 @@ this.dtpLimit = new System.Windows.Forms.DateTimePicker(); this.btnSearch = new System.Windows.Forms.Button(); this.lbPath = new System.Windows.Forms.Label(); - this.textBox1 = new System.Windows.Forms.TextBox(); + this.tbxPath = new System.Windows.Forms.TextBox(); this.btnDownload = new System.Windows.Forms.Button(); this.SuspendLayout(); // @@ -89,12 +89,15 @@ // cbLimitDate // this.cbLimitDate.AutoSize = true; + this.cbLimitDate.Checked = true; + this.cbLimitDate.CheckState = System.Windows.Forms.CheckState.Checked; this.cbLimitDate.Location = new System.Drawing.Point(15, 73); this.cbLimitDate.Name = "cbLimitDate"; this.cbLimitDate.Size = new System.Drawing.Size(137, 17); this.cbLimitDate.TabIndex = 5; this.cbLimitDate.Text = "Only media posted after"; this.cbLimitDate.UseVisualStyleBackColor = true; + this.cbLimitDate.CheckedChanged += new System.EventHandler(this.cbLimitDate_CheckedChanged); // // dtpLimit // @@ -112,6 +115,7 @@ this.btnSearch.TabIndex = 9; this.btnSearch.Text = "..."; this.btnSearch.UseVisualStyleBackColor = true; + this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click); // // lbPath // @@ -122,12 +126,12 @@ this.lbPath.TabIndex = 8; this.lbPath.Text = "Path:"; // - // textBox1 + // tbxPath // - this.textBox1.Location = new System.Drawing.Point(50, 107); - this.textBox1.Name = "textBox1"; - this.textBox1.Size = new System.Drawing.Size(342, 20); - this.textBox1.TabIndex = 7; + this.tbxPath.Location = new System.Drawing.Point(50, 107); + this.tbxPath.Name = "tbxPath"; + this.tbxPath.Size = new System.Drawing.Size(342, 20); + this.tbxPath.TabIndex = 7; // // btnDownload // @@ -137,14 +141,15 @@ this.btnDownload.TabIndex = 10; this.btnDownload.Text = "Start downloading"; this.btnDownload.UseVisualStyleBackColor = true; + this.btnDownload.Click += new System.EventHandler(this.btnDownload_Click); // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(439, 177); + this.ClientSize = new System.Drawing.Size(439, 202); this.Controls.Add(this.btnDownload); - this.Controls.Add(this.textBox1); + this.Controls.Add(this.tbxPath); this.Controls.Add(this.lbPath); this.Controls.Add(this.btnSearch); this.Controls.Add(this.dtpLimit); @@ -177,7 +182,7 @@ private System.Windows.Forms.DateTimePicker dtpLimit; private System.Windows.Forms.Button btnSearch; private System.Windows.Forms.Label lbPath; - private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.TextBox tbxPath; private System.Windows.Forms.Button btnDownload; } } \ No newline at end of file diff --git a/Discord Media Loader/MainForm.cs b/Discord Media Loader/MainForm.cs index 06669fd..1ac10e2 100644 --- a/Discord Media Loader/MainForm.cs +++ b/Discord Media Loader/MainForm.cs @@ -2,8 +2,11 @@ using System.Collections.Generic; using System.ComponentModel; using System.Data; +using System.Diagnostics; using System.Drawing; +using System.IO; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; @@ -72,8 +75,7 @@ namespace Discord_Media_Loader } else { - foreach (var guild in Client.Servers) - cbGuilds.Items.Add(guild.Name); + cbGuilds.Items.AddRange((from g in Client.Servers orderby g.Name select g.Name).ToArray()); cbGuilds.SelectedIndex = 0; @@ -101,10 +103,9 @@ namespace Discord_Media_Loader if (guild != null) { cbChannels.Items.Clear(); - foreach (var channel in guild.TextChannels) - cbChannels.Items.Add(channel.Name); + cbChannels.Items.AddRange((from c in guild.TextChannels orderby c.Position select c.Name).ToArray()); - cbChannels.Text = guild.TextChannels.First()?.Name; + cbChannels.SelectedIndex = 0; } } finally @@ -112,5 +113,90 @@ namespace Discord_Media_Loader Cursor = Cursors.Default; } } + + private void cbLimitDate_CheckedChanged(object sender, EventArgs e) + { + dtpLimit.Enabled = cbLimitDate.Checked; + } + + private void btnSearch_Click(object sender, EventArgs e) + { + var dlg = new FolderBrowserDialog(); + if (dlg.ShowDialog() == DialogResult.OK) + { + tbxPath.Text = dlg.SelectedPath; + } + } + + private async void btnDownload_Click(object sender, EventArgs e) + { + var path = tbxPath.Text; + var useStopDate = cbLimitDate.Checked; + var stopDate = dtpLimit.Value; + + if (!Directory.Exists(path)) + { + MessageBox.Show("Please enter an existing directory."); + return; + } + + Enabled = false; + + var guild = FindServerByName(cbGuilds.Text); + var channel = FindChannelByName(guild, cbChannels.Text); + + var clients = new List(); + + var limit = 100; + var stop = false; + ulong lastId = ulong.MaxValue; + var isFirst = true; + + while (!stop) + { + Discord.Message[] messages; + + if (isFirst) + messages = await channel.DownloadMessages(limit, null, Relative.Before, true); + else + messages = await channel.DownloadMessages(limit, lastId, Relative.Before, true); + + isFirst = false; + + foreach (var m in messages) + { + if (m.Id < lastId) + lastId = m.Id; + + if (useStopDate && m.Timestamp < stopDate.Date) + { + stop = true; + continue; + } + + foreach (var a in m.Attachments) + { + var wc = new WebClient(); + clients.Add(wc); + + wc.DownloadFileCompleted += (wcSender, wcE) => clients.Remove((WebClient)wcSender); + wc.DownloadFile(new Uri(a.Url), $@"{path}\{a.Filename}"); + } + } + + stop = stop || messages.Length < limit; + } + + await Task.Run(() => + { + while (clients.Count > 0) + { + // wait until download finished + } + }); + + Enabled = true; + Process.Start(path); + } } } diff --git a/Discord Media Loader/Properties/AssemblyInfo.cs b/Discord Media Loader/Properties/AssemblyInfo.cs index 8e8f6ae..f5e729e 100644 --- a/Discord Media Loader/Properties/AssemblyInfo.cs +++ b/Discord Media Loader/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.1.0.22")] -[assembly: AssemblyFileVersion("0.1.0.22")] +[assembly: AssemblyVersion("0.1.0.28")] +[assembly: AssemblyFileVersion("0.1.0.28")]