using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; using Amazon.S3.Transfer; namespace Sony.Filtr.AppleMusic.Streams { public class AppleMusicStreamingReportApi { private readonly AmazonS3Client _s3Client; private const string _Bucket = "filtr-apple-music-reports"; public AppleMusicStreamingReportApi(AmazonS3Client s3Client) { _s3Client = s3Client; } public async Task> GetFilePaths(DateTime date) { var dateString = date.ToString("yyyyMMdd", CultureInfo.InvariantCulture); var streamObjects = await _s3Client.ListObjectsAsync(new ListObjectsRequest() { BucketName = _Bucket, Prefix = $"{dateString}/AppleMusic_Streams_" }); var fileNames = streamObjects.S3Objects.Select(p => p.Key).ToList(); return fileNames; } private async Task> ListContentFilesAsync(DateTime date) { var dateString = date.ToString("yyyyMMdd", CultureInfo.InvariantCulture); var contentObjects = await _s3Client.ListObjectsAsync(new ListObjectsRequest() { BucketName = _Bucket, Prefix = $"{dateString}/AppleMusic_Content_" }); return contentObjects.S3Objects; } private async Task> DownloadContentFilesAsync(List files, string destinationFolder) { List filePaths = new List(); foreach (var contentObject in files) { var originalFilename = Path.GetFileName(contentObject.Key); var destinationPath = Path.Combine(destinationFolder, originalFilename); filePaths.Add(destinationPath); if (!File.Exists(destinationPath)) { await DownloadToFileAsync(destinationPath, contentObject.Key); } } return filePaths; } public async Task DownloadToFileAsync(string destinationFilepath, string fileKey) { using (var downloadStream = await DownloadS3FileAsync(_Bucket, fileKey)) { using (var fileStream = File.Open(destinationFilepath, FileMode.OpenOrCreate)) { await downloadStream.CopyToAsync(fileStream); } } } private async Task DownloadS3FileAsync(string bucket, string key) { var transferUtility = new TransferUtility(_s3Client); var contentStream = await transferUtility.OpenStreamAsync(new TransferUtilityOpenStreamRequest() { BucketName = bucket, Key = key, }); return contentStream; } } }