Can now dynamically update song metadata, discovered and fixed a bug that would update all Song records when updating a single song's metadata #36
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using TagLib;
|
using TagLib;
|
||||||
|
|
||||||
@@ -9,6 +10,8 @@ namespace Icarus.Controllers.Utilities
|
|||||||
public class MetadataRetriever
|
public class MetadataRetriever
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
|
private Song _updatedSong;
|
||||||
|
private string _message;
|
||||||
private string _title;
|
private string _title;
|
||||||
private string _artist;
|
private string _artist;
|
||||||
private string _album;
|
private string _album;
|
||||||
@@ -19,6 +22,16 @@ namespace Icarus.Controllers.Utilities
|
|||||||
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
public Song UpdatedSongRecord
|
||||||
|
{
|
||||||
|
get => _updatedSong;
|
||||||
|
set => _updatedSong = value;
|
||||||
|
}
|
||||||
|
public string Message
|
||||||
|
{
|
||||||
|
get => _message;
|
||||||
|
set => _message = value;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -74,9 +87,92 @@ namespace Icarus.Controllers.Utilities
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var msg = ex.Message;
|
var msg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred: \n{msg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdateMetadata(Song updatedSong, Song oldSong)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
InitializeUpdatedSong(oldSong);
|
||||||
|
var songValues = CheckSongValues(updatedSong);
|
||||||
|
PerformUpdate(updatedSong, songValues);
|
||||||
|
Message = "Successfully updated metadata";
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred: {msg}");
|
||||||
|
Message = "Failed to update metadata";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void PerformUpdate(Song updatedSong, SortedDictionary<string, bool> checkedValues)
|
||||||
|
{
|
||||||
|
var filePath = updatedSong.SongPath;
|
||||||
|
var title = updatedSong.Title;
|
||||||
|
var artist = updatedSong.Artist;
|
||||||
|
var album = updatedSong.Album;
|
||||||
|
var genre = updatedSong.Genre;
|
||||||
|
var year = updatedSong.Year;
|
||||||
|
TagLib.File fileTag = TagLib.File.Create(filePath);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Updating metadata of {title}");
|
||||||
|
foreach (var key in checkedValues.Keys)
|
||||||
|
{
|
||||||
|
bool result = checkedValues[key];
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
switch (key.ToLower())
|
||||||
|
{
|
||||||
|
case "title":
|
||||||
|
_updatedSong.Title = title;
|
||||||
|
fileTag.Tag.Title = title;
|
||||||
|
break;
|
||||||
|
case "artists":
|
||||||
|
_updatedSong.Artist = artist;
|
||||||
|
fileTag.Tag.Artists = new []{artist};
|
||||||
|
break;
|
||||||
|
case "album":
|
||||||
|
_updatedSong.Album = album;
|
||||||
|
fileTag.Tag.Album = album;
|
||||||
|
break;
|
||||||
|
case "genre":
|
||||||
|
_updatedSong.Genre = genre;
|
||||||
|
fileTag.Tag.Genres = new []{genre};
|
||||||
|
break;
|
||||||
|
case "year":
|
||||||
|
_updatedSong.Year = year;
|
||||||
|
fileTag.Tag.Year = (uint)year;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fileTag.Save();
|
||||||
|
Console.WriteLine("Successfully updated metadata");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred:\n{msg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void InitializeUpdatedSong(Song song)
|
||||||
|
{
|
||||||
|
_updatedSong = new Song
|
||||||
|
{
|
||||||
|
Id = song.Id,
|
||||||
|
Title = song.Title,
|
||||||
|
Album = song.Album,
|
||||||
|
Artist = song.Artist,
|
||||||
|
Genre = song.Genre,
|
||||||
|
Year = song.Year,
|
||||||
|
Duration = song.Duration,
|
||||||
|
Filename = song.Filename,
|
||||||
|
SongPath = song.SongPath
|
||||||
|
};
|
||||||
|
}
|
||||||
private void PrintMetadata()
|
private void PrintMetadata()
|
||||||
{
|
{
|
||||||
Console.WriteLine("\n\nMetadata of the song:");
|
Console.WriteLine("\n\nMetadata of the song:");
|
||||||
@@ -87,6 +183,45 @@ namespace Icarus.Controllers.Utilities
|
|||||||
Console.WriteLine($"Year: {_year}");
|
Console.WriteLine($"Year: {_year}");
|
||||||
Console.WriteLine($"Duration: {_duration}\n\n");
|
Console.WriteLine($"Duration: {_duration}\n\n");
|
||||||
}
|
}
|
||||||
|
private void PrintMetadata(Song song, string message)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"\n\n{message}");
|
||||||
|
Console.WriteLine($"Title: {song.Title}");
|
||||||
|
Console.WriteLine($"Artist: {song.Artist}");
|
||||||
|
Console.WriteLine($"Album: {song.Album}");
|
||||||
|
Console.WriteLine($"Genre: {song.Genre}");
|
||||||
|
Console.WriteLine($"Year: {song.Year}");
|
||||||
|
Console.WriteLine($"Duration: {song.Duration}\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
private SortedDictionary<string, bool> CheckSongValues(Song song)
|
||||||
|
{
|
||||||
|
var songValues = new SortedDictionary<string, bool>();
|
||||||
|
Console.WriteLine("Checking for null data");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
songValues["Title"] = String.IsNullOrEmpty(song.Title);
|
||||||
|
songValues["Artists"] = String.IsNullOrEmpty(song.Artist);
|
||||||
|
songValues["Album"] = String.IsNullOrEmpty(song.Album);
|
||||||
|
songValues["Genre"] = String.IsNullOrEmpty(song.Genre);
|
||||||
|
if (song.Year==0)
|
||||||
|
{
|
||||||
|
songValues["Year"] = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
songValues["Year"] = false;
|
||||||
|
}
|
||||||
|
Console.WriteLine("Checking for null data completed");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var msg = ex.Message;
|
||||||
|
Console.WriteLine($"An error occurred: \n{msg}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return songValues;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,8 @@ namespace Icarus.Models.Context
|
|||||||
|
|
||||||
string query = "UPDATE Songs SET Title=@Title, Album=@Album, " +
|
string query = "UPDATE Songs SET Title=@Title, Album=@Album, " +
|
||||||
"Artist=@Artist, Year=@Year, Genre=@Genre, " +
|
"Artist=@Artist, Year=@Year, Genre=@Genre, " +
|
||||||
"Duration=@Duration, Filename=@Filename, SongPath=@SongPath";
|
"Duration=@Duration, Filename=@Filename, " +
|
||||||
|
"SongPath=@SongPath WHERE Id=@Id";
|
||||||
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue("@Title", song.Title);
|
cmd.Parameters.AddWithValue("@Title", song.Title);
|
||||||
@@ -68,6 +69,7 @@ namespace Icarus.Models.Context
|
|||||||
cmd.Parameters.AddWithValue("@Duration", song.Duration);
|
cmd.Parameters.AddWithValue("@Duration", song.Duration);
|
||||||
cmd.Parameters.AddWithValue("@Filename", song.Filename);
|
cmd.Parameters.AddWithValue("@Filename", song.Filename);
|
||||||
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
cmd.Parameters.AddWithValue("@SongPath", song.SongPath);
|
||||||
|
cmd.Parameters.AddWithValue(@"Id", song.Id);
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user