From 46c4faf118edb4a5f9c4e0e12e7190187fd3c157 Mon Sep 17 00:00:00 2001 From: Sarah Date: Sun, 27 Feb 2022 14:41:52 +0000 Subject: [PATCH] pass bar into add_imdb_data_to_viewings method --- update_films_by_year_page.py | 4 +++- vcinema_utils/VCinemaUtils.py | 29 ++++++++++++----------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/update_films_by_year_page.py b/update_films_by_year_page.py index c921b11..cc75829 100644 --- a/update_films_by_year_page.py +++ b/update_films_by_year_page.py @@ -31,7 +31,9 @@ def update_films_by_year_page(token_id, token_secret): print("Retrieving VCinema viewings") viewings = VCinemaUtils.get_vcinema_viewings(token_id, token_secret) - VCinemaUtils.add_imdb_data_to_viewings(viewings, ['year']) + viewing_count = len(viewings) + with Bar('Retrieving movie data', max=viewing_count, suffix='%(percent).1f%% - %(eta)ds remaining') as bar: + VCinemaUtils.add_imdb_data_to_viewings(viewings, ['year'], bar) viewings_by_year = VCinemaUtils.filter_viewings(viewings, 'year') diff --git a/vcinema_utils/VCinemaUtils.py b/vcinema_utils/VCinemaUtils.py index dc98bcb..a28a6c0 100644 --- a/vcinema_utils/VCinemaUtils.py +++ b/vcinema_utils/VCinemaUtils.py @@ -52,29 +52,24 @@ def get_vcinema_viewings(token_id, token_secret, combine_repeat_viewings=True): def get_imdb(imdb_id, bar): imdb_entry = IMDbUtils.get_movie(imdb_id) - bar.next() + if bar is not None: + bar.next() return imdb_entry -def add_imdb_data_to_viewings(viewings, field_names): - sys.stdout.write("\rRetrieving movie data") - sys.stdout.flush() +def add_imdb_data_to_viewings(viewings, field_names, progressbar=None): + with ThreadPoolExecutor(4) as executor: + future_to_url = {executor.submit(get_imdb, viewing['imdb_id'], progressbar) for viewing in viewings} - viewing_count = len(viewings) + for future in as_completed(future_to_url): + imdb_data = future.result() - with Bar('Retrieving movie data', max=viewing_count, suffix='%(percent).1f%% - %(eta)ds remaining') as bar: - with ThreadPoolExecutor(4) as executor: - future_to_url = {executor.submit(get_imdb, viewing['imdb_id'], bar) for viewing in viewings} - - for future in as_completed(future_to_url): - imdb_data = future.result() - - for viewing in viewings: - if viewing['imdb_id'] == imdb_data.movieID: - for field_name in field_names: - if field_name in imdb_data: - viewing[field_name] = imdb_data[field_name] + for viewing in viewings: + if viewing['imdb_id'] == imdb_data.movieID: + for field_name in field_names: + if field_name in imdb_data: + viewing[field_name] = imdb_data[field_name] def filter_viewings(viewings, filter_field):