public IEnumerable <SongModel.Song> ParseSongsFromDatabaseResponse(QueryResponse queryResponse)
        {
            var parsedSongList = new List <SongModel.Song>();

            foreach (Dictionary <string, AttributeValue> item in queryResponse.Items)
            {
                var song = new SongModel.Song();
                foreach (KeyValuePair <string, AttributeValue> attribute in item)
                {
                    var attributeValue = attribute.Value.S;
                    switch (attribute.Key)
                    {
                    case "title":
                        song.Title = attributeValue;
                        break;

                    case "artist":
                        song.Artist = attributeValue;
                        break;

                    case "song_number":
                        song.SongNumber = attributeValue;
                        break;
                    }
                }
                parsedSongList.Add(song);
            }
            LambdaLogger.Log($"*** INFO: {JsonConvert.SerializeObject(parsedSongList)}");
            return(parsedSongList);
        }
Beispiel #2
0
        public int GetIndexOfThisSong(IList <SongModel.Song> existingSongList, SongModel.Song songFromRequest)
        {
            // find this song in existing items -- return the index
            var index = 0;

            foreach (var existingSong in existingSongList)
            {
                if (existingSong.Title == songFromRequest.Title && existingSong.Artist == songFromRequest.Artist && existingSong.SongNumber == songFromRequest.SongNumber)
                {
                    LambdaLogger.Log($"FOUND SONG INDEX: {index}");
                    return(index);
                }
                index += 1;
            }
            return(-1);
        }
        //--- Methods ---
        public override async Task <CustomSkillResponse> HandleRequest(CustomSkillRequest customSkillRequest)
        {
            var intentSlots = customSkillRequest.Intent.Slots;
            var intentName  = customSkillRequest.Intent.Name;

            FoundSongs    = new List <SongModel.Song>();
            SongRequested = new SongModel.Song();

            // lookup song title and artist
            GetSongInfoRequested(intentSlots);
            if (IsValidRequest())
            {
                await FindRequestedSong();
            }

            // if no songs found, top matches
            if (FoundSongs.ToList().Count == 0)
            {
                await FindSimilarSongs();
            }

            // generate sqs body and send to the queue
            var generatedMessage = GenerateMessage();

            if (FoundSongs.ToList().Count == 1)
            {
                var sqsRequest = GenerateJukeboxSqsRequest(intentName, generatedMessage, FoundSongs.ToList().FirstOrDefault().SongNumber);
                await SendSqsRequest(sqsRequest, intentName);
            }
            else if (FoundSongs.ToList().Count > 1)
            {
                generatedMessage += "Top 3 matches: ";
                generatedMessage += FoundSongs.Aggregate(" ", (current, song) => current + $"Song {song.SongNumber} {song.Title} by {song.Artist}. ");
            }

            // generate alexa response
            var customSkillResponse = new CustomSkillResponse {
                Message = generatedMessage
            };

            LambdaLogger.Log($"*** INFO: Alexa response to user: {JsonConvert.SerializeObject(customSkillResponse)}");
            return(customSkillResponse);
        }
 //--- Constructor ---
 public PlaySongNumberRequest(ICommonDependencyProvider provider, IAmazonSQS awsSqsClient, string queueUrl, IDynamodbDependencyProvider awsDynmodbProvider) : base(provider, awsSqsClient, queueUrl)
 {
     SongRequested    = new SongModel.Song();
     FoundSongs       = new List <SongModel.Song>();
     DynamodbProvider = awsDynmodbProvider;
 }
Beispiel #5
0
        public async Task InsertSong(IList <SongModel.Song> existingSongsInDb, int songIndexInExistingSongs, IDictionary <string, AttributeValue> key, SongModel.Song songToInsert)
        {
            // other info
            var totalExistingSongs = existingSongsInDb.Count;

            LambdaLogger.Log($"totalExistingSongs: {totalExistingSongs}");

            // no record for that word
            if (totalExistingSongs == 0)
            {
                LambdaLogger.Log($"ADDING NEW ENTRY FOR SONG: {JsonConvert.SerializeObject(songToInsert)}");
                await AddWordSongs(key.ToDictionary(x => x.Key, x => x.Value), songToInsert).ConfigureAwait(false);
            }
            // existing record for that word and but song doesn't exist
            else if (totalExistingSongs > 0 && songIndexInExistingSongs == -1)
            {
                LambdaLogger.Log($"INSERTING ENTRY FOR WORD: {JsonConvert.SerializeObject(songToInsert)}");
                await InsertWordSongs(key.ToDictionary(x => x.Key, x => x.Value), songToInsert, totalExistingSongs).ConfigureAwait(false);
            }
            // song found in list, update that item
            else if (totalExistingSongs > 0 && songIndexInExistingSongs >= 0)
            {
                LambdaLogger.Log($"UPDATING ENTRY FOR WORD: {JsonConvert.SerializeObject(songToInsert)}");
                await InsertWordSongs(key.ToDictionary(x => x.Key, x => x.Value), songToInsert, songIndexInExistingSongs).ConfigureAwait(false);
            }
        }
