vcinema/generate_map_timelapse.py

74 lines
2.6 KiB
Python
Raw Permalink Normal View History

2022-04-22 22:01:09 +01:00
from wiki_pages import FilmsByCountry
from vcinema_utils import VCinemaUtils
import argparse
2022-04-22 22:13:21 +01:00
from collections import OrderedDict
2022-04-22 23:07:09 +01:00
import imageio
2022-04-22 22:01:09 +01:00
from progress.bar import IncrementalBar
2022-04-22 23:47:29 +01:00
from pygifsicle import optimize
2022-04-24 13:54:12 +01:00
from PIL import Image, ImageFont, ImageDraw, ImageFont
import io
2022-04-22 22:01:09 +01:00
2022-04-24 17:40:10 +01:00
def generate_map_timelapse(token_id, token_secret, filename):
2022-04-22 22:01:09 +01:00
print("Getting viewings")
viewings = VCinemaUtils.get_vcinema_viewings(token_id, token_secret, combine_repeat_viewings=False)
viewing_count = len(viewings)
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)
date_viewings = VCinemaUtils.filter_viewings(viewings, "date_watched")
2022-04-22 22:13:21 +01:00
date_viewings = OrderedDict(sorted(date_viewings.items(), key=lambda t: t[0]))
running_country_counts = {}
2022-04-22 23:01:11 +01:00
print(len(date_viewings.keys()))
2022-04-22 22:13:21 +01:00
2022-04-24 17:40:10 +01:00
with imageio.get_writer(filename, mode='I', duration=0.1) as writer:
2022-04-22 23:01:11 +01:00
for date, viewings in date_viewings.items():
date_viewings_countries = VCinemaUtils.filter_viewings(viewings, "countries")
2022-04-22 22:01:09 +01:00
2022-04-22 23:01:11 +01:00
for country in date_viewings_countries:
if country in running_country_counts.keys():
running_country_counts[country] += date_viewings_countries[country]
else:
running_country_counts[country] = date_viewings_countries[country]
2022-04-22 22:01:09 +01:00
2022-04-24 13:54:12 +01:00
map = FilmsByCountry.draw_map(running_country_counts, file_name="map-{}.svg".format(date))
stream = io.BytesIO(map)
img = Image.open(stream)
map_editable = ImageDraw.Draw(img)
2022-04-29 16:35:42 +01:00
# macos font path
font = ImageFont.truetype("/System/Library/Fonts/Supplemental/Arial.ttf", 48)
# image is 655 high
map_editable.text((2, 605), "{}".format(date), (255, 64, 0), font=font)
2022-04-24 13:54:12 +01:00
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
image = imageio.imread(img_byte_arr)
2022-04-22 23:01:11 +01:00
writer.append_data(image)
2022-04-22 23:47:29 +01:00
print("optimizing")
2022-04-24 17:40:10 +01:00
optimize(filename)
2022-04-22 23:47:29 +01:00
2022-04-22 23:01:11 +01:00
print("done")
2022-04-22 22:01:09 +01:00
if __name__ == '__main__':
2022-04-24 17:40:10 +01:00
parser = argparse.ArgumentParser(description='Create timelapse gif of vcinema countries')
2022-04-22 22:01:09 +01:00
parser.add_argument('token_id', help='API token ID.')
parser.add_argument('token_secret', help='API token secret.')
2022-04-24 17:40:10 +01:00
parser.add_argument('filename', help='Name of output gif')
2022-04-22 22:01:09 +01:00
args = parser.parse_args()
2022-04-24 17:40:10 +01:00
generate_map_timelapse(args.token_id, args.token_secret, args.filename)