Skip to content

Instantly share code, notes, and snippets.

@em-shea
Last active May 3, 2023 17:37
Show Gist options
  • Save em-shea/ac435fb068d161d1edf83acc12ccc1c4 to your computer and use it in GitHub Desktop.
Save em-shea/ac435fb068d161d1edf83acc12ccc1c4 to your computer and use it in GitHub Desktop.
Get words for a given list ID function
# Given a list id, retrieve an array with word ids and simplified characters for that list
# This function is part of a state machine that generates pronunciation audio files for a given list
import list_word_service
def lambda_handler(event, context):
list_id = event['list_id']
last_word_token = event['last_word_token']
# Call to list_word_service, a shared service in the application
# for querying DynamoDB for words by list ID
detailed_word_list = list_word_service.get_words_in_list(list_id, limit=200, last_word_token=last_word_token, audio_file_key_check=False)
# If there are exactly 200 words returned, there are more words left
# Set last_word_token to last word in the list
if len(detailed_word_list) == 200:
updated_last_word_token = detailed_word_list[-1]['word_id']
else:
updated_last_word_token = None
word_list = format_and_filter_word_list(detailed_word_list)
response_body = {
"list_id": list_id,
"word_list": word_list,
"last_word_token": updated_last_word_token
}
return response_body
# Remove the word details that aren't needed to reduce the payload size
def format_and_filter_word_list(detailed_word_list):
word_list = []
for item in detailed_word_list:
# Remove any words from the list that already have audio files
if item['word']['Audio file key'] == "":
word_list.append(
{
'list_id': item['list_id'],
'word_id': item['word_id'],
'text': item['word']['Simplified']
}
)
return word_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment