def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        logger.info("In PlayMetronomeHandler")

        tempo = int(get_slot_value(handler_input, "tempo"))
        if tempo < 20 or tempo > 240:
            handler_input.response_builder.speak(strings.TEMPO_NOT_IN_RANGE)
            return handler_input.response_builder.response

        duration = (get_slot_value(handler_input, "duration")
                    or strings.DEFAULT_DURATION)
        seconds = iso_8601_duration_to_seconds(duration)
        if seconds < 10 or seconds > 600:
            handler_input.response_builder.speak(strings.DURATION_NOT_IN_RANGE)
            return handler_input.response_builder.response

        logger.info("tempo={} duration={} seconds={}".format(
            tempo, duration, seconds))

        speak = strings.PLAYING.format(
            tempo, "{}'{}\"".format(int(seconds / 60), seconds % 60))

        data = {
            "card": {
                "title": strings.SKILL_NAME,
                "text": strings.CARD_TEXT.format(tempo),
                "small_image_url": strings.ICON_URL,
                "large_image_url": strings.ICON_URL
            },
            "url": get_metronome_url(tempo, seconds),
        }

        card = StandardCard(
            title=data["card"]["title"],
            text=data["card"]["text"],
            image=Image(small_image_url=data["card"]["small_image_url"],
                        large_image_url=data["card"]["large_image_url"]))

        directive = PlayDirective(
            play_behavior=PlayBehavior.REPLACE_ALL,
            audio_item=AudioItem(
                stream=Stream(expected_previous_token=None,
                              token=data["url"],
                              url=data["url"],
                              offset_in_milliseconds=0),
                metadata=AudioItemMetadata(
                    title=data["card"]["title"],
                    subtitle=data["card"]["text"],
                    art=display.Image(
                        content_description=data["card"]["title"],
                        sources=[display.ImageInstance(url=strings.ICON_URL)]),
                    background_image=display.Image(
                        content_description=data["card"]["title"],
                        sources=[display.ImageInstance(url=strings.ICON_URL)
                                 ]))))
        (handler_input.response_builder.speak(speak).set_card(
            card).add_directive(directive).set_should_end_session(True))
        return handler_input.response_builder.response
Esempio n. 2
0
def add_screen_background(card_data):
    if card_data:
        metadata = AudioItemMetadata(
            title=card_data["title"],
            subtitle=card_data["subtitle"],
            art=display.Image(
                sources=[display.ImageInstance(url=card_data['icon_url'])]))
        return metadata
    else:
        return None
Esempio n. 3
0
def add_screen_background(card_data):
    # type: (Dict) -> Optional[AudioItemMetadata]
    if card_data:
        metadata = AudioItemMetadata(
            title=card_data["title"],
            subtitle=card_data["text"],
            art=display.Image(
                content_description=card_data["title"],
                sources=[
                    display.ImageInstance(
                        url="https://alexademo.ninja/skills/logo-512.png")
                ]),
            background_image=display.Image(
                content_description=card_data["title"],
                sources=[
                    display.ImageInstance(
                        url="https://alexademo.ninja/skills/logo-512.png")
                ]))
        return metadata
    else:
        return None
Esempio n. 4
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        speech_text = "Welcome to audioplayer sample"

        card = StandardCard(
            title=en_us_audio_data["card"]["title"],
            text=en_us_audio_data["card"]["text"],
            image=Image(
                small_image_url=en_us_audio_data["card"]["small_image_url"],
                large_image_url=en_us_audio_data["card"]["large_image_url"]))

        directive = PlayDirective(
            play_behavior=PlayBehavior.REPLACE_ALL,
            audio_item=AudioItem(
                stream=Stream(expected_previous_token=None,
                              token=en_us_audio_data["url"],
                              url=en_us_audio_data["url"],
                              offset_in_milliseconds=0),
                metadata=AudioItemMetadata(
                    title=en_us_audio_data["card"]["title"],
                    subtitle=en_us_audio_data["card"]["text"],
                    art=display.Image(
                        content_description=en_us_audio_data["card"]["title"],
                        sources=[
                            display.ImageInstance(
                                url=
                                "https://alexademo.ninja/skills/logo-512.png")
                        ]),
                    background_image=display.Image(
                        content_description=en_us_audio_data["card"]["title"],
                        sources=[
                            display.ImageInstance(
                                url=
                                "https://alexademo.ninja/skills/logo-512.png")
                        ]))))

        handler_input.response_builder.speak(speech_text).set_card(
            card).add_directive(directive).set_should_end_session(True)
        return handler_input.response_builder.response
