コード例 #1
0
ファイル: Logic.cs プロジェクト: ablackness/Polly-Challenge
		public async Task<ConvertTextResponse> AddItem(ConvertTextRequest request) {
			HttpClient httpClient = new HttpClient();
			// string uri = "https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Ftechcrunch.com%2Ffeed%2F";
			var response = await httpClient.GetAsync(request.Uri);
			string responseContent = await response.Content.ReadAsStringAsync();
			dynamic JsonResponse = JObject.Parse(responseContent);
			dynamic items = JsonResponse.items;

			var item = items[0];
			string guid = item.guid;
			int index = guid.IndexOf("?p=");
			string itemId = guid.Substring(index + 3, 7);

			var voicesResponse = _provider.Polly.DescribeVoicesAsync(new DescribeVoicesRequest 
			{
				LanguageCode = request.Language
			});

			Random random = new Random();
			int r = random.Next(voicesResponse.Result.Voices.Count);
			string voice = voicesResponse.Result.Voices[r].Id;

			var pollyRequest = new SynthesizeSpeechRequest {
				OutputFormat = OutputFormat.Mp3,
				Text = item.description,
				TextType = "text",
				VoiceId = voice
			};

			var pollyResponse = await _provider.Polly.SynthesizeSpeechAsync(pollyRequest);
			if (pollyResponse == null
				|| pollyResponse.HttpStatusCode != (HttpStatusCode)200) {
				throw new Exception("Text could not be converted to audio.");
			}
			var memoryStream = new MemoryStream();
			pollyResponse.AudioStream.CopyTo(memoryStream);
			var s3Response = await _provider.S3.PutObjectAsync(new PutObjectRequest {
				BucketName = _provider.Bucket,
				Key = itemId + ".mp3",
				InputStream = memoryStream
			});
			if (s3Response == null
				|| s3Response.HttpStatusCode != (HttpStatusCode)200) {
				throw new Exception("Unable to save audio file to s3");
			}
			
			await _provider.SnsClient.PublishAsync(new PublishRequest {
                TopicArn = _provider.NotificationTopic,
                Message = "The audio file is ready: https://s3.amazonaws.com/" + _provider.Bucket + "/" + itemId + ".mp3",
				Subject = item.title
            });

			return new ConvertTextResponse {
				FileName = itemId + ".mp3",
				Voice = voice
			};
		}
コード例 #2
0
ファイル: Logic.cs プロジェクト: ClothesInTheDryer/Jamie
        public async Task <ConvertTextResponse> AddItem(ConvertTextRequest request)
        {
            var pollyRequest = new SynthesizeSpeechRequest {
                OutputFormat = OutputFormat.Mp3,
                Text         = request.Content,
                TextType     = "text",
                VoiceId      = VoiceId.Amy
            };

            var pollyResponse = await _provider.Polly.SynthesizeSpeechAsync(pollyRequest);

            if (pollyResponse == null ||
                pollyResponse.HttpStatusCode != (HttpStatusCode)200)
            {
                throw new Exception("Text could not be converted to audio.");
            }
            var memoryStream = new MemoryStream();

            pollyResponse.AudioStream.CopyTo(memoryStream);
            var s3Response = await _provider.S3.PutObjectAsync(new PutObjectRequest {
                BucketName  = _provider.Bucket,
                Key         = request.FileName,
                InputStream = memoryStream
            });

            if (s3Response == null ||
                s3Response.HttpStatusCode != (HttpStatusCode)200)
            {
                throw new Exception("Unable to save audio file to s3");
            }

            // TODO LVL2 SNS Topic would be nice here
            return(new ConvertTextResponse {
                FileName = request.FileName
            });
        }