public static bool SynchronizeDBs(string srcDBFilename1, string srcDBFilename2, string outputFilename) { DB destination; DB source; bool loaded = MusicDBLoader.LoadDB(srcDBFilename1, out destination); if (loaded) { loaded = MusicDBLoader.LoadDB(srcDBFilename2, out source); if (loaded) { // TODO: recode this //destination.albums.AddRange(source.albums); MusicDBLoader.SaveDB(outputFilename, destination); return(true); } } return(false); }
void ThreadParse(object data) { ThreadData td = (ThreadData)data; Thread thr = Thread.CurrentThread; try { ParseMusicStyle(td.style, thr); } catch (ThreadInterruptedException) { iForm.PrintLine("(" + td.style + ")\t!INTERRUPTED the queries ( at " + iLineCount[td.style] + " lines )"); //update log SaveLog((int)this.iLineCount[td.style], td.style); MusicDBLoader.SaveDB(iDBFileName, this.dataBase); } catch (ThreadAbortException) { iForm.PrintLine("(" + td.style + ")\t!ABORTED the queries ( at " + iLineCount[td.style] + " lines )"); //update log SaveLog((int)this.iLineCount[td.style], td.style); MusicDBLoader.SaveDB(iDBFileName, this.dataBase); } catch (Exception e) { iForm.PrintLine(e.Message); } }
//Constructor public MusicDBParser(Form1 aForm) { iForm = aForm; iLineCount = new Hashtable(); // try to load DataBase bool loaded = MusicDBLoader.LoadDB(iDBFileName, out dataBase); if (!loaded) { dataBase = new DB(); dataBase.albums = new Hashtable(); dataBase.countries = new Hashtable(); dataBase.styles = new Hashtable(); dataBase.artists = new Hashtable(); dataBase.dates = new Hashtable(); this.ParseCountries(iCountriesInfoFileName); MusicDBLoader.SaveDB(iDBFileName, this.dataBase); } }
public static bool FilterDB(string srcDBFilename, string outputFilename) { DB destination; string iGeoDataPath = "./geodata/"; List <string> countryFilter = ParseCountryFilter(iGeoDataPath + "countries_acronyms_europe.txt"); bool loaded = MusicDBLoader.LoadDB(srcDBFilename, out destination); if (loaded) { ArrayList albums = new ArrayList(destination.albums.Values); for (int i = 0; i < albums.Count; i++) { Album album = (Album)albums[i]; for (int j = 0; j < album.releases.Count; j++) { MusicBrainzRelease release = album.releases[j]; if (!countryFilter.Contains(release.country.acronym)) { album.releases.RemoveAt(j--); release.freeDBAlbum = null; } } // if it became an empty album if (album.releases.Count == 0) { string key = album.artist.name + "€" + album.title; key = key.ToLowerInvariant(); key = ClearString(key); destination.albums.Remove(key); key = ClearString(album.artist.name.ToLowerInvariant()); Artist artist = (Artist)destination.artists[key]; artist.albums.Remove(album); if (album.artist.albums.Count == 0) { destination.artists.Remove(key); } } } // filter releases in styles foreach (Style style in destination.styles.Values) { for (int k = 0; k < style.releases.Count; k++) { MusicBrainzRelease release = style.releases[k]; if (release.freeDBAlbum == null || !countryFilter.Contains(release.country.acronym)) { style.releases.RemoveAt(k--); } } } // filter countries ArrayList countries = new ArrayList(destination.countries.Values); foreach (Country country in countries) { if (!countryFilter.Contains(country.acronym)) { destination.countries.Remove(country.acronym); } else { for (int k = 0; k < country.releases.Count; k++) { MusicBrainzRelease release = country.releases[k]; if (release.freeDBAlbum == null) { country.releases.RemoveAt(k--); } } } } // filter styles ArrayList styles = new ArrayList(destination.styles.Keys); for (int k = 0; k < styles.Count; k++) { object key = styles[k]; Style style = (Style)destination.styles[key]; if (style.releases.Count == 0) { destination.styles.Remove(key); } } MusicDBLoader.SaveDB(outputFilename, destination); return(true); } return(false); }
////////////////////////////////////////////////////////////////////////// // Method: ParseMusicStyle // FullName: MusicDataminer.MusicDBParser.ParseMusicStyle // Access: public // Returns: void // Parameter: string style // Parameter: Thread aCurrentThread ////////////////////////////////////////////////////////////////////////// public void ParseMusicStyle(string style, Thread aCurrentThread) { // Open the file and read it back. StreamReader sr = File.OpenText("../../../data/freedb/" + style + ".txt"); string text = ""; //Skip previously read int lineNumber = ReadLog(style); for (int i = 0; i < lineNumber; i++) { sr.ReadLine(); } iLineCount[style] = lineNumber; char[] delimiterChars = { '\t' }; string[] tokens; string retrievedName; Style styleObj = GetStyle(style); try { //Start querying while ((text = sr.ReadLine()) != null) { Thread.Sleep(2); tokens = text.Split(delimiterChars); if (tokens.Length < 2) { continue; } // Lowercase all the tokens string artistName = tokens[0].ToLower(); string title = tokens[1].ToLower(); List <MusicBrainzRelease> releases = new List <MusicBrainzRelease>(); // search for info in the MusicBrainz DB bool foundSomething = GetMusicBrainzReleases(artistName, title, releases, out retrievedName, style); if (foundSomething) { Album album = new Album(); if (tokens.Length < 3 || tokens[2].Trim().Length == 0) { album.style = styleObj; } else { album.style = GetStyle(tokens[2]); } album.title = retrievedName; if (AddAlbum(album, artistName)) { // Add the album to all its releases foreach (MusicBrainzRelease release in releases) { release.freeDBAlbum = album; AddReleaseDate(release); } // set the Artist object album.artist = GetArtist(artistName); // add the releases to the Album album.releases = releases; // add the releases to their Style album.style.releases.AddRange(releases); // add the album to its artist album.artist.albums.Add(album); // Some output iForm.PrintLine("(" + style + ")\t-> ADDED: " + album.title + " Artist: " + album.artist.name); foreach (MusicBrainzRelease release in album.releases) { iForm.PrintLine("\tCountry: " + release.country.name + " Date: " + release.date); } } } lineNumber++; iLineCount[style] = lineNumber; } } catch (Exception e) { Console.WriteLine("Boom."); } iForm.PrintLine("(" + style + ")\t!!! FINISHED !!! ( " + lineNumber + " lines )"); //update log SaveLog(lineNumber, style); MusicDBLoader.SaveDB(iDBFileName, this.dataBase); }