vcinema/update_films_by_country.py

135 lines
3.8 KiB
Python
Raw Normal View History

2022-03-24 20:32:07 +00:00
from bookstack import Bookstack
from vcinema_utils import VCinemaUtils
import argparse
import base64
from collections import Counter, OrderedDict
2022-03-24 20:32:07 +00:00
import csv
import os
2022-03-24 20:32:07 +00:00
from progress.bar import IncrementalBar
from wand.image import Image
import worldmap
2022-03-09 20:47:35 +00:00
import warnings
warnings.filterwarnings("ignore")
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-24 20:32:07 +00:00
return flags
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()
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():
row = "<tr>"
row += "<td>"
2022-03-24 20:32:07 +00:00
row += "{} {}".format(str(country), flags[country] if country in flags.keys() else "")
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)
row += "</td>"
row += "</tr>"
table += row
2022-03-24 20:32:07 +00:00
if bar is not None:
bar.next()
table += "</tbody>"
table += "</table>"
2022-03-24 20:32:07 +00:00
if bar is not None:
bar.finish()
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-24 20:32:07 +00:00
worldmap.plot(countries, cmap=["#49E20E"], 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 = "<img src=\"data:image/png;base64,{}\" />".format(encoded)
page_contents = image_html + table
Bookstack.update_page(wiki_url, token_id, token_secret, page_id, html=page_contents)
2022-03-24 20:32:07 +00:00
def update_films_by_country(token_id, token_secret):
print("Retrieving VCinema viewings")
viewings = VCinemaUtils.get_vcinema_viewings(token_id, token_secret)
viewing_count = len(viewings)
2022-03-24 20:32:07 +00:00
with IncrementalBar('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)
2022-03-24 20:32:07 +00:00
with IncrementalBar('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())
2022-03-24 20:32:07 +00:00
with IncrementalBar('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)
2022-03-09 20:47:35 +00:00
print("Drawing map")
country_counter = Counter(viewings_by_country)
png_data = draw_map(country_counter)
2022-03-24 20:32:07 +00:00
print("Updating page")
update_page(png_data, film_by_country_table, VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret)
2022-03-24 20:32:07 +00:00
print("Done!")
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()
2022-03-24 20:32:07 +00:00
update_films_by_country(args.token_id, args.token_secret)