Things I have done over night

This commit is contained in:
Serraniel 2017-01-29 09:34:04 +01:00
parent a7c60b23e0
commit c25f5bbb0a
3 changed files with 274 additions and 129 deletions

View file

@ -29,62 +29,68 @@
private void InitializeComponent() private void InitializeComponent()
{ {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.lbGuild = new System.Windows.Forms.Label();
this.cbGuilds = new System.Windows.Forms.ComboBox();
this.lbUsername = new System.Windows.Forms.Label();
this.cbChannels = new System.Windows.Forms.ComboBox();
this.lbChannel = new System.Windows.Forms.Label();
this.cbLimitDate = new System.Windows.Forms.CheckBox();
this.dtpLimit = new System.Windows.Forms.DateTimePicker();
this.btnSearch = new System.Windows.Forms.Button();
this.lbPath = new System.Windows.Forms.Label(); this.lbPath = new System.Windows.Forms.Label();
this.btnSearch = new System.Windows.Forms.Button();
this.tbxPath = new System.Windows.Forms.TextBox(); this.tbxPath = new System.Windows.Forms.TextBox();
this.dtpLimit = new System.Windows.Forms.DateTimePicker();
this.btnDownload = new System.Windows.Forms.Button(); this.btnDownload = new System.Windows.Forms.Button();
this.cbLimitDate = new System.Windows.Forms.CheckBox();
this.lbThread = new System.Windows.Forms.Label();
this.cbChannels = new System.Windows.Forms.ComboBox();
this.nupThreadCount = new System.Windows.Forms.NumericUpDown();
this.lbChannel = new System.Windows.Forms.Label();
this.lbScanCount = new System.Windows.Forms.Label();
this.lbUsername = new System.Windows.Forms.Label();
this.lbDownload = new System.Windows.Forms.Label();
this.cbGuilds = new System.Windows.Forms.ComboBox();
this.lbGuild = new System.Windows.Forms.Label();
this.pgbProgress = new System.Windows.Forms.ProgressBar();
((System.ComponentModel.ISupportInitialize)(this.nupThreadCount)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// lbGuild // lbPath
// //
this.lbGuild.AutoSize = true; this.lbPath.AutoSize = true;
this.lbGuild.Location = new System.Drawing.Point(12, 35); this.lbPath.Location = new System.Drawing.Point(12, 110);
this.lbGuild.Name = "lbGuild"; this.lbPath.Name = "lbPath";
this.lbGuild.Size = new System.Drawing.Size(34, 13); this.lbPath.Size = new System.Drawing.Size(32, 13);
this.lbGuild.TabIndex = 0; this.lbPath.TabIndex = 8;
this.lbGuild.Text = "Guild:"; this.lbPath.Text = "Path:";
// //
// cbGuilds // btnSearch
// //
this.cbGuilds.FormattingEnabled = true; this.btnSearch.Location = new System.Drawing.Point(403, 105);
this.cbGuilds.Location = new System.Drawing.Point(52, 32); this.btnSearch.Name = "btnSearch";
this.cbGuilds.Name = "cbGuilds"; this.btnSearch.Size = new System.Drawing.Size(25, 23);
this.cbGuilds.Size = new System.Drawing.Size(164, 21); this.btnSearch.TabIndex = 9;
this.cbGuilds.TabIndex = 1; this.btnSearch.Text = "...";
this.cbGuilds.SelectedIndexChanged += new System.EventHandler(this.cbGuilds_SelectedIndexChanged); this.btnSearch.UseVisualStyleBackColor = true;
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
// //
// lbUsername // tbxPath
// //
this.lbUsername.AutoSize = true; this.tbxPath.Location = new System.Drawing.Point(50, 107);
this.lbUsername.Location = new System.Drawing.Point(12, 9); this.tbxPath.Name = "tbxPath";
this.lbUsername.Name = "lbUsername"; this.tbxPath.Size = new System.Drawing.Size(342, 20);
this.lbUsername.Size = new System.Drawing.Size(58, 13); this.tbxPath.TabIndex = 7;
this.lbUsername.TabIndex = 2;
this.lbUsername.Text = "Username:";
// //
// cbChannels // dtpLimit
// //
this.cbChannels.FormattingEnabled = true; this.dtpLimit.Format = System.Windows.Forms.DateTimePickerFormat.Short;
this.cbChannels.Location = new System.Drawing.Point(264, 32); this.dtpLimit.Location = new System.Drawing.Point(158, 70);
this.cbChannels.Name = "cbChannels"; this.dtpLimit.Name = "dtpLimit";
this.cbChannels.Size = new System.Drawing.Size(164, 21); this.dtpLimit.Size = new System.Drawing.Size(95, 20);
this.cbChannels.TabIndex = 4; this.dtpLimit.TabIndex = 6;
// //
// lbChannel // btnDownload
// //
this.lbChannel.AutoSize = true; this.btnDownload.Location = new System.Drawing.Point(12, 167);
this.lbChannel.Location = new System.Drawing.Point(224, 38); this.btnDownload.Name = "btnDownload";
this.lbChannel.Name = "lbChannel"; this.btnDownload.Size = new System.Drawing.Size(415, 23);
this.lbChannel.Size = new System.Drawing.Size(34, 13); this.btnDownload.TabIndex = 10;
this.lbChannel.TabIndex = 3; this.btnDownload.Text = "Start downloading";
this.lbChannel.Text = "Guild:"; this.btnDownload.UseVisualStyleBackColor = true;
this.btnDownload.Click += new System.EventHandler(this.btnDownload_Click);
// //
// cbLimitDate // cbLimitDate
// //
@ -99,55 +105,106 @@
this.cbLimitDate.UseVisualStyleBackColor = true; this.cbLimitDate.UseVisualStyleBackColor = true;
this.cbLimitDate.CheckedChanged += new System.EventHandler(this.cbLimitDate_CheckedChanged); this.cbLimitDate.CheckedChanged += new System.EventHandler(this.cbLimitDate_CheckedChanged);
// //
// dtpLimit // lbThread
// //
this.dtpLimit.Format = System.Windows.Forms.DateTimePickerFormat.Short; this.lbThread.AutoSize = true;
this.dtpLimit.Location = new System.Drawing.Point(158, 70); this.lbThread.Location = new System.Drawing.Point(12, 141);
this.dtpLimit.Name = "dtpLimit"; this.lbThread.Name = "lbThread";
this.dtpLimit.Size = new System.Drawing.Size(95, 20); this.lbThread.Size = new System.Drawing.Size(64, 13);
this.dtpLimit.TabIndex = 6; this.lbThread.TabIndex = 11;
this.lbThread.Text = "Thread limit:";
// //
// btnSearch // cbChannels
// //
this.btnSearch.Location = new System.Drawing.Point(403, 105); this.cbChannels.FormattingEnabled = true;
this.btnSearch.Name = "btnSearch"; this.cbChannels.Location = new System.Drawing.Point(264, 32);
this.btnSearch.Size = new System.Drawing.Size(25, 23); this.cbChannels.Name = "cbChannels";
this.btnSearch.TabIndex = 9; this.cbChannels.Size = new System.Drawing.Size(164, 21);
this.btnSearch.Text = "..."; this.cbChannels.TabIndex = 4;
this.btnSearch.UseVisualStyleBackColor = true;
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
// //
// lbPath // nupThreadCount
// //
this.lbPath.AutoSize = true; this.nupThreadCount.Location = new System.Drawing.Point(82, 139);
this.lbPath.Location = new System.Drawing.Point(12, 110); this.nupThreadCount.Name = "nupThreadCount";
this.lbPath.Name = "lbPath"; this.nupThreadCount.Size = new System.Drawing.Size(70, 20);
this.lbPath.Size = new System.Drawing.Size(32, 13); this.nupThreadCount.TabIndex = 12;
this.lbPath.TabIndex = 8; this.nupThreadCount.Value = new decimal(new int[] {
this.lbPath.Text = "Path:"; 50,
0,
0,
0});
// //
// tbxPath // lbChannel
// //
this.tbxPath.Location = new System.Drawing.Point(50, 107); this.lbChannel.AutoSize = true;
this.tbxPath.Name = "tbxPath"; this.lbChannel.Location = new System.Drawing.Point(224, 38);
this.tbxPath.Size = new System.Drawing.Size(342, 20); this.lbChannel.Name = "lbChannel";
this.tbxPath.TabIndex = 7; this.lbChannel.Size = new System.Drawing.Size(34, 13);
this.lbChannel.TabIndex = 3;
this.lbChannel.Text = "Guild:";
// //
// btnDownload // lbScanCount
// //
this.btnDownload.Location = new System.Drawing.Point(12, 142); this.lbScanCount.AutoSize = true;
this.btnDownload.Name = "btnDownload"; this.lbScanCount.Location = new System.Drawing.Point(12, 203);
this.btnDownload.Size = new System.Drawing.Size(416, 23); this.lbScanCount.Name = "lbScanCount";
this.btnDownload.TabIndex = 10; this.lbScanCount.Size = new System.Drawing.Size(102, 13);
this.btnDownload.Text = "Start downloading"; this.lbScanCount.TabIndex = 13;
this.btnDownload.UseVisualStyleBackColor = true; this.lbScanCount.Text = "Messages scanned:";
this.btnDownload.Click += new System.EventHandler(this.btnDownload_Click); //
// lbUsername
//
this.lbUsername.AutoSize = true;
this.lbUsername.Location = new System.Drawing.Point(12, 9);
this.lbUsername.Name = "lbUsername";
this.lbUsername.Size = new System.Drawing.Size(58, 13);
this.lbUsername.TabIndex = 2;
this.lbUsername.Text = "Username:";
//
// lbDownload
//
this.lbDownload.AutoSize = true;
this.lbDownload.Location = new System.Drawing.Point(234, 203);
this.lbDownload.Name = "lbDownload";
this.lbDownload.Size = new System.Drawing.Size(92, 13);
this.lbDownload.TabIndex = 14;
this.lbDownload.Text = "Files downloaded:";
//
// cbGuilds
//
this.cbGuilds.FormattingEnabled = true;
this.cbGuilds.Location = new System.Drawing.Point(52, 32);
this.cbGuilds.Name = "cbGuilds";
this.cbGuilds.Size = new System.Drawing.Size(164, 21);
this.cbGuilds.TabIndex = 1;
this.cbGuilds.SelectedIndexChanged += new System.EventHandler(this.cbGuilds_SelectedIndexChanged);
//
// lbGuild
//
this.lbGuild.AutoSize = true;
this.lbGuild.Location = new System.Drawing.Point(12, 35);
this.lbGuild.Name = "lbGuild";
this.lbGuild.Size = new System.Drawing.Size(34, 13);
this.lbGuild.TabIndex = 0;
this.lbGuild.Text = "Guild:";
//
// pgbProgress
//
this.pgbProgress.Location = new System.Drawing.Point(15, 219);
this.pgbProgress.Name = "pgbProgress";
this.pgbProgress.Size = new System.Drawing.Size(413, 23);
this.pgbProgress.TabIndex = 15;
// //
// 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, 202); this.ClientSize = new System.Drawing.Size(439, 253);
this.Controls.Add(this.pgbProgress);
this.Controls.Add(this.lbDownload);
this.Controls.Add(this.lbScanCount);
this.Controls.Add(this.nupThreadCount);
this.Controls.Add(this.lbThread);
this.Controls.Add(this.btnDownload); this.Controls.Add(this.btnDownload);
this.Controls.Add(this.tbxPath); this.Controls.Add(this.tbxPath);
this.Controls.Add(this.lbPath); this.Controls.Add(this.lbPath);
@ -166,6 +223,7 @@
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Discord Media Loader"; this.Text = "Discord Media Loader";
this.Shown += new System.EventHandler(this.MainForm_Shown); this.Shown += new System.EventHandler(this.MainForm_Shown);
((System.ComponentModel.ISupportInitialize)(this.nupThreadCount)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -173,16 +231,21 @@
#endregion #endregion
private System.Windows.Forms.Label lbGuild;
private System.Windows.Forms.ComboBox cbGuilds;
private System.Windows.Forms.Label lbUsername;
private System.Windows.Forms.ComboBox cbChannels;
private System.Windows.Forms.Label lbChannel;
private System.Windows.Forms.CheckBox cbLimitDate;
private System.Windows.Forms.DateTimePicker dtpLimit;
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.Label lbPath; private System.Windows.Forms.Label lbPath;
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.TextBox tbxPath; private System.Windows.Forms.TextBox tbxPath;
private System.Windows.Forms.DateTimePicker dtpLimit;
private System.Windows.Forms.Button btnDownload; private System.Windows.Forms.Button btnDownload;
private System.Windows.Forms.CheckBox cbLimitDate;
private System.Windows.Forms.Label lbThread;
private System.Windows.Forms.ComboBox cbChannels;
private System.Windows.Forms.NumericUpDown nupThreadCount;
private System.Windows.Forms.Label lbChannel;
private System.Windows.Forms.Label lbScanCount;
private System.Windows.Forms.Label lbUsername;
private System.Windows.Forms.Label lbDownload;
private System.Windows.Forms.ComboBox cbGuilds;
private System.Windows.Forms.Label lbGuild;
private System.Windows.Forms.ProgressBar pgbProgress;
} }
} }

