Files
ytdlp-gui/Download.cs
2025-05-20 02:11:57 +02:00

87 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Policy;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ytdlp_gui
{
public partial class Download : Form
{
public Download(string url)
{
ytdlp = new YTdlp();
this.url = url;
InitializeComponent();
}
private void Download_Load(object sender, EventArgs e)
{
// Get windows user download folder
string user = Environment.GetEnvironmentVariable("USERNAME");
string workdir = @"C:\Users\"+user+@"\Downloads\";
process = ytdlp.Download(url, false, workdir);
process.Start += (_sender, start_event) =>
{
lblStatus.Text = String.Format(
"Connecting to %s...",
start_event.provider
);
};
process.Progress += (_sender, prog_event) =>
{
int progress = (int)(prog_event.progress * 100 + 0.5);
this.Text = "Downloading - " + progress.ToString() + "%";
lblStatus.Text = prog_event.file;
progressBar.Value = progress;
};
process.Finished += (_sender, finish_event) =>
{
Regex regex = new Regex(@"^(.+)\s\[\w+\](\.[\w.]+)$");
Match match = regex.Match(finish_event.file);
if (!match.Success)
{
MessageBox.Show(this, "Could not find the file we just downloaded...", "Uhhh...");
this.Close();
}
string newFile = match.Groups[1].Value + match.Groups[2].Value;
string filePath = Path.Combine(workdir, finish_event.file);
string newFilePath = Path.Combine(workdir, newFile);
System.IO.File.Move(filePath, newFilePath);
System.IO.File.SetLastWriteTime(newFilePath, DateTime.Now);
this.Close();
};
// Can run in background :)
_ = process.Run();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
protected string url;
protected YTdlp ytdlp;
protected YTdlpProcess process;
}
}