def alexa_pick_random_episode(slots): heard_show = str(slots['Show']['value']).lower().translate(None, string.punctuation) print('Trying to play a random episode of %s' % (heard_show)) sys.stdout.flush() shows = kodi.GetTvShows() if 'result' in shows and 'tvshows' in shows['result']: shows_array = shows['result']['tvshows'] located = kodi.matchHeard(heard_show, shows_array) if located: episodes_result = kodi.GetUnwatchedEpisodesFromShow(located['tvshowid']) if not 'episodes' in episodes_result['result']: episodes_result = kodi.GetEpisodesFromShow(located['tvshowid']) episodes_array = [] for episode in episodes_result['result']['episodes']: episodes_array.append(episode['episodeid']) kodi.ClearVideoPlaylist() kodi.PrepEpisodePlayList(random.choice(episodes_array)) kodi.StartVideoPlaylist() return build_alexa_response('Playing a random episode of %s' % (heard_show)) else: return build_alexa_response('Could not find %s' % (heard_show)) else: return build_alexa_response('Could not find %s' % (heard_show))
def alexa_play_random_episode(slots): heard_show = str(slots['Show']['value']).lower().translate(None, string.punctuation) card_title = 'Playing a random episode of %s' % (heard_show) print card_title sys.stdout.flush() shows = kodi.GetTvShows() if 'result' in shows and 'tvshows' in shows['result']: shows_array = shows['result']['tvshows'] located = kodi.matchHeard(heard_show, shows_array) if located: episodes_result = kodi.GetUnwatchedEpisodesFromShow(located['tvshowid']) if not 'episodes' in episodes_result['result']: # Fall back to all episodes if no unwatched available episodes_result = kodi.GetEpisodesFromShow(located['tvshowid']) episodes_array = [] for episode in episodes_result['result']['episodes']: episodes_array.append(episode['episodeid']) episode_id = random.choice(episodes_array) episode_details = kodi.GetEpisodeDetails(episode_id) kodi.PlayEpisode(episode_id, False) return build_alexa_response('Playing season %d episode %d of %s' % (episode_details['season'], episode_details['episode'], heard_show), card_title) else: return build_alexa_response('Could not find a show named %s' % (heard_show), card_title) else: return build_alexa_response('Error parsing results', card_title)
def alexa_pick_random_episode(slots): shows = kodi.GetTvShows() shows_array = shows['result']['tvshows'] heard_show = str(slots['Show']['value']).lower().translate(None, string.punctuation) located = None for show in shows_array: ascii_name = show['label'].encode('ascii', 'replace') show_name = str(ascii_name).lower().translate(None, string.punctuation) if show_name == heard_show: located = show_name show_id = show['tvshowid'] break if not located: # Try an exact match after removing any leading "the" heard_minus_the = remove_the(heard_show) for show in shows_array: ascii_name = show['label'].encode('ascii', 'replace') show_name = str(ascii_name).lower().translate(None, string.punctuation) if remove_the(show_name) == heard_minus_the: located = show_name show_id = show['tvshowid'] break if not located: #Try removing everthing in parenthesis for shows that might have (2009) or (US) for show in shows_array: ascii_name = show['label'].encode('ascii', 'replace') removed_paren = re.sub(r'\([^)]*\)', '', ascii_name).rstrip() show_name = str(removed_paren).lower().translate(None, string.punctuation) if show_name == heard_show: located = show_name show_id = show['tvshowid'] break if not located: # Last resort -- take out some useless words and see if we have a show with # any of the remaining words in common heard_list = set([x for x in heard_show.split() if x not in STOPWORDS]) for show in shows_array: show_list = set(show['label'].split()) if heard_list & show_list: located = show show_id = show['tvshowid'] break if located: episodes_result = kodi.GetUnwatchedEpisodesFromShow(show_id) if len(episodes_result['result']['episodes']) < 1: episodes_result = kodi.GetEpisodesFromShow(show_id) episodes_array = [] for episode in episodes_result['result']['episodes']: episodes_array.append(episode['episodeid']) kodi.ClearVideoPlaylist() kodi.PrepEpisodePlayList(random.choice(episodes_array)) kodi.StartVideoPlaylist() return build_alexa_response('Playing a random episode of %s' % (located)) else: return build_alexa_response('Could not find %s' % (heard_show))