#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

import os

os.system("rm -rf out")
os.system("mkdir -p out")

from pathlib import Path
import urllib.request
import json
import re

cfg = json.loads(Path('cfg.json').read_text())
project = cfg['project']

mediafinder = re.compile(r'''"/media/([^"]*)"''')

def check_image_(file):
   os.system("wget -N https://events.furmeet.app/media/{0} -O out/{0}".format(file))

def check_image(file):
   os.system("wget -N https://events.furmeet.app{0} -O out/{1}".format(file, file.split('/')[-1]))

def download_json(dataurl):
    return json.loads(urllib.request.urlopen(dataurl).read())

minified = download_json("https://events.furmeet.app/api/conventions/{0}/minified.json".format(project))

minified['editions'] = [edition for edition in minified['editions'] if edition['uuid']==minified['featured']]

filteredjson = json.dumps(minified)

Path("out", "minified.json").write_text(filteredjson)
Path("out", "config.json").write_text(json.dumps(cfg, indent=1))

#medias = sorted(list(set(mediafinder.findall(filteredjson))))

medias = list()
medias.append(minified['default_banner']['pt'])
medias.append(minified['default_banner']['en'])
medias.append(minified['default_banner']['es'])

medias = list(set(medias))

for media in medias:
    check_image(media)

