refactor map script

This commit is contained in:
Sarah 2022-12-18 18:51:21 +00:00
parent 1eced2ee66
commit eab311ad9a
2 changed files with 24 additions and 20 deletions

View File

@ -1,47 +1,44 @@
import argparse import argparse
from collections import Counter
from datetime import datetime from datetime import datetime
import json import json
from progress.bar import IncrementalBar from progress.bar import IncrementalBar
from vcinema_utils import VCinemaUtils 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 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") print("Getting viewings")
viewings = VCinemaUtils.get_vcinema_viewings(token_id, token_secret) 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: 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) 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") viewings_before_year_filtered_by_year = VCinemaUtils.filter_viewings(viewings_before_year, "countries")
country_counter = Counter(viewings_before_year_filtered_by_year) country_counter = Counter(viewings_before_year_filtered_by_year)
png_data = draw_map(country_counter) 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) f.write(png_data)
print("saved")
if __name__ == '__main__': 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() args = parser.parse_args()
with open('../token.json') as json_file: with open('../token.json') as json_file:
token = json.load(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)

View File

@ -120,14 +120,21 @@ def filter_viewings(viewings, filter_field):
return viewings_filtered return viewings_filtered
def get_viewings_before_date(viewings, date): def get_viewings_in_date_range(viewings, start_date, end_date):
viewings_before_date = [] viewings_in_date_range = []
for viewing in viewings: for viewing in viewings:
if all(view_date["date_watched"] < date for view_date in viewing["viewings"]): in_date_range = []
viewings_before_date.append(viewing)
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): def get_film_list(films):