Esempio n. 5
0
    def handle(self, handler_input):
        # type: (handler_input) -> Response

        logger.info(">>>>> In LaunchRequestHandler")
        interfaces = (ask_utils.request_util.get_supported_interfaces(
            handler_input).to_dict())

        with open("parameters.json") as conf_data:
            conf = json.load(conf_data)
        locale = ask_utils.get_locale(handler_input)
        logger.debug(f"Locale is {locale}")
        # localized strings stored in language_strings.json
        with open("language_strings.json") as language_prompts:
            language_data = json.load(language_prompts)
        try:
            loc_data = language_data[locale[:2]]
            skill_name = loc_data['SKILL_NAME']
        except KeyError:
            loc_data = language_data['en']
            skill_name = loc_data['SKILL_NAME']
        speak_output = loc_data['NO_VIDEO']
        channel_id = loc_data['CHANNEL']
        youtube_url = (
            "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId="
            + channel_id + "&maxResults=1&order=date&type=video&key=" +
            conf['youtube_key'])
        success = True
        try:
            url = urllib.request.urlopen(youtube_url)
        except URLError as e:
            speak_output = loc_data['URL_ERROR']
            logger.info("URLError %s", e)
            success = False
        except HTTPError as e:
            logger.info("HTTPError %s, %e", e, e.code)
            if e.code == 429:
                speak_output = loc_data['TOO_MANY_REQUESTS']
            success = False
        if success:
            try:
                data = json.loads(url.read().decode())
                videoId = data['items'][0]['id']['videoId']
                yt = YouTube('https://www.youtube.com/watch?v=' + videoId)
            except Exception as e:
                speak_output = loc_data['YOUTUBE_ERROR']
                success = False
        if success:
            logger.info("List of the available interfaces: %s", interfaces)
            if interfaces['video_app'] is not None:  # device has video support
                for stream in yt.streams.order_by('resolution').desc():
                    if stream.includes_audio_track:
                        break
                speak_output = loc_data[
                    'VIDEO_MESSAGE'] + ": " + stream.title + ". " + yt.description
                logger.info("Video {}. "
                            "Locale='{}'. "
                            "channel_id='{}'. "
                            "speak_output='{}'. "
                            "youtube_key='{}'".format(stream, locale[:2],
                                                      channel_id, speak_output,
                                                      conf['youtube_key']))
                directive = LaunchDirective(video_item=VideoItem(
                    source=stream.url,
                    metadata=Metadata(title=stream.title,
                                      subtitle=yt.description)))
                return (handler_input.response_builder.speak(
                    speak_output).add_directive(directive).response)
            elif interfaces[
                    'audio_player'] is not None:  # device has only audio support (no video)
                stream = (yt.streams.filter(
                    only_audio=True,
                    subtype='mp4').order_by('fps').desc().first())
                speak_output = loc_data[
                    'AUDIO_MESSAGE'] + ": " + stream.title + ". " + yt.description
                logger.info("Audio {}. "
                            "Locale='{}'. "
                            "channel_id='{}'. "
                            "speak_output='{}'. "
                            "youtube_key='{}'".format(stream, locale[:2],
                                                      channel_id, speak_output,
                                                      conf['youtube_key']))
                directive = PlayDirective(
                    play_behavior=PlayBehavior.REPLACE_ALL,
                    audio_item=AudioItem(
                        stream=Stream(expected_previous_token=None,
                                      token=stream.url,
                                      url=stream.url,
                                      offset_in_milliseconds=0),
                        metadata=AudioItemMetadata(
                            title=stream.title,
                            subtitle=yt.description,
                            art=display.Image(
                                content_description=stream.title,
                                sources=[
                                    display.ImageInstance(url=yt.thumbnail_url)
                                ]),
                            background_image=display.Image(
                                content_description=stream.title,
                                sources=[
                                    display.ImageInstance(url=yt.thumbnail_url)
                                ]))))
                return (handler_input.response_builder.speak(
                    speak_output).add_directive(
                        directive).set_should_end_session(True).response)
            else:
                speak_output = loc_data[
                    'NO_INTERFACE'] + ": " + stream.title + ". " + yt.description
        return (handler_input.response_builder.speak(speak_output).response)