First Try Download

This commit is contained in:
Serraniel 2017-01-28 22:28:23 +01:00
parent 2c27aa139c
commit c6cbafbf61
3 changed files with 107 additions and 16 deletions

View file

@ -38,7 +38,7 @@
this.dtpLimit = new System.Windows.Forms.DateTimePicker(); this.dtpLimit = new System.Windows.Forms.DateTimePicker();
this.btnSearch = new System.Windows.Forms.Button(); this.btnSearch = new System.Windows.Forms.Button();
this.lbPath = new System.Windows.Forms.Label(); 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.btnDownload = new System.Windows.Forms.Button();
this.SuspendLayout(); this.SuspendLayout();
// //
@ -89,12 +89,15 @@
// cbLimitDate // cbLimitDate
// //
this.cbLimitDate.AutoSize = true; 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.Location = new System.Drawing.Point(15, 73);
this.cbLimitDate.Name = "cbLimitDate"; this.cbLimitDate.Name = "cbLimitDate";
this.cbLimitDate.Size = new System.Drawing.Size(137, 17); this.cbLimitDate.Size = new System.Drawing.Size(137, 17);
this.cbLimitDate.TabIndex = 5; this.cbLimitDate.TabIndex = 5;
this.cbLimitDate.Text = "Only media posted after"; this.cbLimitDate.Text = "Only media posted after";
this.cbLimitDate.UseVisualStyleBackColor = true; this.cbLimitDate.UseVisualStyleBackColor = true;
this.cbLimitDate.CheckedChanged += new System.EventHandler(this.cbLimitDate_CheckedChanged);
// //
// dtpLimit // dtpLimit
// //
@ -112,6 +115,7 @@
this.btnSearch.TabIndex = 9; this.btnSearch.TabIndex = 9;
this.btnSearch.Text = "..."; this.btnSearch.Text = "...";
this.btnSearch.UseVisualStyleBackColor = true; this.btnSearch.UseVisualStyleBackColor = true;
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
// //
// lbPath // lbPath
// //
@ -122,12 +126,12 @@
this.lbPath.TabIndex = 8; this.lbPath.TabIndex = 8;
this.lbPath.Text = "Path:"; this.lbPath.Text = "Path:";
// //
// textBox1 // tbxPath
// //
this.textBox1.Location = new System.Drawing.Point(50, 107); this.tbxPath.Location = new System.Drawing.Point(50, 107);
this.textBox1.Name = "textBox1"; this.tbxPath.Name = "tbxPath";
this.textBox1.Size = new System.Drawing.Size(342, 20); this.tbxPath.Size = new System.Drawing.Size(342, 20);
this.textBox1.TabIndex = 7; this.tbxPath.TabIndex = 7;
// //
// btnDownload // btnDownload
// //
@ -137,14 +141,15 @@
this.btnDownload.TabIndex = 10; this.btnDownload.TabIndex = 10;
this.btnDownload.Text = "Start downloading"; this.btnDownload.Text = "Start downloading";
this.btnDownload.UseVisualStyleBackColor = true; this.btnDownload.UseVisualStyleBackColor = true;
this.btnDownload.Click += new System.EventHandler(this.btnDownload_Click);
// //
// MainForm // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 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.btnDownload);
this.Controls.Add(this.textBox1); this.Controls.Add(this.tbxPath);
this.Controls.Add(this.lbPath); this.Controls.Add(this.lbPath);
this.Controls.Add(this.btnSearch); this.Controls.Add(this.btnSearch);
this.Controls.Add(this.dtpLimit); this.Controls.Add(this.dtpLimit);
@ -177,7 +182,7 @@
private System.Windows.Forms.DateTimePicker dtpLimit; private System.Windows.Forms.DateTimePicker dtpLimit;
private System.Windows.Forms.Button btnSearch; private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.Label lbPath; private System.Windows.Forms.Label lbPath;
private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox tbxPath;
private System.Windows.Forms.Button btnDownload; private System.Windows.Forms.Button btnDownload;
} }
} }

View file

@ -2,8 +2,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -72,8 +75,7 @@ namespace Discord_Media_Loader
} }
else else
{ {
foreach (var guild in Client.Servers) cbGuilds.Items.AddRange((from g in Client.Servers orderby g.Name select g.Name).ToArray());
cbGuilds.Items.Add(guild.Name);
cbGuilds.SelectedIndex = 0; cbGuilds.SelectedIndex = 0;
@ -101,10 +103,9 @@ namespace Discord_Media_Loader
if (guild != null) if (guild != null)
{ {
cbChannels.Items.Clear(); cbChannels.Items.Clear();
foreach (var channel in guild.TextChannels) cbChannels.Items.AddRange((from c in guild.TextChannels orderby c.Position select c.Name).ToArray());
cbChannels.Items.Add(channel.Name);
cbChannels.Text = guild.TextChannels.First()?.Name; cbChannels.SelectedIndex = 0;
} }
} }
finally finally
@ -112,5 +113,90 @@ namespace Discord_Media_Loader
Cursor = Cursors.Default; 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<WebClient>();
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);
}
} }
} }

View file

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben: // übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.0.22")] [assembly: AssemblyVersion("0.1.0.28")]
[assembly: AssemblyFileVersion("0.1.0.22")] [assembly: AssemblyFileVersion("0.1.0.28")]