diff --git a/update_films_by_country_page.py b/update_films_by_country_page.py new file mode 100644 index 0000000..7f3bf6b --- /dev/null +++ b/update_films_by_country_page.py @@ -0,0 +1,140 @@ +import argparse +import base64 +from collections import Counter, OrderedDict +import os +from progress.bar import Bar +from wand.image import Image +import worldmap + + +from bookstack import Bookstack +from vcinema_utils import VCinemaUtils + + +FLAGS = { + "Australia": "🇦🇺", + "Belgium": "🇧🇪", + "Canada": "🇨🇦", + "China": "🇨🇳", + "Czech Republic": "🇨🇿", + "Czechia": "🇨🇿", + "France": "🇫🇷", + "Germany": "🇩🇪", + "Hong Kong": "🇭🇰", + "Hong Kong SAR China": "🇭🇰", + "Italy": "🇮🇹", + "Israel": "🇮🇱", + "Japan": "🇯🇵", + "Luxembourg": "🇱🇺", + "Mexico": "🇲🇽", + "New Zealand": "🇳🇿", + "Nigeria": "🇳🇬", + "North Korea": "🇰🇵", + "Philippines": "🇵🇭", + "Russia": "🇷🇺", + "Spain": "🇪🇸", + "South Africa": "🇿🇦", + "South Korea": "🇰🇷", + "Taiwan": "🇹🇼", + "Thailand": "🇹🇭", + "Turkey": "🇹🇷", + "Uganda": "🇺🇬", + "United Kingdom": "🇬🇧", + "United States": "🇺🇸", + "West Germany": "⬅️🇩🇪", +} + + +def build_table(fby, bar): + fby_sorted = OrderedDict(sorted(fby.items(), key=lambda t: t[0])) + + table = "" + + table += "" + table += "" + + for country in fby_sorted.keys(): + row = "" + + row += "" + + row += "" + row += "" + + table += row + + bar.next() + + table += "" + table += "
CountryFilms
" + row += "{} {}".format(str(country), FLAGS[country]) + row += "" + + row += "
".join(["{}".format(film['imdb_id'], film['title']) for film in fby_sorted[country]]) + + row += "
" + + return table + + +def draw_map(counter, file_name="vcinema_map.svg"): + countries = [k for k, v in counter.items()] + counts = [len(v) for _, v in counter.items()] + + max_count = max(counts) + + opacity = [0.5 + (x / (float(max_count))/2.0) for x in counts] + + worldmap.plot(countries, cmap=["#ff0000"], opacity=opacity, filename=file_name, verbose=False) + + with Image(filename=file_name, width=1000, height=655) as i: + png_data = i.make_blob("png") + + os.remove(file_name) + + return png_data + + +def update_page(map_png, table, wiki_url, token_id, token_secret): + page_id = 34 + + encoded = base64.b64encode(map_png).decode("utf-8") + + image_html = "".format(encoded) + + page_contents = image_html + table + + Bookstack.update_page(wiki_url, token_id, token_secret, page_id, html=page_contents) + + +def update_films_by_country_page(token_id, token_secret): + print("Retrieving VCinema viewings") + viewings = VCinemaUtils.get_vcinema_viewings(token_id, token_secret) + + viewing_count = len(viewings) + + with Bar('Retrieving movie data', max=viewing_count, suffix='%(percent).1f%% - %(eta)ds remaining', check_tty=False) as bar: + VCinemaUtils.add_imdb_data_to_viewings(viewings, ['countries'], bar) + + with Bar('Processing viewing data', max=viewing_count, suffix='%(percent).1f%% - %(eta)ds remaining', check_tty=False) as bar: + viewings_by_country = VCinemaUtils.filter_viewings(viewings, 'countries', bar) + + country_count = len(viewings_by_country.keys()) + with Bar('Generating table', max=country_count, suffix='%(percent).1f%% - %(eta)ds remaining', check_tty=False) as bar: + film_by_country_table = build_table(viewings_by_country, bar) + + country_counter = Counter(viewings_by_country) + + png_data = draw_map(country_counter) + + update_page(png_data, film_by_country_table, VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Update page displaying VCinema films sorted by country.') + parser.add_argument('token_id', help='API token ID.') + parser.add_argument('token_secret', help='API token secret.') + + args = parser.parse_args() + + update_films_by_country_page(args.token_id, args.token_secret) diff --git a/update_films_by_year_page.py b/update_films_by_year_page.py index b70cbd5..fa69d5e 100644 --- a/update_films_by_year_page.py +++ b/update_films_by_year_page.py @@ -2,7 +2,6 @@ import argparse from collections import OrderedDict from progress.bar import Bar -from bookstack import Bookstack from vcinema_utils import VCinemaUtils @@ -19,6 +18,9 @@ def build_table(films_by_year, progressbar=None): if progressbar is not None: progressbar.next() + if progressbar is not None: + progressbar.finish() + return page_table @@ -27,14 +29,14 @@ def update_films_by_year_page(token_id, token_secret): viewings = VCinemaUtils.get_vcinema_viewings(token_id, token_secret) 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) + with Bar('Retrieving movie data', max=viewing_count, suffix='%(percent).1f%% - %(eta)ds remaining', check_tty=False) as bar: + VCinemaUtils.add_imdb_data_to_viewings(viewings, bar) - with Bar('Processing viewing data', max=viewing_count, suffix='%(percent).1f%% - %(eta)ds remaining') as bar: + with Bar('Processing viewing data', max=viewing_count, suffix='%(percent).1f%% - %(eta)ds remaining', check_tty=False) as bar: viewings_by_year = VCinemaUtils.filter_viewings(viewings, 'year', bar) year_count = len(viewings_by_year) - with Bar('Generating table', max=year_count, suffix='%(percent).1f%% - %(eta)ds remaining') as bar: + with Bar('Generating table', max=year_count, suffix='%(percent).1f%% - %(eta)ds remaining', check_tty=False) as bar: film_by_year_table = build_table(viewings_by_year, bar) # Page ID of https://wiki.jacknet.io/books/vcinema/page/films-by-release-year