View file

@ -8,6 +8,7 @@ using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -22,9 +23,34 @@ namespace Discord_Media_Loader
public partial class MainForm : Form public partial class MainForm : Form
{ {
private DiscordClient Client { get; } = new DiscordClient(); private DiscordClient Client { get; } = new DiscordClient();
private event EventHandler<UpdateProgessEventArgs> UpdateProgress;
public MainForm() public MainForm()
{ {
InitializeComponent(); InitializeComponent();
UpdateProgress += (s, e) =>
{
SetControlPropertyThreadSafe(lbDownload, "Text", $"Files downloaded: {e.Downloaded}");
SetControlPropertyThreadSafe(lbScanCount, "Text", $"Messages scanned: {e.Scanned}");
//SetControlPropertyThreadSafe(pgbProgress, "Max", e.MaxProgress);
//SetControlPropertyThreadSafe(pgbProgress, "Value", e.Progress);
};
}
private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
private static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
{
if (control.InvokeRequired)
{
control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe),
new object[] { control, propertyName, propertyValue });
}
else
{
control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
}
} }
public async Task<bool> Login() public async Task<bool> Login()
@ -69,7 +95,7 @@ namespace Discord_Media_Loader
private async void MainForm_Shown(object sender, EventArgs e) private async void MainForm_Shown(object sender, EventArgs e)
{ {
Enabled = false; SetEnabled(false);
if (!await Login()) if (!await Login())
{ {
@ -78,10 +104,10 @@ namespace Discord_Media_Loader
else else
{ {
cbGuilds.Items.AddRange((from g in Client.Servers orderby g.Name select g.Name).ToArray()); cbGuilds.Items.AddRange((from g in Client.Servers orderby g.Name select g.Name).ToArray());
cbGuilds.SelectedIndex = 0; cbGuilds.SelectedIndex = 0;
lbUsername.Text = $"Username: {Client.CurrentUser.Name}#{Client.CurrentUser.Discriminator}";
Enabled = true; SetEnabled(true);
} }
} }
@ -95,6 +121,15 @@ namespace Discord_Media_Loader
return (from c in server.TextChannels where c.Name == name select c).FirstOrDefault(); return (from c in server.TextChannels where c.Name == name select c).FirstOrDefault();
} }
private void SetEnabled(bool enabled)
{
foreach (Control c in Controls)
{
SetControlPropertyThreadSafe(c, "Enabled", enabled);
//c.Enabled = enabled;
}
}
private void cbGuilds_SelectedIndexChanged(object sender, EventArgs e) private void cbGuilds_SelectedIndexChanged(object sender, EventArgs e)
{ {
Cursor = Cursors.WaitCursor; Cursor = Cursors.WaitCursor;
@ -130,11 +165,21 @@ namespace Discord_Media_Loader
} }
} }
private async void btnDownload_Click(object sender, EventArgs e) private void OnUpdateProgress(UpdateProgessEventArgs e)
{
EventHandler<UpdateProgessEventArgs> handler = UpdateProgress;
if (handler != null)
{
handler(this, e);
}
}
private void btnDownload_Click(object sender, EventArgs e)
{ {
var path = tbxPath.Text; var path = tbxPath.Text;
var useStopDate = cbLimitDate.Checked; var useStopDate = cbLimitDate.Checked;
var stopDate = dtpLimit.Value; var stopDate = dtpLimit.Value;
var threadLimit = nupThreadCount.Value;
if (!Directory.Exists(path)) if (!Directory.Exists(path))
{ {
@ -142,7 +187,7 @@ namespace Discord_Media_Loader
return; return;
} }
Enabled = false; SetEnabled(false);
var guild = FindServerByName(cbGuilds.Text); var guild = FindServerByName(cbGuilds.Text);
var channel = FindChannelByName(guild, cbChannels.Text); var channel = FindChannelByName(guild, cbChannels.Text);
@ -154,58 +199,95 @@ namespace Discord_Media_Loader
ulong lastId = ulong.MaxValue; ulong lastId = ulong.MaxValue;
var isFirst = true; var isFirst = true;
while (!stop) ulong msgScanCount = 0;
ulong fileFound = 0;
ulong downloadCount = 0;
var locker = new object();
var timeDiffSteps = (uint)Math.Floor(int.MaxValue / 2 / (DateTime.Now - dtpLimit.Value.Date).TotalHours);
uint progress = 0;
Task.Run(async () =>
{ {
Discord.Message[] messages;
if (isFirst) while (!stop)
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) Discord.Message[] messages;
lastId = m.Id;
if (useStopDate && m.Timestamp < stopDate.Date) if (isFirst)
{ messages = await channel.DownloadMessages(limit, null, Relative.Before, true);
stop = true; else
continue; messages = await channel.DownloadMessages(limit, lastId, Relative.Before, true);
}
foreach (var a in m.Attachments) isFirst = false;
foreach (var m in messages)
{ {
while (clients.Count > 50) if (m.Id < lastId)
lastId = m.Id;
if (useStopDate && m.Timestamp < stopDate.Date)
{ {
// wait stop = true;
continue;
} }
var wc = new WebClient();
clients.Add(wc);
wc.DownloadFileCompleted += (wcSender, wcE) => foreach (var a in m.Attachments)
{ {
clients.Remove(wc); fileFound++;
}; while (clients.Count >= threadLimit)
wc.DownloadFileAsync(new Uri(a.Url), $@"{path}\{a.Filename}"); {
// wait
}
var wc = new WebClient();
clients.Add(wc);
wc.DownloadFileCompleted += (wcSender, wcE) =>
{
clients.Remove(wc);
lock (locker)
{
downloadCount++;
OnUpdateProgress(new UpdateProgessEventArgs() { Downloaded = downloadCount, Scanned = msgScanCount });
}
};
if (!path.EndsWith(@"\"))
path += @"\";
var fname = $"{guild.Name}_{channel.Name}_{m.Timestamp}_{a.Filename}";
fname = Path.GetInvalidFileNameChars().Aggregate(fname, (current, c) => current.Replace(c, '-'));
wc.DownloadFileAsync(new Uri(a.Url), $@"{path}{fname}");
}
msgScanCount++;
progress += timeDiffSteps;
OnUpdateProgress(new UpdateProgessEventArgs() { Downloaded = downloadCount, Scanned = msgScanCount, Progress = progress });
} }
stop = stop || messages.Length < limit;
} }
stop = stop || messages.Length < limit; await Task.Run(() =>
}
await Task.Run(() =>
{
while (clients.Count > 0)
{ {
// wait until download finished while (clients.Count > 0)
} {
}); // wait until download finished
}
});
Enabled = true; SetEnabled(true);
Process.Start(path); });
} }
} }
internal class UpdateProgessEventArgs : EventArgs
{
internal ulong Scanned { get; set; } = 0;
internal ulong Downloaded { get; set; } = 0;
internal uint Progress { get; set; } = 0;
internal uint MaxProgress { get; set; } = int.MaxValue;
}
} }

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.31")] [assembly: AssemblyVersion("0.1.0.42")]
[assembly: AssemblyFileVersion("0.1.0.31")] [assembly: AssemblyFileVersion("0.1.0.42")]