Python 100 project #8: Trending Wordcloud – on Alexa Echo Spot – Cavs is hot in US, while Liverpool is hot in UK

I have added display interface on the existing Alexa skill. It retrieves the wordcloud image from s3 (if it is there), and it generates the wordcloud image file if it doesn’t found one. And it stores the newly generate file onto the s3 bucket. So basically it will generate respective wordcloud file on the first request of respective country. Subsequent request (of the day) just retrieves the image file from s3.

 

Output Example:

  • this is the result for “Ask ‘skill-name’ to show me the trends”, which shows the US(default) trending search keyword

  • this is the result for “Ask ‘skill-name’ to give me the trends in United Kingdom”, which shows the UK trending search keyword

 

Here is the Lambda function to provide both speech output and display output:

import urllib.request

import boto3

# --------------- Global Variables ---------------------------------------------

COUNTRYDICT = {'Argentina': 'p30', 'Australia': 'p8', 'Austria': 'p44', 'Belgium': 'p41', 'Brazil': 'p18',
               'Canada': 'p13', 'Chile': 'p38', 'Colombia': 'p32', 'Czech Republic': 'p43', 'Egypt': 'p29',
               'Finland': 'p50', 'France': 'p16', 'Germany': 'p15', 'Greece': 'p48', 'Hong Kong': 'p10',
               'Hungary': 'p45', 'India': 'p3', 'Indonesia': 'p19', 'Israel': 'p6', 'Italy': 'p27', 'Japan': 'p4',
               'Kenya': 'p37', 'Malaysia': 'p34', 'Mexico': 'p21', 'Netherlands': 'p17', 'New Zealand': 'p53',
               'Nigeria': 'p52', 'Norway': 'p51', 'Philippines': 'p25', 'Poland': 'p31', 'Romania': 'p39',
               'Russia': 'p14', 'Saudi Arabia': 'p36', 'Singapore': 'p5', 'South Africa': 'p40', 'South Korea': 'p23',
               'Spain': 'p26', 'Sweden': 'p42', 'Switzerland': 'p46', 'Taiwan': 'p12', 'Thailand': 'p33',
               'Turkey': 'p24', 'Ukraine': 'p35', 'United Kingdom': 'p9', 'United States': 'p1', 'Vietnam': 'p28'}

BUCKETNAME = "your-bucket-name"


# --------------- Helpers that build all of the responses ----------------------

def build_speechlet_response(title, output, reprompt_text, should_end_session):
    return {
        'outputSpeech': {
            'type': 'PlainText',
            'text': output
        },
        'card': {
            'type': 'Simple',
            'title': "SessionSpeechlet - " + title,
            'content': "SessionSpeechlet - " + output
        },
        'reprompt': {
            'outputSpeech': {
                'type': 'PlainText',
                'text': reprompt_text
            }
        },
        'shouldEndSession': should_end_session
    }


def build_display_response(wc_image):
    return {
        "directives": [
            {
                "type": "Display.RenderTemplate",
                "template": {
                    "type": "BodyTemplate1",
                    "token": "GoogleTrendsSearch",
                    "backButton": "HIDDEN",
                    "backgroundImage": {
                        "contentDescription": "The Image",
                        "sources": [
                            {
                                "url": wc_image,
                            },
                        ]
                    },
                    "title": "",
                    "textContent": {
                        "primaryText": {
                            "text": "",
                            "type": "PlainText"
                        }
                    }
                }
            }
        ]
    }


def build_response(session_attributes, speechlet_response, display_response):
    speechlet_response.update(display_response)

    return {
        'version': '1.0',
        'sessionAttributes': session_attributes,
        'response': speechlet_response
    }


# --------------- Functions that control the skill's behavior ------------------

def get_welcome_response():
    """ If we wanted to initialize the session to have some attributes we could
    add those here
    """

    session_attributes = {}
    card_title = "Welcome"
    speech_output = "Welcome to the Mister Trends Alexa SKill. " \
                    "This skill will tell you the trends based on Google Trends."
    reprompt_text = "Please tell me give me the latest news."
    should_end_session = False
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session), {})


def handle_session_end_request():
    card_title = "Session Ended"
    speech_output = "Thank you for trying the Mister Trends Alexa Skill. " \
                    "Have a nice day! "
    should_end_session = True
    return build_response({}, build_speechlet_response(
        card_title, speech_output, None, should_end_session), {})


