DiscordMediaLoader/Discord Media Loader/MainForm.cs

298 lines
9.6 KiB
C#
Raw Normal View History

2017-01-28 16:51:55 +01:00
using System;
using System.Collections.Generic;
2017-01-28 22:28:23 +01:00
using System.Diagnostics;
using System.IO;
2017-01-28 16:51:55 +01:00
using System.Linq;
2017-01-28 22:28:23 +01:00
using System.Net;
2017-01-29 09:34:04 +01:00
using System.Reflection;
2017-01-28 16:51:55 +01:00
using System.Threading.Tasks;
using System.Windows.Forms;
2017-01-28 18:13:30 +01:00
using Discord;
using Discord.Net;
using ConnectionState = Discord.ConnectionState;
2017-01-28 16:51:55 +01:00
namespace Discord_Media_Loader
{
public partial class MainForm : Form
{
2017-01-28 18:13:30 +01:00
private DiscordClient Client { get; } = new DiscordClient();
2017-01-29 09:34:04 +01:00
private event EventHandler<UpdateProgessEventArgs> UpdateProgress;
2017-01-29 10:02:14 +01:00
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
2017-01-28 16:51:55 +01:00
public MainForm()
{
InitializeComponent();
2017-01-29 09:34:04 +01:00
UpdateProgress += (s, e) =>
{
SetControlPropertyThreadSafe(lbDownload, "Text", $"Files downloaded: {e.Downloaded}");
SetControlPropertyThreadSafe(lbScanCount, "Text", $"Messages scanned: {e.Scanned}");
};
}
private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
private static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
{
if (control.InvokeRequired)
{
2017-01-29 10:20:54 +01:00
control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), control, propertyName, propertyValue);
2017-01-29 09:34:04 +01:00
}
else
{
2017-01-29 10:20:54 +01:00
control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new[] { propertyValue });
2017-01-29 09:34:04 +01:00
}
2017-01-28 16:51:55 +01:00
}
2017-01-28 18:13:30 +01:00
public async Task<bool> Login()
{
var email = Properties.Settings.Default.email;
var abort = false;
while (Client.State != ConnectionState.Connected && !abort)
{
2017-01-29 10:20:54 +01:00
string password;
2017-01-28 18:13:30 +01:00
if (LoginForm.Exec(ref email, out password))
{
try
{
Cursor = Cursors.WaitCursor;
try
{
await Client.Connect(email, password);
Properties.Settings.Default.email = email;
Properties.Settings.Default.Save();
}
finally
{
Cursor = Cursors.Default;
}
}
2017-01-29 10:20:54 +01:00
catch (HttpException)
2017-01-28 18:13:30 +01:00
{
// ignore http exception on invalid login
}
}
else
{
abort = true;
}
}
return !abort;
}
private async void MainForm_Shown(object sender, EventArgs e)
{
2017-01-29 10:20:54 +01:00
lbVersion.Text = $"v{Assembly.GetExecutingAssembly().GetName().Version}";
2017-01-29 09:34:04 +01:00
SetEnabled(false);
2017-01-28 18:13:30 +01:00
if (!await Login())
{
Close();
}
else
{
2017-01-28 22:28:23 +01:00
cbGuilds.Items.AddRange((from g in Client.Servers orderby g.Name select g.Name).ToArray());
2017-01-28 18:13:30 +01:00
cbGuilds.SelectedIndex = 0;
2017-01-29 09:34:04 +01:00
lbUsername.Text = $"Username: {Client.CurrentUser.Name}#{Client.CurrentUser.Discriminator}";
2017-01-28 18:13:30 +01:00
2017-01-29 09:34:04 +01:00
SetEnabled(true);
2017-01-28 18:13:30 +01:00
}
}
private Server FindServerByName(string name)
{
return (from s in Client.Servers where s.Name == name select s).FirstOrDefault();
}
private Channel FindChannelByName(Server server, string name)
{
return (from c in server.TextChannels where c.Name == name select c).FirstOrDefault();
}
2017-01-29 09:34:04 +01:00
private void SetEnabled(bool enabled)
{
foreach (Control c in Controls)
{
SetControlPropertyThreadSafe(c, "Enabled", enabled);
//c.Enabled = enabled;
}
}
2017-01-28 18:13:30 +01:00
private void cbGuilds_SelectedIndexChanged(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
try
{
Server guild = FindServerByName(cbGuilds.Text);
if (guild != null)
{
cbChannels.Items.Clear();
2017-01-28 22:28:23 +01:00
cbChannels.Items.AddRange((from c in guild.TextChannels orderby c.Position select c.Name).ToArray());
2017-01-28 18:13:30 +01:00
2017-01-28 22:28:23 +01:00
cbChannels.SelectedIndex = 0;
2017-01-28 18:13:30 +01:00
}
}
finally
{
Cursor = Cursors.Default;
}
}
2017-01-28 22:28:23 +01:00
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;
}
}
2017-01-29 09:34:04 +01:00
private void OnUpdateProgress(UpdateProgessEventArgs e)
{
EventHandler<UpdateProgessEventArgs> handler = UpdateProgress;
2017-01-29 10:20:54 +01:00
handler?.Invoke(this, e);
2017-01-29 09:34:04 +01:00
}
2017-01-29 10:02:14 +01:00
private static long DateTimeToUnixTimeStamp(DateTime dateTime)
{
TimeSpan elapsedTime = dateTime - Epoch;
return (long)elapsedTime.TotalSeconds;
}
2017-01-29 09:34:04 +01:00
private void btnDownload_Click(object sender, EventArgs e)
2017-01-28 22:28:23 +01:00
{
var path = tbxPath.Text;
var useStopDate = cbLimitDate.Checked;
var stopDate = dtpLimit.Value;
2017-01-29 09:34:04 +01:00
var threadLimit = nupThreadCount.Value;
2017-01-29 10:02:14 +01:00
var skipExisting = cbSkip.Checked;
2017-01-28 22:28:23 +01:00
if (!Directory.Exists(path))
{
MessageBox.Show("Please enter an existing directory.");
return;
}
2017-01-29 09:34:04 +01:00
SetEnabled(false);
2017-01-28 22:28:23 +01:00
var guild = FindServerByName(cbGuilds.Text);
var channel = FindChannelByName(guild, cbChannels.Text);
var clients = new List<WebClient>();
var limit = 100;
var stop = false;
2017-01-29 10:20:54 +01:00
var lastId = ulong.MaxValue;
2017-01-28 22:28:23 +01:00
var isFirst = true;
2017-01-29 09:34:04 +01:00
ulong msgScanCount = 0;
ulong downloadCount = 0;
var locker = new object();
2017-01-28 22:28:23 +01:00
2017-01-29 09:34:04 +01:00
Task.Run(async () =>
{
2017-01-28 22:28:23 +01:00
2017-01-29 09:34:04 +01:00
while (!stop)
2017-01-28 22:28:23 +01:00
{
2017-01-29 09:34:04 +01:00
Discord.Message[] messages;
2017-01-28 22:28:23 +01:00
2017-01-29 09:34:04 +01:00
if (isFirst)
2017-01-29 10:20:54 +01:00
messages = await channel.DownloadMessages(limit, null);
2017-01-29 09:34:04 +01:00
else
2017-01-29 10:20:54 +01:00
messages = await channel.DownloadMessages(limit, lastId);
2017-01-28 22:28:23 +01:00
2017-01-29 09:34:04 +01:00
isFirst = false;
foreach (var m in messages)
2017-01-28 22:28:23 +01:00
{
2017-01-29 09:34:04 +01:00
if (m.Id < lastId)
lastId = m.Id;
if (useStopDate && m.Timestamp < stopDate.Date)
2017-01-28 22:44:17 +01:00
{
2017-01-29 09:34:04 +01:00
stop = true;
continue;
2017-01-28 22:44:17 +01:00
}
2017-01-28 22:28:23 +01:00
2017-01-29 09:34:04 +01:00
foreach (var a in m.Attachments)
2017-01-28 22:44:17 +01:00
{
2017-01-29 10:02:14 +01:00
if (!path.EndsWith(@"\"))
path += @"\";
var fname = $"{guild.Name}_{channel.Name}_{DateTimeToUnixTimeStamp(m.Timestamp)}_{a.Filename}";
fname = Path.GetInvalidFileNameChars().Aggregate(fname, (current, c) => current.Replace(c, '-'));
fname = path + fname;
if (skipExisting && File.Exists(fname))
continue;
2017-01-29 09:34:04 +01:00
while (clients.Count >= threadLimit)
{
// 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 });
}
};
2017-01-29 10:02:14 +01:00
wc.DownloadFileAsync(new Uri(a.Url), fname);
2017-01-29 09:34:04 +01:00
}
msgScanCount++;
2017-01-29 10:02:14 +01:00
OnUpdateProgress(new UpdateProgessEventArgs() { Downloaded = downloadCount, Scanned = msgScanCount});
2017-01-28 22:28:23 +01:00
}
2017-01-29 09:34:04 +01:00
stop = stop || messages.Length < limit;
}
2017-01-28 22:28:23 +01:00
2017-01-29 09:34:04 +01:00
await Task.Run(() =>
2017-01-28 22:28:23 +01:00
{
2017-01-29 09:34:04 +01:00
while (clients.Count > 0)
{
// wait until download finished
}
});
2017-01-28 22:28:23 +01:00
2017-01-29 10:20:54 +01:00
Process.Start(path);
2017-01-29 09:34:04 +01:00
SetEnabled(true);
});
2017-01-28 22:28:23 +01:00
}
2017-01-29 10:20:54 +01:00
private void lbGithub_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("https://github.com/Serraniel/DiscordMediaLoader/releases");
}
private void lbAbout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MessageBox.Show(Properties.Resources.AboutString);
}
2017-01-28 16:51:55 +01:00
}
2017-01-29 09:34:04 +01:00
internal class UpdateProgessEventArgs : EventArgs
{
2017-01-29 10:20:54 +01:00
internal ulong Scanned { get; set; }
internal ulong Downloaded { get; set; }
2017-01-29 09:34:04 +01:00
}
2017-01-28 16:51:55 +01:00
}