rename file

This commit is contained in:
Sarah 2022-03-24 20:32:07 +00:00
parent 1067a193cf
commit 586ccfb997
1 changed files with 40 additions and 48 deletions

View File

@ -1,81 +1,69 @@
from bookstack import Bookstack
from vcinema_utils import VCinemaUtils
import argparse import argparse
import base64 import base64
from collections import Counter, OrderedDict from collections import Counter, OrderedDict
import csv
import os import os
from progress.bar import Bar from progress.bar import IncrementalBar
from wand.image import Image from wand.image import Image
import worldmap import worldmap
import warnings import warnings
warnings.filterwarnings("ignore") warnings.filterwarnings("ignore")
from bookstack import Bookstack def get_flags_dict():
from vcinema_utils import VCinemaUtils 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
FLAGS = { def build_table(films_by_country, bar=None):
"Australia": "🇦🇺", films_by_country_sorted = OrderedDict(sorted(films_by_country.items(), key=lambda t: t[0]))
"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": "⬅️🇩🇪",
}
flags = get_flags_dict()
def build_table(fby, bar):
fby_sorted = OrderedDict(sorted(fby.items(), key=lambda t: t[0]))
table = "<table>" table = "<table>"
table += "<thead><tr><th>Country</th><th>Films</th></tr>" table += "<thead><tr><th>Country</th><th>Films</th></tr>"
table += "<tbody>" table += "<tbody>"
for country in fby_sorted.keys(): for country in films_by_country_sorted.keys():
row = "<tr>" row = "<tr>"
row += "<td>" row += "<td>"
row += "{} {}".format(str(country), FLAGS[country]) row += "{} {}".format(str(country), flags[country] if country in flags.keys() else "")
row += "</td>" row += "</td>"
row += "<td>" row += "<td>"
row += "<br>".join(["<a href=\"{}\">{}</a>".format(film['imdb_id'], film['title']) for film in fby_sorted[country]]) 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 += "</td>"
row += "</tr>" row += "</tr>"
table += row table += row
bar.next() if bar is not None:
bar.next()
table += "</tbody>" table += "</tbody>"
table += "</table>" table += "</table>"
if bar is not None:
bar.finish()
return table return table
@ -87,7 +75,7 @@ def draw_map(counter, file_name="vcinema_map.svg"):
opacity = [0.5 + (x / (float(max_count))/2.0) for x in 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) worldmap.plot(countries, cmap=["#49E20E"], opacity=opacity, filename=file_name, verbose=False)
with Image(filename=file_name, width=1000, height=655) as i: with Image(filename=file_name, width=1000, height=655) as i:
png_data = i.make_blob("png") png_data = i.make_blob("png")
@ -109,28 +97,32 @@ def update_page(map_png, table, wiki_url, token_id, token_secret):
Bookstack.update_page(wiki_url, token_id, token_secret, page_id, html=page_contents) Bookstack.update_page(wiki_url, token_id, token_secret, page_id, html=page_contents)
def update_films_by_country_page(token_id, token_secret): def update_films_by_country(token_id, token_secret):
print("Retrieving VCinema viewings") print("Retrieving VCinema viewings")
viewings = VCinemaUtils.get_vcinema_viewings(token_id, token_secret) viewings = VCinemaUtils.get_vcinema_viewings(token_id, token_secret)
viewing_count = len(viewings) viewing_count = len(viewings)
with Bar('Retrieving movie data', max=viewing_count, suffix='%(percent).1f%% - %(eta)ds remaining', check_tty=False) as bar: 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) 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: 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) viewings_by_country = VCinemaUtils.filter_viewings(viewings, 'countries', bar)
country_count = len(viewings_by_country.keys()) 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: 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) film_by_country_table = build_table(viewings_by_country, bar)
print("Drawing map") print("Drawing map")
country_counter = Counter(viewings_by_country) country_counter = Counter(viewings_by_country)
png_data = draw_map(country_counter) png_data = draw_map(country_counter)
print("Updating page")
update_page(png_data, film_by_country_table, VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret) update_page(png_data, film_by_country_table, VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret)
print("Done!")
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Update page displaying VCinema films sorted by country.') parser = argparse.ArgumentParser(description='Update page displaying VCinema films sorted by country.')
@ -139,4 +131,4 @@ if __name__ == '__main__':
args = parser.parse_args() args = parser.parse_args()
update_films_by_country_page(args.token_id, args.token_secret) update_films_by_country(args.token_id, args.token_secret)