Ejemplo n.º 1
0
        public async Task <byte[]> DownloadTrack(string url, IProgressBar trackProgress, string title, int retries = 3)
        {
            var attempts = 1;

            while (attempts <= retries)
            {
                try
                {
                    using (HttpResponseMessage downloadResponse = await _httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
                    {
                        if (downloadResponse.IsSuccessStatusCode && downloadResponse.Content.Headers.ContentLength.HasValue)
                        {
                            return(await DownloadWithProgress(downloadResponse, trackProgress, title));
                        }
                    }
                }
                catch (HttpRequestException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                attempts++;
                trackProgress.Next("Request failed, waiting 5s...");
                await Task.Delay(5000);
            }

            return(null);
        }
Ejemplo n.º 2
0
        private async Task <byte[]> GetDecryptedBytes(TrackInfo trackInfo, AudioQuality audioQuality, IProgressBar trackProgress, string progressTitle)
        {
            trackProgress.Next($"{progressTitle} | Grabbing download URL");

            string downloadUrl = EncryptionHandler.GetDownloadUrl(trackInfo, (int)audioQuality);

            byte[] encryptedBytes = await _deezerHttp.DownloadTrack(downloadUrl, trackProgress, progressTitle);

            if (encryptedBytes == null || encryptedBytes.Length == 0)
            {
                Helpers.RedMessage("Failed to download encrypted track");
                return(new byte[0]);
            }

            trackProgress.Next($"{progressTitle} | Decrypting track");
            return(await EncryptionHandler.DecryptTrack(encryptedBytes, trackInfo.TrackTags.Id));
        }
Ejemplo n.º 3
0
        private async Task <byte[]> DownloadWithProgress(HttpResponseMessage response, IProgressBar trackProgress, string title)
        {
            // thanks stackoverflow
            using (Stream fileStream = await response.Content.ReadAsStreamAsync())
            {
                // ReSharper disable once PossibleInvalidOperationException
                long   total          = response.Content.Headers.ContentLength.Value;
                double totalMegabytes = ByteSize.FromBytes(total).MegaBytes;
                totalMegabytes = Math.Round(totalMegabytes, 2);

                var finalBytes   = new byte[total];
                var totalRead    = 0L;
                var buffer       = new byte[4096];
                var isMoreToRead = true;

                do
                {
                    int read = await fileStream.ReadAsync(buffer, 0, buffer.Length);

                    if (read == 0)
                    {
                        trackProgress.Next($"{title} | Download Complete");
                        isMoreToRead = false;
                    }
                    else
                    {
                        var data = new byte[read];
                        buffer.ToList().CopyTo(0, data, 0, read);
                        data.CopyTo(finalBytes, totalRead);

                        totalRead += read;

                        double percent = totalRead * 1d / (total * 1d) * 100;

                        double totalReadMegabytes = ByteSize.FromBytes(totalRead).MegaBytes;
                        totalReadMegabytes = Math.Round(totalReadMegabytes, 2);

                        trackProgress.Refresh(Convert.ToInt32(percent), $"{title} | {totalReadMegabytes}MB/{totalMegabytes}MB");
                    }
                } while (isMoreToRead);

                return(finalBytes);
            }
        }
Ejemplo n.º 4
0
 public void Next(string item)
 {
     _bar.Next(item);
 }