示例#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);
        }
示例#4
0
        private void AnalyseManifestTracks(IList <Track> allTracks, List <Track> tracksToDownload)
        {
            Track track        = null;
            var   manifestPath = ManifestUtil.DetermineManifestPath();

            try
            {
                if (!File.Exists(manifestPath))
                {
                    return;
                }
                var manifest = ManifestUtil.LoadManifestFromFile();
                for (int index = 0; index < manifest.Count; index++)
                {
                    track = manifest[index];

                    track.LocalPath = Path.Combine(ManifestUtil.FileSystemUtil.Directory.FullName, track.LocalPathRelative);
                    var compareTrack = allTracks.FirstOrDefault(i => i.id == track.id);
                    if (compareTrack == null)
                    {
                        if (ManifestUtil.SyncMethod == 1)
                        {
                            continue;
                        }
                        manifest.Remove(track);
                        index--;
                        DeleteFile(track.LocalPath);
                        continue;
                    }
                    if (!File.Exists(track.LocalPath))
                    {
                        manifest.Remove(track);
                        index--;
                        tracksToDownload.Add(compareTrack);
                        continue;
                    }

                    //If the duration is shorter than before; suspect a change from full song to sample song
                    if (track.duration > compareTrack.duration)
                    {
                        continue;
                    }

                    if (compareTrack.IsHD && !track.IsHD) //track changed to HD
                    {
                        manifest.Remove(track);
                        index--;
                        DeleteFile(track.LocalPath);
                        tracksToDownload.Add(compareTrack);
                        continue;
                    }
                    IEqualityComparer <SoundcloudBaseTrack> comparer = new CompareUtils();
                    if (!comparer.Equals(track, compareTrack))
                    {
                        var oldPath = track.LocalPath;
                        ManifestUtil.ReplaceJsonManifestObject(ref manifest, compareTrack, track, index);
                        Directory.CreateDirectory(Path.GetDirectoryName(track.LocalPath));
                        File.Move(oldPath, track.LocalPath);
                        DeleteEmptyDirectory(oldPath);
                        MetadataTaggingUtils.TagIt(track);
                        continue;
                    }
                }
                ManifestUtil.WriteManifestToFile(manifest);
            }
            catch (Exception e)
            {
                ManifestUtil.ProgressUtil.IsError = true;
                throw new Exception(string.Format(LanguageManager.Language["STR_EXCEPTION_SYNC"], track?.EffectiveDownloadUrl, track?.LocalPath, e));
            }
        }