Example #1
0
        private async Task DownloadTracks(DirectoryStructure structure, string friendlyName, Track[] tracks)
        {
            if (!Directory.Exists(SavePath))
            {
                Directory.CreateDirectory(SavePath);
            }
            Console.WriteLine("Downloading Playlist: " + friendlyName);
            try
            {
                foreach (var song in tracks)
                {
                    try
                    {
                        var newFilePath = structure.GetFilePath(friendlyName, song);
                        Console.WriteLine("     Downloading Song: " + song.Title + " - " + song.Artist);

                        //Create Directory Structure
                        if (!Directory.Exists(structure.GetFileFolder(friendlyName, song)))
                        {
                            Directory.CreateDirectory(structure.GetFileFolder(friendlyName, song));
                        }

                        if (File.Exists(newFilePath)) //File Exists, skip to next song.
                        {
                            continue;
                        }

                        //Download Track
                        var streamUrl = await ApplicationState.MobileClient.GetStreamUrlAsync(song);

                        using (var client = new WebClient())
                        {
                            await client.DownloadFileTaskAsync(streamUrl, newFilePath);
                        }

                        // Edit File MetaData
                        var fileInfo = new FileInfo(newFilePath);
                        using (var fileStream = File.Open(newFilePath, FileMode.Open, FileAccess.ReadWrite))
                        {
                            var file =
                                TagLib.File.Create(new StreamFileAbstraction(fileInfo.Name, fileStream, fileStream));
                            var tags = file.Tag;
                            tags.Title        = song.Title;
                            tags.Album        = song.Album;
                            tags.AlbumSort    = song.DiscNumber.ToString();
                            tags.AlbumArtists = new[] { song.Artist };
                            tags.Track        = Convert.ToUInt32(song.TrackNumber);
                            tags.Year         = Convert.ToUInt32(song.Year);

                            file.Save();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("     Failed Downloading Song: " + song.Title + " - " + song.Artist);
                        Console.WriteLine(ex.Message);
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    finally
                    {
                        ProgressBar.Value++;
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("     Downloaded Song: " + song.Title + " - " + song.Artist);
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                }
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Downloaded Playlist: " + friendlyName);
                Console.ForegroundColor = ConsoleColor.White;
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Failed Downloading Playlist: " + friendlyName);
                Console.WriteLine(ex.Message);
                Console.ForegroundColor = ConsoleColor.White;
            }
        }