@hook.command()
@hook.command('tv_prev')
def tv_last(text, bot=None):
 api_key = bot.config.get('api_keys', {}).get('tvdb', None)
 if (api_key is None):
  return 'error: no api key set'
 episodes = get_episodes_for_series(text, api_key)
 if episodes['error']:
  return episodes['error']
 series_name = episodes['name']
 ended = episodes['ended']
 episodes = episodes['episodes']
 prev_ep = None
 today = datetime.date.today()
 for episode in reversed(episodes):
  ep_info = get_episode_info(episode)
  if (ep_info is None):
   continue
  (first_aired, air_date, episode_desc) = ep_info
  if (air_date < today):
   prev_ep = '{} ({})'.format(first_aired, episode_desc)
   break
 if (not prev_ep):
  return 'There are no previously aired episodes for {}.'.format(series_name)
 if ended:
  return '{} ended. The last episode aired {}.'.format(series_name, prev_ep)
 return 'The last episode of {} aired {}.'.format(series_name, prev_ep)

@ hook . command ( ) @ hook . command ( 'tv_prev' ) def tv_last ( text , bot = None ) : api_key = bot . config . get ( 'api_keys' , { } ) . get ( 'tvdb' , None ) if ( api_key is None ) : return 'error: no api key set' episodes = get_episodes_for_series ( text , api_key ) if episodes [ 'error' ] : return episodes [ 'error' ] series_name = episodes [ 'name' ] ended = episodes [ 'ended' ] episodes = episodes [ 'episodes' ] prev_ep = None today = datetime . date . today ( ) for episode in reversed ( episodes ) : ep_info = get_episode_info ( episode ) if ( ep_info is None ) : continue ( first_aired , air_date , episode_desc ) = ep_info if ( air_date < today ) : prev_ep = '{} ({})' . format ( first_aired , episode_desc ) break if ( not prev_ep ) : return 'There are no previously aired episodes for {}.' . format ( series_name ) if ended : return '{} ended. The last episode aired {}.' . format ( series_name , prev_ep ) return 'The last episode of {} aired {}.' . format ( series_name , prev_ep )
tv_last <series> -- gets the most recently aired episode of <series> .