vcinema/wiki_pages/FilmsByCountry.py

105 lines
2.8 KiB
Python
Raw Permalink Normal View History

import base64
from collections import Counter, OrderedDict
2022-03-24 20:32:07 +00:00
import csv
import os
2022-09-09 22:42:52 +01:00
import pyvips
import worldmap
2022-03-09 20:47:35 +00:00
import warnings
2022-04-18 23:04:06 +01:00
from bookstack import Bookstack
from vcinema_utils import VCinemaUtils
2022-03-09 20:47:35 +00:00
warnings.filterwarnings("ignore")
2022-03-25 22:17:11 +00:00
# Page ID of https://wiki.jacknet.io/books/vcinema/page/films-by-country
2022-04-15 17:29:19 +01:00
PAGE_ID = 34
2022-03-25 22:17:11 +00:00
def get_films_by_country(viewings):
2022-12-19 22:26:38 +00:00
viewings_filtered_by_country = VCinemaUtils.filter_films(viewings, "countries")
2022-03-25 22:17:11 +00:00
2022-04-08 23:58:02 +01:00
if "Czechia" in viewings_filtered_by_country.keys():
viewings_filtered_by_country["Czech Republic"] = viewings_filtered_by_country["Czechia"]
viewings_filtered_by_country.pop("Czechia")
2022-03-25 22:17:11 +00:00
return viewings_filtered_by_country
2022-04-16 09:43:51 +01:00
def update_page(token_id, token_secret, films_by_country):
page = build_page(films_by_country)
Bookstack.update_page(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, PAGE_ID, markdown=page)
2022-03-25 22:17:11 +00:00
def build_page(films_by_country):
2022-04-03 22:27:49 +01:00
table = build_table(films_by_country)
2022-03-25 22:17:11 +00:00
country_counter = Counter(films_by_country)
png_data = draw_map(country_counter)
encoded = base64.b64encode(png_data).decode("utf-8")
2022-04-03 16:22:27 +01:00
image = "![](data:image/png;base64,{})".format(encoded)
2022-04-03 22:27:49 +01:00
page = image + "\n" + table
2022-03-25 22:17:11 +00:00
return page
2022-03-24 20:32:07 +00:00
def get_flags_dict():
flags = {}
2022-04-03 20:34:53 +01:00
with open('country-flags.csv', newline='') as f:
2022-03-24 20:32:07 +00:00
reader = csv.reader(f, quotechar="\"")
next(reader, None) # skip the headers
for row in reader:
flags[row[0]] = row[1]
2022-03-24 20:32:07 +00:00
return flags
2022-03-24 20:32:07 +00:00
2022-04-03 22:27:49 +01:00
def build_table(films_by_country):
2022-03-24 20:32:07 +00:00
films_by_country_sorted = OrderedDict(sorted(films_by_country.items(), key=lambda t: t[0]))
flags = get_flags_dict()
2022-04-03 16:22:27 +01:00
table = "| Country | Films |\n| - | - |"
2022-04-03 22:27:49 +01:00
for country, films in films_by_country_sorted.items():
2022-04-03 16:22:27 +01:00
table += "\n"
2022-04-03 22:27:49 +01:00
2022-04-08 22:01:58 +01:00
row_data = []
2022-04-03 22:27:49 +01:00
2022-04-08 22:01:58 +01:00
country_label = country
2022-04-03 22:27:49 +01:00
if country in flags.keys():
2022-04-08 22:01:58 +01:00
country_label += " "
country_label += flags[country]
2022-04-03 22:27:49 +01:00
2022-04-08 22:01:58 +01:00
row_data.append(country_label)
row_data.append(VCinemaUtils.get_film_list(films))
table += " | ".join(row_data)
2022-03-24 20:32:07 +00:00
return table
2022-04-03 22:27:49 +01:00
def draw_map(films_by_country, file_name="vcinema_map.svg"):
films_by_country['Germany'] += films_by_country['West Germany']
del films_by_country['West Germany']
2022-04-03 22:27:49 +01:00
counter = Counter(films_by_country)
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]
2022-03-25 22:17:11 +00:00
worldmap.plot(countries, cmap=["#FF4000"], opacity=opacity, filename=file_name, verbose=False)
2022-09-09 21:31:40 +01:00
image = pyvips.Image.new_from_file(file_name)
2022-09-09 22:42:52 +01:00
image = image.thumbnail_image(1000, crop=pyvips.Interesting.ALL)
2022-09-09 21:31:40 +01:00
2022-09-09 22:42:52 +01:00
png_data = image.write_to_buffer(".png")
os.remove(file_name)
2022-09-09 22:42:52 +01:00
return png_data