Beispiel #6
0
        //--- Methods ---
        public async Task HandleRequest(DynamoDBEvent.DynamodbStreamRecord dynamodbStreamRecord)
        {
            var action      = dynamodbStreamRecord.EventName.Value;
            var requestItem = new Dictionary <string, AttributeValue>();

            LambdaLogger.Log($"Action: {action}");
            switch (action)
            {
            case "INSERT": {
                requestItem = dynamodbStreamRecord.Dynamodb.NewImage;
                break;
            }

            case "REMOVE": {
                requestItem = dynamodbStreamRecord.Dynamodb.OldImage;
                break;
            }
            }

            LambdaLogger.Log($"Request Item: {JsonConvert.SerializeObject(requestItem)}");
            requestItem.TryGetValue("song_number", out var number);
            requestItem.TryGetValue("artist", out var artist);
            requestItem.TryGetValue("title", out var title);

            // song to remove from index
            var songItem = new SongModel.Song {
                Artist     = artist.S,
                Title      = title.S,
                SongNumber = number.S
            };

            LambdaLogger.Log($"INDEXED SONG ITEM: {JsonConvert.SerializeObject(songItem)}");

            var splitSongTitle = title.S.Split(" ");

            foreach (var wordFromList in splitSongTitle)
            {
                var word = wordFromList.ToLowerInvariant();

                // key
                var recordKey = new Dictionary <string, AttributeValue> {
                    {
                        "word", new AttributeValue {
                            S = word
                        }
                    }
                };

                // Find existing songs in database for that word
                var existingSongsInDb = await GetExistingSongs(recordKey).ConfigureAwait(false);

                LambdaLogger.Log($"EXISTING SONGS IN DATABASE: {JsonConvert.SerializeObject(existingSongsInDb)}");

                var songIndexInExistingSongs = GetIndexOfThisSong(existingSongsInDb, songItem);
                LambdaLogger.Log($"songIndexInExistingSongs: {songIndexInExistingSongs}");

                switch (action)
                {
                case "INSERT": {
                    LambdaLogger.Log($"INDEXING WORD: {word}");
                    await InsertSong(existingSongsInDb, songIndexInExistingSongs, recordKey, songItem).ConfigureAwait(false);
                }
                break;

                case "REMOVE": {
                    LambdaLogger.Log($"INDEXING WORD: {word}");
                    await DeleteSong(existingSongsInDb, songIndexInExistingSongs, recordKey).ConfigureAwait(false);
                }
                break;
                }
            }
        }
Beispiel #7
0
 public async Task InsertWordSongs(Dictionary <string, AttributeValue> recordKey, SongModel.Song songToInsert, int songIndexInExistingSongs)
 {
     var expressionAttributeValues = new Dictionary <string, AttributeValue> {
         {
             ":n", new AttributeValue {
                 S = JsonConvert.SerializeObject(songToInsert)
             }
         }
     };
     var updateExpression = $"SET songs[{songIndexInExistingSongs}] = :n";
     await DynamodbProvider.DynamodbUpdateItemAsync(recordKey, updateExpression, expressionAttributeValues);
 }
Beispiel #8
0
 public async Task AddWordSongs(Dictionary <string, AttributeValue> recordKey, SongModel.Song songToInsert)
 {
     var expressionAttributeValues = new Dictionary <string, AttributeValue> {
         {
             ":n", new AttributeValue {
                 L = new List <AttributeValue> {
                     new AttributeValue {
                         S = JsonConvert.SerializeObject(songToInsert)
                     }
                 }
             }
         }
     };
     var updateExpression = "SET songs = :n";
     await DynamodbProvider.DynamodbUpdateItemAsync(recordKey, updateExpression, expressionAttributeValues);
 }
Beispiel #9
0
 //--- Constructor ---
 public PlaySongTitleArtistRequest(ICommonDependencyProvider provider, IAmazonSQS awsSqsClient, string queueUrl, IDynamodbDependencyProvider awsDynmodbProvider) : base(provider, awsSqsClient, queueUrl)
 {
     SongRequested    = new SongModel.Song();
     DynamodbProvider = awsDynmodbProvider;
 }