from urllib import request
import csv
import jinja2

# pad = "https://pad.xpub.nl/p/XPUB-promo-strategies.addresses/export/txt"
# addresses = request.urlopen(pad).read().decode()
# print(addresses)
# with open("addresses.csv", "w") as out:
# 	out.write(addresses)
# 	print("saved: addresses.csv")

db = {}

with open("addresses.csv", newline="\n") as csvfile:
	addresses = csv.reader(csvfile, delimiter=',', quotechar='"')
	print("opened: addresses.csv")
	for x, entry in enumerate(addresses):
		friends = entry[0].strip()
		school = entry[1].strip()
		contactperson = entry[2].strip()
		street = entry[3].strip()
		postcode = entry[4].strip()
		city = entry[5].strip()
		country = entry[6].strip()

		print("added:", school)

		db[x] = {}
		db[x]["friends"] = friends
		db[x]["name"] = school
		db[x]["contactperson"] = contactperson
		db[x]["street"] = street
		db[x]["postcode"] = postcode
		db[x]["city"] = city
		db[x]["country"] = country

env = jinja2.Environment(loader=jinja2.FileSystemLoader("."))
template = env.get_template("template.html")
html = template.render(db=db)

output = open("addresses.html", "w")
output.write(html)
output.close()
print("saved: addresses.html (open it with a local server running!)")