def refine(video, **kwargs):
 if (not isinstance(video, Episode)):
  logger.error('Cannot refine episodes')
  return
 if (video.series_tvdb_id and video.tvdb_id):
  logger.debug('No need to search')
  return
 logger.info('Searching series %r', video.series)
 results = search_series(video.series.lower())
 if (not results):
  logger.warning('No results for series')
  return
 logger.debug('Found %d results', len(results))
 matching_results = []
 for result in results:
  matching_result = {}
  series_names = [result['seriesName']]
  series_names.extend(result['aliases'])
  original_match = series_re.match(result['seriesName']).groupdict()
  series_year = None
  if result['firstAired']:
   series_year = datetime.strptime(result['firstAired'], '%Y-%m-%d').year
  if (video.year and series_year and (video.year != series_year)):
   logger.debug('Discarding series %r mismatch on year %d', result['seriesName'], series_year)
   continue
  for series_name in series_names:
   (series, year, country) = series_re.match(series_name).groups()
   if year:
    year = int(year)
   if (year and (video.original_series or (video.year != year))):
    logger.debug('Discarding series name %r mismatch on year %d', series, year)
    continue
   if (sanitize(series) == sanitize(video.series)):
    logger.debug('Found exact match on series %r', series_name)
    matching_result['match'] = {'series': original_match['series'], 'year': series_year, 'original_series': (original_match['year'] is None)}
    break
  if matching_result:
   matching_result['data'] = result
   matching_results.append(matching_result)
 if (not matching_results):
  logger.error('No matching series found')
  return
 if (len(matching_results) > 1):
  logger.error('Multiple matches found')
  return
 matching_result = matching_results[0]
 series = get_series(matching_result['data']['id'])
 logger.debug('Found series %r', series)
 video.series = matching_result['match']['series']
 video.year = matching_result['match']['year']
 video.original_series = matching_result['match']['original_series']
 video.series_tvdb_id = series['id']
 video.series_imdb_id = (series['imdbId'] or None)
 logger.info('Getting series episode %dx%d', video.season, video.episode)
 episode = get_series_episode(video.series_tvdb_id, video.season, video.episode)
 if (not episode):
  logger.warning('No results for episode')
  return
 logger.debug('Found episode %r', episode)
 video.tvdb_id = episode['id']
 video.title = (episode['episodeName'] or None)
 video.imdb_id = (episode['imdbId'] or None)

def refine ( video , ** kwargs ) : if ( not isinstance ( video , Episode ) ) : logger . error ( 'Cannot refine episodes' ) return if ( video . series_tvdb_id and video . tvdb_id ) : logger . debug ( 'No need to search' ) return logger . info ( 'Searching series %r' , video . series ) results = search_series ( video . series . lower ( ) ) if ( not results ) : logger . warning ( 'No results for series' ) return logger . debug ( 'Found %d results' , len ( results ) ) matching_results = [ ] for result in results : matching_result = { } series_names = [ result [ 'seriesName' ] ] series_names . extend ( result [ 'aliases' ] ) original_match = series_re . match ( result [ 'seriesName' ] ) . groupdict ( ) series_year = None if result [ 'firstAired' ] : series_year = datetime . strptime ( result [ 'firstAired' ] , '%Y-%m-%d' ) . year if ( video . year and series_year and ( video . year != series_year ) ) : logger . debug ( 'Discarding series %r mismatch on year %d' , result [ 'seriesName' ] , series_year ) continue for series_name in series_names : ( series , year , country ) = series_re . match ( series_name ) . groups ( ) if year : year = int ( year ) if ( year and ( video . original_series or ( video . year != year ) ) ) : logger . debug ( 'Discarding series name %r mismatch on year %d' , series , year ) continue if ( sanitize ( series ) == sanitize ( video . series ) ) : logger . debug ( 'Found exact match on series %r' , series_name ) matching_result [ 'match' ] = { 'series' : original_match [ 'series' ] , 'year' : series_year , 'original_series' : ( original_match [ 'year' ] is None ) } break if matching_result : matching_result [ 'data' ] = result matching_results . append ( matching_result ) if ( not matching_results ) : logger . error ( 'No matching series found' ) return if ( len ( matching_results ) > 1 ) : logger . error ( 'Multiple matches found' ) return matching_result = matching_results [ 0 ] series = get_series ( matching_result [ 'data' ] [ 'id' ] ) logger . debug ( 'Found series %r' , series ) video . series = matching_result [ 'match' ] [ 'series' ] video . year = matching_result [ 'match' ] [ 'year' ] video . original_series = matching_result [ 'match' ] [ 'original_series' ] video . series_tvdb_id = series [ 'id' ] video . series_imdb_id = ( series [ 'imdbId' ] or None ) logger . info ( 'Getting series episode %dx%d' , video . season , video . episode ) episode = get_series_episode ( video . series_tvdb_id , video . season , video . episode ) if ( not episode ) : logger . warning ( 'No results for episode' ) return logger . debug ( 'Found episode %r' , episode ) video . tvdb_id = episode [ 'id' ] video . title = ( episode [ 'episodeName' ] or None ) video . imdb_id = ( episode [ 'imdbId' ] or None )
simplify an expression using assumptions .