예제 #1
0
        public bool DownloadTrackAndTag(ref Track song)
        {
            if (!ManifestUtil.ProgressUtil.IsActive)
            {
                return(false);
            }
            if (song?.LocalPath == null)
            {
                return(false);
            }
            Directory.CreateDirectory(Path.GetDirectoryName(song.LocalPath));

            using (var client = new WebClient())
            {
                if (song.IsHD)
                {
                    string extension = DetermineExtension(song);

                    //1MB 씩 다운로드 하고 Taglib 로 분석하기.
                    if (ConvertToMp3 && Highqualitysong &&
                        DetermineAllowedFormats().Contains(extension))
                    {
                        //get the wav song as byte data, as we won't store it just yet
                        var soundbytes = client.DownloadData(song.EffectiveDownloadUrl +
                                                             $"?client_id={ClientIDsUtil.ClientIdCurrentValue}");
                        //convert to mp3 & then write bytes to file
                        var succesfulConvert = AudioConverterUtils.ConvertAllTheThings(soundbytes, ref song, extension);
                        if (!succesfulConvert)
                        //something has gone wrong, download the stream url instead of download url
                        {
                            song.LocalPath += ".mp3";
                            client.DownloadFile(song.stream_url + $"?client_id={ClientIDsUtil.ClientIdCurrentValue}", song.LocalPath);
                        }
                    }
                    else if (extension == ".mp3") //get the high res mp3 without converting
                    {
                        song.LocalPath += extension;
                        client.DownloadFile(song.EffectiveDownloadUrl + $"?client_id={ClientIDsUtil.ClientIdCurrentValue}", song.LocalPath);
                    }
                    else //get the low res mp3 if all above not possible
                    {
                        song.LocalPath += ".mp3";
                        client.DownloadFile(song.stream_url + $"?client_id={ClientIDsUtil.ClientIdCurrentValue}", song.LocalPath);
                    }
                }
                else
                {
                    song.LocalPath += ".mp3";
                    client.DownloadFile(song.stream_url + $"?client_id={ClientIDsUtil.ClientIdCurrentValue}", song.LocalPath);
                }
            }
            MetadataTaggingUtils.TagIt(song);
            Interlocked.Increment(ref ManifestUtil.ProgressUtil.SongsDownloaded);
            return(true);
        }
        public void DownloadTrackAndTag(ref Track song)
        {
            if (song?.LocalPath == null)
            {
                throw new Exception("Local path empty");
            }

            Directory.CreateDirectory(Path.GetDirectoryName(song.LocalPath));

            if (!ChooseHighqualitysong || (ChooseHighqualitysong && !song.downloadable && !song.IsHD))
            {
                song.EffectiveDownloadUrl = GetEffectiveDownloadUrlForStream(song.id);
                PersistStreamToDisk(ref song, httpClient.GetStreamAsync(song.EffectiveDownloadUrl).Result, false);
                MetadataTaggingUtils.TagIt(song, this.ManifestUtil.FileSystemUtil);
                Interlocked.Increment(ref ManifestUtil.ProgressUtil.SongsDownloaded);
                return;
            }

            song.EffectiveDownloadUrl = GetEffectiveDownloadUrlForHQ(song.download_url, out string extensionForHQ);

            var highQualitySoundMemoryStream = FilesystemUtils.recyclableMemoryStreamManager.GetStream();

            using (var highQualitySoundStream = httpClient.GetStreamAsync(song.EffectiveDownloadUrl).Result)
            {
                highQualitySoundStream.CopyToAsync(highQualitySoundMemoryStream).GetAwaiter().GetResult();
                highQualitySoundMemoryStream.Position = 0;
            }

            if (!ConvertToMp3 || (ConvertToMp3 && !DetermineAllowedFormatsForConversion().Contains(extensionForHQ)))
            {
                //not able to convert or not chosen
                PersistStreamToDisk(ref song, highQualitySoundMemoryStream, true, extensionForHQ);
                MetadataTaggingUtils.TagIt(song, this.ManifestUtil.FileSystemUtil);
                Interlocked.Increment(ref ManifestUtil.ProgressUtil.SongsDownloaded);
                return;
            }

            try
            {
                AudioConverterUtils.ConvertHighQualityAudioFormats(highQualitySoundMemoryStream, ref song, extensionForHQ);
                highQualitySoundMemoryStream.DisposeAsync();
            }
            catch (Exception e)
            {
                ManifestUtil.FileSystemUtil.LogTrackException(song, e, false);
                PersistStreamToDisk(ref song, highQualitySoundMemoryStream, true, extensionForHQ);
            }

            //high quality track converted or not converted because of exception
            MetadataTaggingUtils.TagIt(song, this.ManifestUtil.FileSystemUtil);
            Interlocked.Increment(ref ManifestUtil.ProgressUtil.SongsDownloaded);
            return;
        }
예제 #3
0
        public bool DownloadTrackAndTag(ref Track song)
        {
            if (!ManifestUtil.ProgressUtil.IsActive)
            {
                return(false);
            }
            if (song?.LocalPath == null)
            {
                return(false);
            }
            Directory.CreateDirectory(Path.GetDirectoryName(song.LocalPath));

            song.EffectiveDownloadUrl = GetEffectiveDownloadUrlForHQ(song.download_url, song.downloadable);
            string extension        = DetermineExtension(song);
            var    allowedExtension = DetermineAllowedFormats().Contains(extension);

            bool succesfulConvert = false;

            if (!string.IsNullOrWhiteSpace(song.EffectiveDownloadUrl) && song.IsHD && ConvertToMp3 && allowedExtension)
            {
                //get the wav song as byte data, as we won't store it just yet
                var soundbytes = httpClient.GetByteArrayAsync(song.EffectiveDownloadUrl).Result;
                //convert to mp3 & then write bytes to file
                succesfulConvert = AudioConverterUtils.ConvertAllTheThings(soundbytes, ref song, extension);
            }

            if (!succesfulConvert)
            {
                song.EffectiveDownloadUrl = GetEffectiveDownloadUrlForStream(song.id);
                if (string.IsNullOrWhiteSpace(song.EffectiveDownloadUrl))
                {
                    return(false);
                }

                song.LocalPath += ".mp3";
                using (var download = httpClient.GetAsync(song.EffectiveDownloadUrl).Result)
                    using (var fs = new FileStream(song.LocalPath, FileMode.Create))
                    {
                        download.Content.CopyToAsync(fs).GetAwaiter().GetResult();
                    }
            }

            MetadataTaggingUtils.TagIt(song);
            Interlocked.Increment(ref ManifestUtil.ProgressUtil.SongsDownloaded);
            return(true);
        }