public void MyTestInitialize()
 {
     this.lastfmConnector = new LastFMConnector();
 }
Example #2
0
        /// <summary>
        /// Backgroundworker, which actually does almost all the job.
        /// </summary>
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            SenderData data = (SenderData)e.Argument;

            BackgroundWorker worker = sender as BackgroundWorker;
            LastFMConnector lastFM = new LastFMConnector();

            // Validating artist.
            worker.ReportProgress(0);

            if (!lastFM.isValidArtist(data.artist))
            {
                e.Cancel = true;
            }
            else
            {
                // Get the charts of the artists.
                worker.ReportProgress(25);

                List<string> tracks = lastFM.getArtistTopTracks(data.artist);
                List<fileData> files = new List<fileData>();
                List<string> playlist = new List<string>();

                // Scan the directory recursively looking for all the valid music files and store their paths and metadata in the files list.
                worker.ReportProgress(50);

                foreach (string file in fastFileSearch(data.musicPath))
                {
                    if (file.EndsWith("mp3") || file.EndsWith("m4a") || file.EndsWith("wav") || file.EndsWith("flac"))
                    {
                        TagLib.File f = TagLib.File.Create(file);

                        files.Add(new fileData()
                        {
                            title = LastFMConnector.sanitizeSongName(f.Tag.Title),
                            path = file
                        });
                    }
                }

                // Build the playlist with the songs that the user have and the top-tracks of lastfm.
                worker.ReportProgress(75);

                foreach (string topTrack in tracks)
                {
                    var file = files.FirstOrDefault(o => o.title == topTrack);

                    if ((file != null) && (!playlist.Contains(file.path)))
                        playlist.Add(file.path);
                }

                // Dump the data into a valid playlist file.
                worker.ReportProgress(100);

                using (System.IO.StreamWriter file = new System.IO.StreamWriter(data.musicPath + @"\" + data.artist + " - Best Songs.m3u"))
                {
                    foreach (string song in playlist)
                        file.WriteLine(song);
                }

                // Gives a lil extra time to bulk the data into the file.
                System.Threading.Thread.Sleep(200);

                // Dispose the lists.
                tracks = null;
                files = null;
                playlist = null;
            }
        }