From 09363537338ed8b65ef3763dab7a7344475566a9 Mon Sep 17 00:00:00 2001 From: Raymond Krehn Date: Fri, 8 Mar 2024 16:42:03 -0700 Subject: [PATCH] 1.5 (#3) * - Re-added timer to check on mp3 file conversions and implemented levenshtein to validate it's a file that needs to be converted - Added right-click menu on datagrid to clear it out - Added right-click menu item to restart failed downloads/conversion - Added right-click menu item to open data file directory - Set default state to "paused" so user can make adjustments/fixes before starting process * - Re-added timer to check on mp3 file conversions and implemented levenshtein to validate it's a file that needs to be converted - Added right-click menu on datagrid to clear it out - Added right-click menu item to restart failed downloads/conversion - Added right-click menu item to open data file directory - Set default state to "paused" so user can make adjustments/fixes before starting process * - Automatically creates the music output directory if it doesn't exist --- Form1.Designer.cs | 46 +++++++++++++++++++++--- Form1.cs | 91 +++++++++++++++++++++++++++++++++++++++-------- Form1.resx | 3 ++ YTPD.csproj | 3 +- 4 files changed, 123 insertions(+), 20 deletions(-) diff --git a/Form1.Designer.cs b/Form1.Designer.cs index db30828..ae7c9ee 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -54,7 +54,12 @@ private void InitializeComponent() timer_convert = new System.Windows.Forms.Timer(components); btn_Pause = new Button(); btn_Resume = new Button(); + contextMenuStrip1 = new ContextMenuStrip(components); + menu_cleardata = new ToolStripMenuItem(); + openDataFileToolStripMenuItem = new ToolStripMenuItem(); + RestartBadItems = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)dgv_downloads).BeginInit(); + contextMenuStrip1.SuspendLayout(); SuspendLayout(); // // label1 @@ -115,6 +120,7 @@ private void InitializeComponent() dgv_downloads.TabIndex = 3; dgv_downloads.CellContentClick += dgv_downloads_CellContentClick; dgv_downloads.CellDoubleClick += dgv_downloads_CellDoubleClick; + dgv_downloads.MouseDown += dgv_downloads_MouseDown; // // Artist // @@ -190,7 +196,7 @@ private void InitializeComponent() txt_Dir.Name = "txt_Dir"; txt_Dir.Size = new Size(583, 23); txt_Dir.TabIndex = 5; - txt_Dir.Text = "C:\\Users\\satsu\\Downloads"; + txt_Dir.Text = "C:\\Downloads"; // // button2 // @@ -205,13 +211,11 @@ private void InitializeComponent() // // timer1 // - timer1.Enabled = true; timer1.Interval = 1000; timer1.Tick += timer1_Tick; // // timer_tag // - timer_tag.Enabled = true; timer_tag.Interval = 10000; timer_tag.Tick += timer_tag_Tick; // @@ -222,7 +226,7 @@ private void InitializeComponent() // // timer_convert // - timer_convert.Interval = 5000; + timer_convert.Interval = 30000; timer_convert.Tick += timer_convert_Tick; // // btn_Pause @@ -233,6 +237,7 @@ private void InitializeComponent() btn_Pause.TabIndex = 7; btn_Pause.Text = "Pause"; btn_Pause.UseVisualStyleBackColor = true; + btn_Pause.Visible = false; btn_Pause.Click += btn_Pause_Click; // // btn_Resume @@ -243,9 +248,35 @@ private void InitializeComponent() btn_Resume.TabIndex = 8; btn_Resume.Text = "Resume"; btn_Resume.UseVisualStyleBackColor = true; - btn_Resume.Visible = false; btn_Resume.Click += btn_Resume_Click; // + // contextMenuStrip1 + // + contextMenuStrip1.Items.AddRange(new ToolStripItem[] { menu_cleardata, openDataFileToolStripMenuItem, RestartBadItems }); + contextMenuStrip1.Name = "contextMenuStrip1"; + contextMenuStrip1.Size = new Size(213, 70); + // + // menu_cleardata + // + menu_cleardata.Name = "menu_cleardata"; + menu_cleardata.Size = new Size(212, 22); + menu_cleardata.Text = "Clear Data"; + menu_cleardata.Click += menu_cleardata_Click; + // + // openDataFileToolStripMenuItem + // + openDataFileToolStripMenuItem.Name = "openDataFileToolStripMenuItem"; + openDataFileToolStripMenuItem.Size = new Size(212, 22); + openDataFileToolStripMenuItem.Text = "Open Data Folder"; + openDataFileToolStripMenuItem.Click += openDataFileToolStripMenuItem_Click; + // + // RestartBadItems + // + RestartBadItems.Name = "RestartBadItems"; + RestartBadItems.Size = new Size(212, 22); + RestartBadItems.Text = "Restart Broken Downloads"; + RestartBadItems.Click += RestartBadItems_Click; + // // Form1 // AcceptButton = button1; @@ -271,6 +302,7 @@ private void InitializeComponent() FormClosing += Form1_FormClosing; Load += Form1_Load; ((System.ComponentModel.ISupportInitialize)dgv_downloads).EndInit(); + contextMenuStrip1.ResumeLayout(false); ResumeLayout(false); PerformLayout(); } @@ -299,5 +331,9 @@ private void InitializeComponent() private DataGridViewTextBoxColumn Converted; private Button btn_Pause; private Button btn_Resume; + private ContextMenuStrip contextMenuStrip1; + private ToolStripMenuItem menu_cleardata; + private ToolStripMenuItem openDataFileToolStripMenuItem; + private ToolStripMenuItem RestartBadItems; } } diff --git a/Form1.cs b/Form1.cs index 73d53dc..7e4e976 100644 --- a/Form1.cs +++ b/Form1.cs @@ -8,6 +8,7 @@ using TagLib; using System.Diagnostics; using Microsoft.VisualBasic; +using Fastenshtein; namespace YTPD { @@ -80,12 +81,10 @@ private async void button1_Click(object sender, EventArgs e) private async void timer1_Tick(object sender, EventArgs e) { // ensure a directory exists - if (!Directory.Exists(txt_Dir.Text)) - { - MessageBox.Show("Directory does not exist! Please browse for a new directory.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - timer1.Enabled = false; - return; - } + if (!Directory.Exists(txt_Dir.Text)) Directory.CreateDirectory(txt_Dir.Text); + + // turn it off while it's running + timer1.Enabled = false; string artist = ""; string album = ""; @@ -113,7 +112,7 @@ private async void timer1_Tick(object sender, EventArgs e) continue; } - if(row.Cells[0].Value.ToString().ToLower().Contains("romeo")) + if (row.Cells[0].Value.ToString().ToLower().Contains("romeo")) { song = ""; } @@ -184,7 +183,7 @@ private async void timer1_Tick(object sender, EventArgs e) row.Cells["DL"].Value = "100"; dgv_downloads.Rows[row.Index].DefaultCellStyle.BackColor = Color.DarkRed; dgv_downloads.Rows[row.Index].DefaultCellStyle.ForeColor = Color.White; - } + } finally { if (!isConverting && fullpath.Length > 1) @@ -201,6 +200,9 @@ private async void timer1_Tick(object sender, EventArgs e) break; } } + + // turn timer back on when done + timer1.Enabled = true; } static async Task ConvertFile(string inputFilePath, string fileExt) @@ -437,7 +439,6 @@ private void button2_Click(object sender, EventArgs e) } } - timer1.Enabled = true; } private void txt_URL_TextChanged(object sender, EventArgs e) @@ -457,14 +458,25 @@ private async void timer_convert_Tick(object sender, EventArgs e) .ToArray(); Console.WriteLine("\nNon-mp3 files files:" + nonMp3Files.Length); + + // converts non-mp3 to mp3 foreach (string nonMp3File in nonMp3Files) { - if (!isConverting) + foreach (DataGridViewRow row in dgv_downloads.Rows) { - isConverting = true; - await ConvertFile(nonMp3File, nonMp3File.Substring(nonMp3File.IndexOf('.'))); - Console.WriteLine(nonMp3File); - isConverting = false; + if (row.Cells[0].Value == null || row.Cells[0].Value.ToString().Length == 0) continue; + // review levenshtein distance for mp3 file and song name so we only convert the mp3 + string foundsong = nonMp3File.Substring(nonMp3File.LastIndexOf('\\') + 2); + foundsong = foundsong.Substring(foundsong.IndexOf('-') + 2, foundsong.LastIndexOf('.') - 4); + string cellsong = row.Cells["Song"].Value.ToString(); + int lev = Levenshtein.Distance(foundsong, cellsong); + + // if threshold is met, then convert non-mp3 to mp3 + if (lev > 70) + { + await ConvertFile(nonMp3File, nonMp3File.Substring(nonMp3File.LastIndexOf('.'))); + Console.WriteLine(nonMp3File); + } } } } @@ -483,6 +495,8 @@ private void btn_Pause_Click(object sender, EventArgs e) { isPaused = true; timer1.Enabled = false; + timer_convert.Enabled = false; + timer_tag.Enabled = false; btn_Pause.Visible = false; btn_Resume.Visible = true; } @@ -491,6 +505,8 @@ private void btn_Resume_Click(object sender, EventArgs e) { isPaused = false; timer1.Enabled = true; + timer_convert.Enabled = true; + timer_tag.Enabled = true; btn_Pause.Visible = true; btn_Resume.Visible = false; } @@ -569,5 +585,52 @@ private void dgv_downloads_CellDoubleClick(object sender, DataGridViewCellEventA } } } + + private void btn_secret_MouseDoubleClick(object sender, MouseEventArgs e) + { + + } + + private void dgv_downloads_MouseDown(object sender, MouseEventArgs e) + { + if (e.Button == MouseButtons.Right) + { + contextMenuStrip1.Show(MousePosition.X, MousePosition.Y); + } + } + + private void menu_cleardata_Click(object sender, EventArgs e) + { + DialogResult dr = MessageBox.Show("Are you sure want to delete your data?", "YouTube Album Downloader", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); + + if (dr == DialogResult.Yes) + { + dgv_downloads.Rows.Clear(); + SaveDataGridViewToCSV(); + } + } + + private void btn_secret_Click(object sender, EventArgs e) + { + + } + + private void openDataFileToolStripMenuItem_Click(object sender, EventArgs e) + { + Process.Start("explorer.exe", Application.StartupPath); + } + + private void RestartBadItems_Click(object sender, EventArgs e) + { + foreach(DataGridViewRow row in dgv_downloads.Rows) + { + if (row.Cells[0].Value == null || row.Cells[0].Value.ToString().Length == 0) continue; + + if (row.Cells["Converted"].Value.ToString() == "No") + { + row.Cells["DL"].Value = "0"; + } + } + } } } diff --git a/Form1.resx b/Form1.resx index 325a02a..838ad2c 100644 --- a/Form1.resx +++ b/Form1.resx @@ -156,4 +156,7 @@ 319, 17 + + 446, 17 + \ No newline at end of file diff --git a/YTPD.csproj b/YTPD.csproj index 9d9d36f..a27471c 100644 --- a/YTPD.csproj +++ b/YTPD.csproj @@ -21,9 +21,10 @@ + - +