From eab311ad9a64ef52e6cc2179b31e4698644e3adc Mon Sep 17 00:00:00 2001 From: Sarah Date: Sun, 18 Dec 2022 18:51:21 +0000 Subject: [PATCH] refactor map script --- ..._from_date.py => get_film_map_for_date.py} | 27 +++++++++---------- vcinema_utils/VCinemaUtils.py | 17 ++++++++---- 2 files changed, 24 insertions(+), 20 deletions(-) rename roundup_scripts/{get_map_from_date.py => get_film_map_for_date.py} (62%) diff --git a/roundup_scripts/get_map_from_date.py b/roundup_scripts/get_film_map_for_date.py similarity index 62% rename from roundup_scripts/get_map_from_date.py rename to roundup_scripts/get_film_map_for_date.py index 8549d37..1b884b6 100644 --- a/roundup_scripts/get_map_from_date.py +++ b/roundup_scripts/get_film_map_for_date.py @@ -1,47 +1,44 @@ import argparse +from collections import Counter from datetime import datetime import json from progress.bar import IncrementalBar from vcinema_utils import VCinemaUtils - -import base64 -from collections import Counter, OrderedDict -import csv -import os -import pyvips -import worldmap -import warnings - from wiki_pages.FilmsByCountry import draw_map -def get_date_map(token_id, token_secret, year): +def get_film_map_for_date(token_id, token_secret, date, outfile): print("Getting viewings") viewings = VCinemaUtils.get_vcinema_viewings(token_id, token_secret) with IncrementalBar('Retrieving movie data', max=len(viewings), suffix='%(percent).1f%% - %(eta)ds remaining', check_tty=False) as bar: VCinemaUtils.add_imdb_data_to_viewings(viewings, ["countries"], bar) - viewings_before_year = VCinemaUtils.get_viewings_before_date(viewings, datetime(day=31, month=12, year=year-1)) + viewings_before_year = VCinemaUtils.get_viewings_in_date_range(datetime.min, date) viewings_before_year_filtered_by_year = VCinemaUtils.filter_viewings(viewings_before_year, "countries") country_counter = Counter(viewings_before_year_filtered_by_year) png_data = draw_map(country_counter) - with open("map-2022.png", "wb") as f: + print("saving map to:" + outfile) + + with open(outfile, "wb") as f: f.write(png_data) + print("saved") + if __name__ == '__main__': - parser = argparse.ArgumentParser(description='Update wiki pages.') + parser = argparse.ArgumentParser() - parser.add_argument('year', type=int) + parser.add_argument('date', type=lambda s: datetime.strptime(s, '%d-%m-%Y')) + parser.add_argument('outfile', type=str) args = parser.parse_args() with open('../token.json') as json_file: token = json.load(json_file) - get_date_map(token['token_id'], token['token_secret'], args.year) + get_film_map_for_date(token['token_id'], token['token_secret'], args.date, args.outfile) diff --git a/vcinema_utils/VCinemaUtils.py b/vcinema_utils/VCinemaUtils.py index 41c3dac..bf52c73 100644 --- a/vcinema_utils/VCinemaUtils.py +++ b/vcinema_utils/VCinemaUtils.py @@ -120,14 +120,21 @@ def filter_viewings(viewings, filter_field): return viewings_filtered -def get_viewings_before_date(viewings, date): - viewings_before_date = [] +def get_viewings_in_date_range(viewings, start_date, end_date): + viewings_in_date_range = [] for viewing in viewings: - if all(view_date["date_watched"] < date for view_date in viewing["viewings"]): - viewings_before_date.append(viewing) + in_date_range = [] - return viewings_before_date + for film_viewing in viewing["viewings"]: + if start_date <= film_viewing["date_watched"] <= end_date: + in_date_range.append(film_viewing) + + if len(in_date_range) > 0: + viewing["viewings"] = in_date_range + viewings_in_date_range.append(viewing) + + return viewings_in_date_range def get_film_list(films):