159 lines
5.4 KiB
Python
159 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
from pathlib import Path
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
import urllib.request
|
|
import argparse
|
|
import datetime
|
|
import json
|
|
|
|
tplenv = Environment(loader=FileSystemLoader('.'))
|
|
codetemplate = tplenv.get_template('classtpl.kt')
|
|
|
|
|
|
def prepare_directory_structure(location, assetlocation):
|
|
Path(location).mkdir(parents=True, exist_ok=True)
|
|
Path(assetlocation).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
def download_json(dataurl):
|
|
return json.loads(urllib.request.urlopen(dataurl).read())
|
|
|
|
|
|
def find_selected_edition(data):
|
|
selected = None
|
|
for edition in data['editions']:
|
|
if edition['uuid'] == data['featured']:
|
|
selected = edition
|
|
if selected is None:
|
|
selected = data['editions'][0]
|
|
return selected
|
|
|
|
|
|
def find_media(data):
|
|
resultset = set()
|
|
selected = find_selected_edition(data)
|
|
for event in selected['events']:
|
|
picture = event['picture']
|
|
if picture['uuid'] is None:
|
|
continue
|
|
resultset.add(picture['pt'])
|
|
resultset.add(picture['en'])
|
|
resultset.add(picture['es'])
|
|
for image in [
|
|
selected['banner_before'], selected['banner_during'], selected['banner_after'],
|
|
selected['map_hotel'], selected['map_dealers']
|
|
]:
|
|
if image['uuid'] is None:
|
|
continue
|
|
resultset.add(image['pt'])
|
|
resultset.add(image['en'])
|
|
resultset.add(image['es'])
|
|
return resultset
|
|
|
|
|
|
def download_image(url):
|
|
return urllib.request.urlopen(url).read()
|
|
|
|
|
|
def download_image_cached(base, target, url):
|
|
if not Path(target).joinpath(url.strip('/')).exists():
|
|
Path(target).joinpath(url.strip('/')).parent.mkdir(parents=True, exist_ok=True)
|
|
Path(target).joinpath(url.strip('/')).write_bytes(download_image(str(base)+str(url)))
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(description='Build an Android convention app Kotlin class.')
|
|
parser.add_argument('location', help="Deploy path for code")
|
|
parser.add_argument('assetlocation', help="Deploy path for assets")
|
|
parser.add_argument('appid', help="Android Application ID")
|
|
parser.add_argument('classname', help="Android class name")
|
|
parser.add_argument('dataurl', help="JSON data URL")
|
|
parser.add_argument('mediaurl', help="Media root URL")
|
|
return parser.parse_args()
|
|
|
|
|
|
def escape_string_into_kotlin(st):
|
|
return st.replace("\\", "\\\\").replace('"', '\\"').replace("'", "\\'").replace('\n', '\\n').replace('\t', '\\t').replace('\r', '\\r')
|
|
|
|
|
|
def set_string_into_dict(d, s):
|
|
if s['uuid'] is not None:
|
|
d[s['uuid']] = s
|
|
|
|
|
|
def get_strings(dt):
|
|
d = dict()
|
|
set_string_into_dict(d, dt['name'])
|
|
set_string_into_dict(d, dt['statute'])
|
|
for sm in dt['social_medias']:
|
|
set_string_into_dict(d, sm['name'])
|
|
set_string_into_dict(d, sm['url'])
|
|
ed = find_selected_edition(dt)
|
|
set_string_into_dict(d, ed['theme'])
|
|
for plc in ed['places']:
|
|
set_string_into_dict(d, plc['label'])
|
|
for tg in ed['tags']:
|
|
set_string_into_dict(d, tg['label'])
|
|
for rt in ed['registration_tiers']:
|
|
set_string_into_dict(d, rt['tier'])
|
|
for rb in rt['benefits']:
|
|
set_string_into_dict(d, rb)
|
|
for ev in ed['events']:
|
|
set_string_into_dict(d, ev['title'])
|
|
set_string_into_dict(d, ev['subtitle'])
|
|
set_string_into_dict(d, ev['description'])
|
|
return d
|
|
|
|
|
|
def get_files(dt):
|
|
d = dict()
|
|
ed = find_selected_edition(dt)
|
|
set_string_into_dict(d, ed['banner_before'])
|
|
set_string_into_dict(d, ed['banner_during'])
|
|
set_string_into_dict(d, ed['banner_after'])
|
|
set_string_into_dict(d, ed['map_hotel'])
|
|
set_string_into_dict(d, ed['map_dealers'])
|
|
for ev in ed['events']:
|
|
set_string_into_dict(d, ev['picture'])
|
|
return d
|
|
|
|
|
|
def main():
|
|
parsed = parse_args()
|
|
prepare_directory_structure(parsed.location, parsed.assetlocation)
|
|
data = download_json(parsed.dataurl)
|
|
medias = find_media(data)
|
|
[download_image_cached(parsed.mediaurl, parsed.assetlocation, media) for media in medias]
|
|
edition = find_selected_edition(data)
|
|
strings = get_strings(data)
|
|
files = get_files(data)
|
|
minevdatetime = min([datetime.datetime.fromisoformat(i['time_start']['formatted']) for i in edition['events']])
|
|
minevdate = datetime.date(minevdatetime.year, minevdatetime.month, minevdatetime.day)
|
|
maxevdatetime = max([datetime.datetime.fromisoformat(i['time_start']['formatted']) for i in edition['events']])
|
|
maxevdate = datetime.date(maxevdatetime.year, maxevdatetime.month, maxevdatetime.day)
|
|
evdates = []
|
|
evdatecursor = minevdate
|
|
while evdatecursor <= maxevdate:
|
|
evdates.append(evdatecursor)
|
|
evdatecursor = evdatecursor + datetime.timedelta(days=1)
|
|
Path(parsed.location).joinpath('assets', str(parsed.classname)+'.json').write_text(json.dumps(data))
|
|
Path(parsed.location).joinpath(str(parsed.classname)+'.kt').write_text(codetemplate.render(
|
|
escape_string_into_kotlin=escape_string_into_kotlin,
|
|
convention=data,
|
|
edition=edition,
|
|
strings=strings,
|
|
files=files,
|
|
minevdatetime=minevdatetime,
|
|
maxevdatetime=maxevdatetime,
|
|
evdates=evdates,
|
|
map=map,
|
|
list=list,
|
|
str=str,
|
|
tzexp=('1' if edition['timezone']['hr'] >= 0 else '-1')+'*(('+str(abs(edition['timezone']['hr']))+'*3600)+('+str(abs(edition['timezone']['mn']))+'*60))*1000',
|
|
classname=parsed.classname,
|
|
appid=parsed.appid
|
|
))
|