def get_latest_news(intent, session):
    """ Sets the color in the session and prepares the speech to reply to the
    user.
    """
    import datetime
    import operator
    import urllib.request

    card_title = intent['name']
    session_attributes = {}
    should_end_session = True

    country = intent['slots']['Country'].get('value')

    if country:
        pn = COUNTRYDICT[country]
    else:
        pn = 'p1'

    word_dict = get_latest_trends(pn)

    print('word_dict generated: ', word_dict)

    file_name = pn + str(datetime.date.today()) + '.png'

    bucket_url = 'https://s3.amazonaws.com/' + BUCKETNAME

    try:
        urllib.request.urlopen(bucket_url + '/' + file_name)
    except:
        generate_wordcloud(word_dict, file_name)

    speech_output = "Here is the trending searches"

    sorted_list = sorted(word_dict.items(), key=operator.itemgetter(1))

    for id, word in enumerate(sorted_list):
        speech_output += '. ' + str(id + 1) + '. ' + word[0]

    reprompt_text = ""

    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session),
                          build_display_response(bucket_url + '/' + file_name))


# --------------- Events ------------------

def on_session_started(session_started_request, session):
    """ Called when the session starts """

    print("on_session_started requestId=" + session_started_request['requestId']
          + ", sessionId=" + session['sessionId'])


def on_launch(launch_request, session):
    """ Called when the user launches the skill without specifying what they
    want
    """

    print("on_launch requestId=" + launch_request['requestId'] +
          ", sessionId=" + session['sessionId'])
    # Dispatch to your skill's launch
    return get_welcome_response()


def on_intent(intent_request, session):
    """ Called when the user specifies an intent for this skill """

    print("on_intent requestId=" + intent_request['requestId'] +
          ", sessionId=" + session['sessionId'])

    intent = intent_request['intent']
    intent_name = intent_request['intent']['name']

    # Dispatch to your skill's intent handlers
    if intent_name == "LatestNewsIntent":
        return get_latest_news(intent, session)
    elif intent_name == "WhatsMyColorIntent":
        return get_color_from_session(intent, session)
    elif intent_name == "AMAZON.HelpIntent":
        return get_welcome_response()
    elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
        return handle_session_end_request()
    else:
        raise ValueError("Invalid intent")


def on_session_ended(session_ended_request, session):
    """ Called when the user ends the session.

    Is not called when the skill returns should_end_session=true
    """
    print("on_session_ended requestId=" + session_ended_request['requestId'] +
          ", sessionId=" + session['sessionId'])
    # add cleanup logic here


# --------------- Helper -----------------------

def get_latest_trends(pn):
    """
    take country number (eg. p1 for USA) and retrieve hot search keywords from Google Trends.
    :param pn: string - p1, p2 etc
    :return: dictionary - key for the hot words, value for the volume of that word had been searched on the day
    """

    from pytrends.request import TrendReq

    pytrend = TrendReq()

    trending_searches_df = pytrend.trending_searches(pn=pn)

    trending_dict = {}
    for id, row in trending_searches_df.iterrows():
        trending_dict[row.title] = row.trafficBucketLowerBound

    return trending_dict


def generate_wordcloud(word_dict, file_name):
    """
    generate wordcloud from the two files given, and save the image file onto the s3 bucket
    :param word_dict: dictionary - key for the hot words, value for the volume of that word had been searched on the day
    :param file_name: string - file name of the generated file to be stored
    :return: True if everything goes ok
    """
    import matplotlib
    matplotlib.use('Agg')

    import numpy as np
    from PIL import Image
    from wordcloud import WordCloud

    urllib.request.urlretrieve("https://s3.amazonaws.com/your-bucket-name/480x480_circle.png", "/tmp/mask.png")

    mask = np.array(Image.open('/tmp/mask.png'))

    wordcloud = WordCloud(mask=mask)
    wordcloud.generate_from_frequencies(frequencies=word_dict)

    wordcloud.to_file('/tmp/' + file_name)

    s3_client = boto3.client('s3')
    with open('/tmp/' + file_name, 'rb') as f:
        image = f.read()
        s3_client.put_object(Body=image, Bucket=BUCKETNAME, Key=file_name, ACL='public-read')

    return True


# --------------- Main handler ------------------

def lambda_handler(event, context):
    """ Route the incoming request based on type (LaunchRequest, IntentRequest,
    etc.) The JSON body of the request is provided in the event parameter.
    """
    print("event.session.application.applicationId=" +
          event['session']['application']['applicationId'])

    """
    Uncomment this if statement and populate with your skill's application ID to
    prevent someone else from configuring a skill that sends requests to this
    function.
    """
    # if (event['session']['application']['applicationId'] !=
    #         "amzn1.echo-sdk-ams.app.[unique-value-here]"):
    #     raise ValueError("Invalid Application ID")

    if event['session']['new']:
        on_session_started({'requestId': event['request']['requestId']},
                           event['session'])

    if event['request']['type'] == "LaunchRequest":
        return on_launch(event['request'], event['session'])
    elif event['request']['type'] == "IntentRequest":
        return on_intent(event['request'], event['session'])
    elif event['request']['type'] == "SessionEndedRequest":
        return on_session_ended(event['request'], event['session'])