def movies_by_year(): movies_per_page = 2 # Read query parameters. year = request.values.get('release_year') cursor = request.args.get('cursor') movie_to_show_reviews = request.args.get('view_reviews_for') year = int(year) if movie_to_show_reviews is None: # No view-reviews query parameter, so set to a non-existent movie rank. movie_to_show_reviews = -1 else: # Convert movie to show reviews from string to int. movie_to_show_reviews = int(movie_to_show_reviews) if cursor is None: # No cursor query parameter, so initialise cursor to start at the beginning. cursor = 0 else: # Convert cursor from string to int. cursor = int(cursor) # Retrieve movie ranks for movies that are released in that year. movie_ranks = services.get_movie_ranks_for_year(year, repo.repo_instance) #movie_ranks = services.get_movies_by_year(year, repo.repo_instance) # Retrieve the batch of movies to display on the Web page. movies = services.get_movies_by_rank( movie_ranks[cursor:cursor + movies_per_page], repo.repo_instance) movies_image = {} for movie in movies: title = movie["title"] args = {"t": title} link = "http://www.omdbapi.com/?{}&apikey=4421208f".format( urllib.parse.urlencode(args)) response = urlopen(link) content = response.read() content_dict = content.decode("UTF-8") detail = ast.literal_eval(content_dict) image = detail["Poster"] movies_image[movie["title"]] = image first_movie_url = None last_movie_url = None next_movie_url = None prev_movie_url = None if cursor > 0: # There are preceding movies, so generate URLs for the 'previous' and 'first' navigation buttons. prev_movie_url = url_for('movies_bp.movies_by_year', release_year=year, cursor=cursor - movies_per_page) first_movie_url = url_for('movies_bp.movies_by_year', release_year=year) if cursor + movies_per_page < len(movie_ranks): # There are further movies, so generate URLs for the 'next' and 'last' navigation buttons. next_movie_url = url_for('movies_bp.movies_by_year', release_year=year, cursor=cursor + movies_per_page) last_cursor = movies_per_page * int(len(movie_ranks) / movies_per_page) if len(movie_ranks) % movies_per_page == 0: last_cursor -= movies_per_page last_movie_url = url_for('movies_bp.movies_by_year', release_year=year, cursor=last_cursor) # Construct urls for viewing movie reviews and adding reviews. for movie in movies: movie['view_review_url'] = url_for('movies_bp.movies_by_year', release_year=year, cursor=cursor, view_reviews_for=movie['rank']) movie['add_review_url'] = url_for('movies_bp.review_on_movie', movie=movie['rank']) # Generate the webpage to display the movies. return render_template( 'movies/movies.html', #title='Movies', movies_title='Movies released in ' + str(year), #release_year=year, movies=movies, image=movies_image, selected_movies=utilities.get_selected_movies(10), #len(movies) * 2), year_urls=utilities.get_years_and_urls(), genre_urls=utilities.get_genres_and_urls(), rank_urls=utilities.get_rank_and_url(), first_movie_url=first_movie_url, last_movie_url=last_movie_url, prev_movie_url=prev_movie_url, next_movie_url=next_movie_url, show_reviews_for_movie=movie_to_show_reviews, )
def home(): return render_template('home/home.html', selected_movies=utilities.get_selected_movies(), genre_urls=utilities.get_genres_and_urls())
def movies_by_year(): # Read query parameters. target_year = request.args.get('release_year') movies_to_show_reviews = request.args.get('view_reviews_for') # 不确定 # Fetch the first and last articles in the series. first_movie = services.get_first_movie(repo.repo_instance) last_movie = services.get_last_movie(repo.repo_instance) if target_year is None: target_year = first_movie['release_year'] else: target_year = int(target_year) if movies_to_show_reviews is None: movies_to_show_reviews = -1 else: movies_to_show_reviews = int(movies_to_show_reviews) movies, previous_year, next_year = services.get_movies_by_year( target_year, repo.repo_instance) first_movie_url = None last_movie_url = None next_movie_url = None prev_movie_url = None if len(movies) > 0: if previous_year is not None: prev_movie_url = url_for('movie_bp.movies_by_year', release_year=int(previous_year)) first_movie_url = url_for('movie_bp.movies_by_year', release_year=int( first_movie['release_year'])) if next_year is not None: next_movie_url = url_for('movie_bp.movies_by_year', release_year=int(next_year)) last_movie_url = url_for('movie_bp.movies_by_year', release_year=int( last_movie['release_year'])) for movie in movies: movie['view_review_url'] = url_for('movie_bp.movies_by_year', release_year=target_year, view_reviews_for=movie['id']) movie['add_review_url'] = url_for('movie_bp.review_on_movie', movie=movie['id']) return render_template( 'movie/movie.html', title='Movies', movie_title=target_year, movies=movies, selected_movies=utilities.get_selected_movies(5), genre_urls=utilities.get_genres_and_urls(), first_movie_url=first_movie_url, last_movie_url=last_movie_url, prev_movie_url=prev_movie_url, next_movie_url=next_movie_url, show_reviews_for_movie=movies_to_show_reviews) return redirect(url_for('home_bp.home'))
def movies_by_genre(): movies_per_page = 100 genre_name = request.args.get('genre') cursor = request.args.get('cursor') movies_to_show_reviews = request.args.get('view_reviews_for') if movies_to_show_reviews is None: movies_to_show_reviews = -1 else: movies_to_show_reviews = int(movies_to_show_reviews) if cursor is None: cursor = 0 else: cursor = int(cursor) movie_ids = services.get_movie_ids_for_genre(genre_name, repo.repo_instance) movies = services.get_movies_by_id( movie_ids[cursor:cursor + movies_per_page], repo.repo_instance) first_movie_url = None last_movie_url = None next_movie_url = None prev_movie_url = None if cursor > 0: prev_movie_url = url_for('movie_bp.movies_by_genre', genre=genre_name, cursor=cursor - movies_per_page) first_movie_url = url_for('movie_bp.movies_by_genre', genre=genre_name) if cursor + movies_per_page < len(movie_ids): next_movie_url = url_for('movie_bp.movies_by_genre', genre=genre_name, cursor=cursor + movies_per_page) last_cursor = movies_per_page * int(len(movie_ids) / movies_per_page) if len(movie_ids) % movies_per_page == 0: last_cursor -= movies_per_page last_movie_url = url_for('movie_bp.movies_by_genre', genre=genre_name, cursor=last_cursor) for movie in movies: movie['view_review_url'] = url_for('movie_bp.movies_by_genre', genre=genre_name, cursor=cursor, view_reviews_for=movie['id']) movie['add_review_url'] = url_for('movie_bp.review_on_movie', movie=movie['id']) return render_template('movie/movie.html', title='movies', movie_title='Movies tagged by ' + genre_name, movies=movies, selected_movies=utilities.get_selected_movies(5), genre_urls=utilities.get_genres_and_urls(), first_movie_url=first_movie_url, last_movie_url=last_movie_url, prev_movie_url=prev_movie_url, next_movie_url=next_movie_url, show_reviews_for_movie=movies_to_show_reviews)
def genres_list(): return render_template('genres_list.html', genre_urls=utilities.get_genres_and_urls(), selected_movies=utilities.get_selected_movies(5))