'Perform imdb lookup for entry.
:param entry: Entry instance
:param search_allowed: Allow fallback to search
:raises PluginError: Failure reason'

@plugin.internet(log)
@with_session
def lookup(self, entry, search_allowed=True, session=None):

    from flexget.manager import manager
    if entry.get(u'imdb_id', eval_lazy=False):
        log.debug((u'No    title    passed.    Lookup    for    %s' % entry[u'imdb_id']))
    elif entry.get(u'imdb_url', eval_lazy=False):
        log.debug((u'No    title    passed.    Lookup    for    %s' % entry[u'imdb_url']))
    elif entry.get(u'title', eval_lazy=False):
        log.debug((u'lookup    for    %s' % entry[u'title']))
    else:
        raise plugin.PluginError(u'looking    up    IMDB    for    entry    failed,    no    title,    imdb_url    or    imdb_id    passed.')
    if (entry.get(u'imdb_id', eval_lazy=False) and (not entry.get(u'imdb_url', eval_lazy=False))):
        entry[u'imdb_url'] = make_url(entry[u'imdb_id'])
    if entry.get(u'imdb_url', eval_lazy=False):
        imdb_id = extract_id(entry[u'imdb_url'])
        if imdb_id:
            entry[u'imdb_url'] = make_url(imdb_id)
        else:
            log.debug((u'imdb    url    %s    is    invalid,    removing    it' % entry[u'imdb_url']))
            del entry[u'imdb_url']
    if (not entry.get(u'imdb_url', eval_lazy=False)):
        result = session.query(SearchResult).filter((SearchResult.title == entry[u'title'])).first()
        if result:
            if (result.fails and (not manager.options.execute.retry)):
                log.debug((u'%s    will    fail    lookup' % entry[u'title']))
                raise plugin.PluginError((u'IMDB    lookup    failed    for    %s' % entry[u'title']))
            elif result.url:
                log.trace((u'Setting    imdb    url    for    %s    from    db' % entry[u'title']))
                entry[u'imdb_id'] = result.imdb_id
                entry[u'imdb_url'] = result.url
    if ((not entry.get(u'imdb_url', eval_lazy=False)) and search_allowed):
        log.verbose((u'Searching    from    imdb    `%s`' % entry[u'title']))
        search = ImdbSearch()
        search_name = entry.get(u'movie_name', entry[u'title'], eval_lazy=False)
        search_result = search.smart_match(search_name)
        if search_result:
            entry[u'imdb_url'] = search_result[u'url']
            result = SearchResult(entry[u'title'], entry[u'imdb_url'])
            session.add(result)
            session.commit()
            log.verbose((u'Found    %s' % entry[u'imdb_url']))
        else:
            log_once((u'IMDB    lookup    failed    for    %s' % entry[u'title']), log, logging.WARN, session=session)
            result = SearchResult(entry[u'title'])
            result.fails = True
            session.add(result)
            session.commit()
            raise plugin.PluginError((u'Title    `%s`    lookup    failed' % entry[u'title']))
    movie = session.query(Movie).filter((Movie.url == entry[u'imdb_url'])).first()
    if (movie and (not movie.expired)):
        entry.update_using_map(self.field_map, movie)
        return
    if (movie is not None):
        if movie.expired:
            log.verbose((u'Movie    `%s`    details    expired,    refreshing    ...' % movie.title))
        session.query(MovieLanguage).filter((MovieLanguage.movie_id == movie.id)).delete()
        session.query(Movie).filter((Movie.url == entry[u'imdb_url'])).delete()
        session.commit()
    if (u'title' in entry):
        log.verbose((u'Parsing    imdb    for    `%s`' % entry[u'title']))
    else:
        log.verbose((u'Parsing    imdb    for    `%s`' % entry[u'imdb_id']))
    try:
        movie = self._parse_new_movie(entry[u'imdb_url'], session)
    except UnicodeDecodeError:
        log.error((u'Unable    to    determine    encoding    for    %s.    Installing    chardet    library    may    help.' % entry[u'imdb_url']))
        movie = Movie()
        movie.url = entry[u'imdb_url']
        session.add(movie)
        session.commit()
        raise plugin.PluginError(u'UnicodeDecodeError')
    except ValueError as e:
        if manager.options.debug:
            log.exception(e)
        raise plugin.PluginError((u'Invalid    parameter:    %s' % entry[u'imdb_url']), log)
    for att in [u'title', u'score', u'votes', u'year', u'genres', u'languages', u'actors', u'directors', u'writers', u'mpaa_rating']:
        log.trace((u'movie.%s:    %s' % (att, getattr(movie, att))))
    entry.update_using_map(self.field_map, movie)
