public Top100DB() { string connectionString = "Server=localhost;Database=top40;User ID=kevin;Password=admin;Pooling=false"; dbConnection = new MySqlConnection(connectionString); try { dbConnection.Open(); dbSongsList = new Songs(dbConnection); } catch (MySqlException) { Top100Util.Error("Could not connect to database."); throw; } }
public void FindMissingTagsAndComments(List <Song> iTunesSongList, Func <Song, bool> compare) { string top100 = "Top 100"; Regex comment = new Regex("(?<year>^[0-9][0-9][0-9][0-9]), #(?<number>[01][0-9][0-9]).*"); Regex badComment = new Regex("^[0-9][0-9][0-9][0-9], #[01][0-9][0-9] [0-9][0-9][0-9][0-9], #[0-9]?[1-9].*"); var timer = Top100Timer.Start("FindMissingTagsAndComments"); foreach (Song dbSong in dbSongsList.List.FindAll(x => x.Own.Equals(true) && compare(x))) { var list = iTunesSongList.FindAll(x => x.IsMatch(dbSong)); if ((list != null) && (list.Count >= 1)) { foreach (Song s in list) { bool updateSong = false; string appleScript = "tell application \"iTunes\"\n" + " activate\n" + String.Format(" set results to (every file track of playlist \"Library\" whose name contains (\"{0}\") and artist contains (\"{1}\"))\n", scrubString(s.Title), scrubString(s.Artist)) + " repeat with t in results\n"; if (!s.Grouping.Contains(top100)) { appleScript += String.Format(" set t's grouping to \"{0}\" as text\n", addTag(s.Grouping, top100)); Top100Util.Debug(String.Format("Missing Grouping: {0}=>{1}", s, addTag(s.Grouping, top100))); updateSong = true; } if (badComment.IsMatch(s.Comments)) { appleScript += String.Format(" set t's comment to \"{0}\" as text\n", prependComment(s.Comments, dbSong.Year, dbSong.Number)); Top100Util.Debug(String.Format("Bad Comment: {0}=>{1}", s, prependComment(s.Comments, dbSong.Year, dbSong.Number))); updateSong = true; } else if (!comment.IsMatch(s.Comments)) { appleScript += String.Format(" set t's comment to \"{0}\" as text\n", prependComment(s.Comments, dbSong.Year, dbSong.Number)); Top100Util.Debug(String.Format("Missing Comment: {0}=>{1}", s, prependComment(s.Comments, dbSong.Year, dbSong.Number))); updateSong = true; } else if (comment.IsMatch(s.Comments)) { if (dbSong.Number < s.Number) { appleScript += String.Format(" set t's comment to \"{0}\" as text\n", prependComment(s.Comments, dbSong.Year, dbSong.Number)); Top100Util.Debug(String.Format("Updating Comment: {0}=>{1}", s, prependComment(s.Comments, dbSong.Year, dbSong.Number))); updateSong = true; } } if (updateSong) { appleScript += " end repeat\n" + "end tell\n"; try { if (!Top100Settings.Preview) { AppleScript.Run(appleScript); } } catch (Exception e) { Top100Util.Error(String.Format("Cannot update song: {0}\n\tException: {1}\n\tUsing: {2}", s, e, appleScript)); } } } } else { Top100Util.Error("Cannot find owned song in Library. " + dbSong); } } timer.End(); }