from vcinema_utils import VCinemaUtils
import base64
from collections import Counter, OrderedDict
import csv
import os
from progress.bar import IncrementalBar
from wand.image import Image
import worldmap
import warnings
warnings.filterwarnings("ignore")
# Page ID of https://wiki.jacknet.io/books/vcinema/page/films-by-country
FILMS_BY_COUNTRY_PAGE_ID = 34
def get_films_by_country(viewings):
viewings_filtered_by_country = VCinemaUtils.filter_viewings(viewings, "countries")
return viewings_filtered_by_country
def build_page(films_by_country):
country_count = len(films_by_country.keys())
with IncrementalBar('Generating table', max=country_count, suffix='%(percent).1f%% - %(eta)ds remaining', check_tty=False) as bar:
film_by_country_table = build_table(films_by_country, bar)
print("Drawing map")
country_counter = Counter(films_by_country)
png_data = draw_map(country_counter)
encoded = base64.b64encode(png_data).decode("utf-8")
image_html = "".format(encoded)
page = image_html + film_by_country_table
return page
def get_flags_dict():
flags = {}
with open('country_flags.csv', newline='') as f:
reader = csv.reader(f, quotechar="\"")
next(reader, None) # skip the headers
for row in reader:
flags[row[0]] = row[1]
return flags
def build_table(films_by_country, bar=None):
films_by_country_sorted = OrderedDict(sorted(films_by_country.items(), key=lambda t: t[0]))
flags = get_flags_dict()
table = "
Country | Films |
---|---|
" row += "{} {}".format(str(country), flags[country] if country in flags.keys() else "") row += " | " row += ""
film_links = ["{}".format(film['imdb_id'], film['title'])
for film in films_by_country_sorted[country]]
row += " ".join(film_links) row += " | "
row += "