2022-03-24 20:32:07 +00:00
|
|
|
from vcinema_utils import VCinemaUtils
|
|
|
|
|
2022-03-09 20:15:21 +00:00
|
|
|
import base64
|
|
|
|
from collections import Counter, OrderedDict
|
2022-03-24 20:32:07 +00:00
|
|
|
import csv
|
2022-03-09 20:15:21 +00:00
|
|
|
import os
|
2022-03-24 20:32:07 +00:00
|
|
|
from progress.bar import IncrementalBar
|
2022-03-09 20:15:21 +00:00
|
|
|
from wand.image import Image
|
|
|
|
import worldmap
|
2022-03-09 20:47:35 +00:00
|
|
|
import warnings
|
|
|
|
warnings.filterwarnings("ignore")
|
2022-03-09 20:15:21 +00:00
|
|
|
|
2022-03-25 22:17:11 +00:00
|
|
|
# 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 = "<img src=\"data:image/png;base64,{}\" />".format(encoded)
|
|
|
|
|
|
|
|
page = image_html + film_by_country_table
|
|
|
|
|
|
|
|
return page
|
|
|
|
|
2022-03-09 20:15:21 +00:00
|
|
|
|
2022-03-24 20:32:07 +00:00
|
|
|
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]
|
2022-03-09 20:15:21 +00:00
|
|
|
|
2022-03-24 20:32:07 +00:00
|
|
|
return flags
|
2022-03-09 20:15:21 +00:00
|
|
|
|
2022-03-24 20:32:07 +00:00
|
|
|
|
|
|
|
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()
|
2022-03-09 20:15:21 +00:00
|
|
|
|
|
|
|
table = "<table>"
|
|
|
|
|
|
|
|
table += "<thead><tr><th>Country</th><th>Films</th></tr>"
|
|
|
|
table += "<tbody>"
|
|
|
|
|
2022-03-24 20:32:07 +00:00
|
|
|
for country in films_by_country_sorted.keys():
|
2022-03-09 20:15:21 +00:00
|
|
|
row = "<tr>"
|
|
|
|
|
|
|
|
row += "<td>"
|
2022-03-24 20:32:07 +00:00
|
|
|
row += "{} {}".format(str(country), flags[country] if country in flags.keys() else "")
|
2022-03-09 20:15:21 +00:00
|
|
|
row += "</td>"
|
|
|
|
|
|
|
|
row += "<td>"
|
|
|
|
|
2022-03-24 20:32:07 +00:00
|
|
|
film_links = ["<a href=\"https://www.imdb.com/title/tt{}/\">{}</a>".format(film['imdb_id'], film['title'])
|
|
|
|
for film in films_by_country_sorted[country]]
|
|
|
|
|
|
|
|
row += "<br>".join(film_links)
|
2022-03-09 20:15:21 +00:00
|
|
|
|
|
|
|
row += "</td>"
|
|
|
|
row += "</tr>"
|
|
|
|
|
|
|
|
table += row
|
|
|
|
|
2022-03-24 20:32:07 +00:00
|
|
|
if bar is not None:
|
|
|
|
bar.next()
|
2022-03-09 20:15:21 +00:00
|
|
|
|
|
|
|
table += "</tbody>"
|
|
|
|
table += "</table>"
|
|
|
|
|
2022-03-24 20:32:07 +00:00
|
|
|
if bar is not None:
|
|
|
|
bar.finish()
|
|
|
|
|
2022-03-09 20:15:21 +00:00
|
|
|
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]
|
|
|
|
|
2022-03-25 22:17:11 +00:00
|
|
|
worldmap.plot(countries, cmap=["#FF4000"], opacity=opacity, filename=file_name, verbose=False)
|
2022-03-09 20:15:21 +00:00
|
|
|
|
|
|
|
with Image(filename=file_name, width=1000, height=655) as i:
|
|
|
|
png_data = i.make_blob("png")
|
|
|
|
|
|
|
|
os.remove(file_name)
|
|
|
|
|
|
|
|
return png_data
|