Search a movie on TMDB.

def search():
    """ Search a movie on TMDB.
    """
    redis_key = 's_%s' % request.args['query'].lower()
    cached = redis_ro_conn.get(redis_key)
    if cached:
        return Response(cached)
    else:
        try:
            found = get_on_tmdb(u'/search/movie', query=request.args['query'])
            movies = []
            for movie in found['results']:
                cast = get_on_tmdb(u'/movie/%s/casts' % movie['id'])
                year = datetime.strptime(movie['release_date'], '%Y-%m-%d').year if movie['release_date'] else None
                movies.append({'title': movie['original_title'],
                               'directors': [x['name'] for x in cast['crew'] if x['department'] == 'Directing' and x['job'] == 'Director'],
                               'year': year,
                               '_tmdb_id': movie['id']})
        except requests.HTTPError as err:
            return Response('TMDB API error: %s' % str(err), status=err.response.status_code)
        json_response = json.dumps({'movies': movies})
        redis_conn.setex(redis_key, app.config['CACHE_TTL'], json_response)
        return Response(json_response)