def run(): print("Creating contestants/index") html = templates.get("contestants/index") html = templates.initial_replace(html, 2) def keyfn(item): return (item[1]["G"], item[1]["S"], item[1]["B"], item[1]["H"], sum(item[1].values())) sorted_items = sorted(s_db_h.items(), reverse=True, key=keyfn) tablehtml = "" for contestant, history in sorted_items: rowhtml = templates.get("contestants/index_row") \ .replace("__NAME__", s_db_c[contestant][0]["name"]) \ .replace("__USER_ID__", contestant) \ .replace("__GOLD__", str(history["G"])) \ .replace("__SILVER__", str(history["S"])) \ .replace("__BRONZE__", str(history["B"])) \ .replace("__HONOURABLE_MENTION__", str(history["H"])) \ .replace("__PARTICIPATIONS__", str(sum(history.values()))) tablehtml += rowhtml html = html.replace("__TABLE__", tablehtml) html = templates.final_replace(html, "..") util.writefile("../dest/contestants/index.html", html)
def run(): print("Creating timeline/index") html = templates.get("timeline/index") html = templates.initial_replace(html, 1) tablehtml = "" upcominghtml = "" for row in t_db: rowhtml = templates.get("timeline/index_row") rowhtml = rowhtml.replace("__NUMBER__", row["number"]) rowhtml = rowhtml.replace("__YEAR__", row["year"]) rowhtml = rowhtml.replace("__DATE__", row["date"]) rowhtml = rowhtml.replace("__CODE__", row["code"]) rowhtml = rowhtml.replace("__CITY__", row["city"]) rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[row["code"]]) rowhtml = rowhtml.replace("__P_COUNTRY__", row["p_country"]) rowhtml = rowhtml.replace("__P_STUDENT__", row["p_student"]) if "code2" in row: rowhtml = rowhtml.replace("__CODE2__", row["code2"]) rowhtml = rowhtml.replace("__COUNTRY2__", code_to_country[row["code2"]]) rowhtml = rowhtml.replace("__CODE2_STYLE__", "") else: rowhtml = rowhtml.replace("__CODE2_STYLE__", "display: none;") rowhtml = rowhtml.replace("__CODE2__", ".") # Google crawler fix if int(row["year"]) < int(config.previous_year) + 3: # Reverse list tablehtml = rowhtml + tablehtml else: upcominghtml = rowhtml + upcominghtml html = html.replace("__TABLE__", tablehtml) html = html.replace("__UPCOMING__", upcominghtml) html = templates.final_replace(html, "..") util.writefile("../timeline/index.html", html)
def run(): print("Creating 404") html = templates.get("404") html = templates.initial_replace(html, -1) # This can't both work local and Github :/ # but it only works on Github anyways html = templates.final_replace(html, "") util.writefile("../404.html", html)
def run(): print("Creating search") util.makedirs("../dest/search") util.copyfile("templates/search/search.js", "../dest/search/search.js") html = templates.get("search/index") html = templates.initial_replace(html, 3) html = templates.final_replace(html, "..") util.writefile("../dest/search/index.html", html)
def run(code): print("Creating countries/" + code + "/individual") html = templates.get("countries/code/individual") html = templates.initial_replace(html, 2) html = html.replace("__CODE__", code) html = html.replace("__COUNTRY__", c_db_c[code]["country"]) if code in previous_code: html = html.replace("__PREVIOUS_CODE__", previous_code[code]) html = html.replace("__PREVIOUS_CODE_STYLE__", "") else: html = html.replace("__PREVIOUS_CODE_STYLE__", "display: none;") html = html.replace("__PREVIOUS_CODE__", ".") # Google crawler fix if code in next_code: html = html.replace("__NEXT_CODE__", next_code[code]) html = html.replace("__NEXT_CODE_STYLE__", "") else: html = html.replace("__NEXT_CODE_STYLE__", "display: none;") html = html.replace("__NEXT_CODE__", ".") # Google crawler fix tablehtml = "" if code in s_db_c: yearhtml = "" lastyear = "" for studentdata in s_db_c[code]: rowhtml = templates.get("countries/code/individual_row") rowhtml = rowhtml.replace("__NAME__", studentdata["name"]) rowhtml = rowhtml.replace("__RANK__", ("≥" if studentdata["rank>="] else "") + studentdata["rank"]) rowhtml = rowhtml.replace("__YEAR__", studentdata["year"]) if studentdata["medal"] == "G": rowhtml = rowhtml.replace("__MEDAL__", templates.get("countries/code/individual_gold")) elif studentdata["medal"] == "S": rowhtml = rowhtml.replace("__MEDAL__", templates.get("countries/code/individual_silver")) elif studentdata["medal"] == "B": rowhtml = rowhtml.replace("__MEDAL__", templates.get("countries/code/individual_bronze")) elif studentdata["medal"] == "H": rowhtml = rowhtml.replace("__MEDAL__", templates.get("countries/code/individual_honourable")) else: rowhtml = rowhtml.replace("__MEDAL__", "") if lastyear == studentdata["year"]: rowhtml = rowhtml.replace("__CLASS__", "") yearhtml += rowhtml else: lastyear = studentdata["year"] # reverse ordered: tablehtml = yearhtml + tablehtml rowhtml = rowhtml.replace("__CLASS__", "doubleTopLine") yearhtml = rowhtml # Hacky way of removing first double top line: yearhtml = yearhtml.replace("doubleTopLine", "", 1) tablehtml = yearhtml + tablehtml html = html.replace("__TABLE__", tablehtml) html = templates.final_replace(html, "../..") util.writefile("../countries/" + code + "/individual.html", html)
def run(): print("Creating search") util.makedirs("../search") util.copyfile("database/countries.csv", "../search/countries.csv") util.copyfile("database/estudiantes.csv", "../search/estudiantes.csv") util.copyfile("templates/search/search.js", "../search/search.js") util.copyfile("templates/search/asciify.js", "../search/asciify.js") html = templates.get("search/index") html = templates.initial_replace(html, 3) html = templates.final_replace(html, "..") util.writefile("../search/index.html", html)
def run(year): print("Creating timeline/" + year + "/individual") html = templates.get("timeline/year/individual") html = templates.initial_replace(html, 1) yeardata = t_db_y[year] html = html.replace("__YEAR__", year) html = html.replace("__NUMBER__", yeardata["number"]) html = html.replace("__ORDINAL__", util.ordinal(yeardata["number"])) if year in previous_year: html = html.replace("__PREVIOUS_YEAR__", previous_year[year]) html = html.replace("__PREVIOUS_YEAR_STYLE__", "") else: html = html.replace("__PREVIOUS_YEAR_STYLE__", "display: none;") html = html.replace("__PREVIOUS_YEAR__", ".") # Google crawler fix if year in next_year: html = html.replace("__NEXT_YEAR__", next_year[year]) html = html.replace("__NEXT_YEAR_STYLE__", "") else: html = html.replace("__NEXT_YEAR_STYLE__", "display: none;") html = html.replace("__NEXT_YEAR__", ".") # Google crawler fix tablehtml = "" if year in s_db_y: for row in s_db_y[year]: rowhtml = templates.get("timeline/year/individual_row") if row["code"] == "": rowhtml = rowhtml.replace("__CODE__", "TUR") # Yup, this is my hack rowhtml = rowhtml.replace("__COUNTRY__", "") else: rowhtml = rowhtml.replace("__CODE__", row["code"]) rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[row["code"]]) rowhtml = rowhtml.replace("__NAME__", row["name"]) rowhtml = rowhtml.replace("__RANK__", ("≥" if row["rank>="] else "") + row["rank"]) if row["medal"] == "G": rowhtml = rowhtml.replace("__MEDAL__", templates.get("timeline/year/individual_gold")) elif row["medal"] == "S": rowhtml = rowhtml.replace("__MEDAL__", templates.get("timeline/year/individual_silver")) elif row["medal"] == "B": rowhtml = rowhtml.replace("__MEDAL__", templates.get("timeline/year/individual_bronze")) elif row["medal"] == "H": rowhtml = rowhtml.replace("__MEDAL__", templates.get("timeline/year/individual_honourable")) else: rowhtml = rowhtml.replace("__MEDAL__", "") tablehtml += rowhtml html = html.replace("__TABLE__", tablehtml) html = templates.final_replace(html, "../..") util.writefile("../timeline/" + year + "/individual.html", html)
def run(): print("Creating countries/index") html = templates.get("countries/index") html = templates.initial_replace(html, 2) tablehtml = "" for row in c_db: rowhtml = templates.get("countries/index_row") rowhtml = rowhtml.replace("__CODE__", row["code"]) rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[row["code"]]) if row["website"] != "": rowhtml = rowhtml.replace("__NATIONAL_SITE__", row["website"]) if len(row["website"]) < 50: rowhtml = rowhtml.replace("__NATIONAL_SITE_TEXT__", row["website"]) else: rowhtml = rowhtml.replace("__NATIONAL_SITE_TEXT__", row["website"][0:35] + "...") rowhtml = rowhtml.replace("__NATIONAL_SITE_STYLE__", "") else: rowhtml = rowhtml.replace("__NATIONAL_SITE_STYLE__", "display: none;") if row["code"] in t_db_c: hosts = "" flag = False for year in t_db_c[row["code"]]: if flag: hosts += ", " hosts += templates.get("countries/index_hostyear").replace( "__YEAR__", year["year"]) flag = True rowhtml = rowhtml.replace("__HOSTS__", hosts) else: rowhtml = rowhtml.replace("__HOSTS__", "") rowhtml = rowhtml.replace("__CLASS__", "tr-former" if row["former"] else "") tablehtml += rowhtml html = html.replace("__TABLE__", tablehtml) html = templates.final_replace(html, "..") util.writefile("../countries/index.html", html)
def run(): print("Creating timeline/index") html = templates.get("timeline/index") html = templates.initial_replace(html, 1) tablehtml = "" for row in t_db: rowhtml = templates.get("timeline/index_row") rowhtml = rowhtml.replace("__NUMBER__", row["number"]) rowhtml = rowhtml.replace("__MONTH__", row["month"]) rowhtml = rowhtml.replace("__CONTEST_NAME__", row["name"]) rowhtml = rowhtml.replace("__DATE__", row["date"]) rowhtml = rowhtml.replace("__P_STUDENT__", row["p_student"]) tablehtml = rowhtml + tablehtml html = html.replace("__TABLE__", tablehtml) html = templates.final_replace(html, "..") util.writefile("../dest/timeline/index.html", html)
def run(): print("Creating timeline/index") html = templates.get("timeline/index") html = templates.initial_replace(html, 1) tablehtml = "" upcominghtml = "" upcoming_row_ctr = 0 for row in t_db: rowhtml = templates.get("timeline/index_row") rowhtml = rowhtml.replace("__NUMBER__", row["number"]) rowhtml = rowhtml.replace("__YEAR__", row["year"]) rowhtml = rowhtml.replace("__DATE__", row["date"]) rowhtml = rowhtml.replace("__CODE__", row["code"]) rowhtml = rowhtml.replace("__CITY__", row["city"]) rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[row["code"]]) rowhtml = rowhtml.replace("__P_COUNTRY__", row["p_country"]) rowhtml = rowhtml.replace("__P_STUDENT__", row["p_student"]) if "code2" in row: rowhtml = rowhtml.replace("__CODE2__", row["code2"]) rowhtml = rowhtml.replace("__COUNTRY2__", code_to_country[row["code2"]]) rowhtml = rowhtml.replace("__CODE2_STYLE__", "") else: rowhtml = rowhtml.replace("__CODE2_STYLE__", "display: none;") rowhtml = rowhtml.replace("__CODE2__", ".") # Google crawler fix if int(row["year"]) <= int(config.next_year) + 2: # Reverse list tablehtml = rowhtml + tablehtml else: upcominghtml = rowhtml + upcominghtml upcoming_row_ctr += 1 if int(row["year"]) == 2019: tablehtml = templates.get("timeline/index_row_2020") + tablehtml # Append an empty row to preserve row parity between tables for styling purposes if upcoming_row_ctr % 2: upcominghtml = "<tr style=\"display:none;\"></tr>" + upcominghtml html = html.replace("__TABLE__", tablehtml) html = html.replace("__UPCOMING__", upcominghtml) html = templates.final_replace(html, "..") util.writefile("../timeline/index.html", html)
def run(): print("Creating timeline/2020") util.makedirs("../timeline/2020") html = templates.get("timeline/2020/index") html = templates.initial_replace(html, 1) tablehtml = "" for row in database: rowhtml = templates.get("timeline/year/individual_row") rowhtml = rowhtml.replace("__CODE__", row["code"]) rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[row["code"]]) if row["website"]: link = templates.get("timeline/year/individual_student_link") link = link.replace("__LINK__", row["website"]) link = link.replace("__NAME__", row["name"]) rowhtml = rowhtml.replace("__NAME__", link) else: rowhtml = rowhtml.replace("__NAME__", row["name"]) rowhtml = rowhtml.replace("__RANK__", row["rank"]) if row["medal"] == "G": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/year/individual_gold")) elif row["medal"] == "S": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/year/individual_silver")) elif row["medal"] == "B": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/year/individual_bronze")) elif row["medal"] == "H": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/year/individual_honourable")) else: rowhtml = rowhtml.replace("__MEDAL__", "") rowhtml = rowhtml.replace("__POINTS_STYLE__", "display: none;") tablehtml += rowhtml html = html.replace("__TABLE__", tablehtml) html = templates.final_replace(html, "../..") util.writefile("../timeline/2020/index.html", html)
def run(): print("Creating countries/index") html = templates.get("countries/index") html = templates.initial_replace(html, 2) tablehtml = "" for row in c_db: rowhtml = templates.get("countries/index_row") rowhtml = rowhtml.replace("__CODE__", row["code"]) rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[row["code"]]) if row["website"] != "": rowhtml = rowhtml.replace("__NATIONAL_SITE__", row["website"]) if len(row["website"]) < 50: rowhtml = rowhtml.replace("__NATIONAL_SITE_TEXT__", row["website"]) else: rowhtml = rowhtml.replace("__NATIONAL_SITE_TEXT__", row["website"][0:35] + "...") rowhtml = rowhtml.replace("__NATIONAL_SITE_STYLE__", "") else: rowhtml = rowhtml.replace("__NATIONAL_SITE_STYLE__", "display: none;") if row["code"] in t_db_c: hosts = "" flag = False for year in t_db_c[row["code"]]: if flag: hosts += ", " hosts += templates.get("countries/index_hostyear").replace("__YEAR__", year["year"]) flag = True rowhtml = rowhtml.replace("__HOSTS__", hosts) else: rowhtml = rowhtml.replace("__HOSTS__", "") rowhtml = rowhtml.replace("__CLASS__", "tr-former" if row["former"] else "") tablehtml += rowhtml html = html.replace("__TABLE__", tablehtml) html = templates.final_replace(html, "..") util.writefile("../countries/index.html", html)
def run(user_id): print("Creating contestants/" + user_id + "/index") history = contestant_history[user_id] html = templates.get("contestants/profile") html = templates.initial_replace(html, 2) \ .replace("__NAME__", contestant_grouped[user_id][0]["name"]) \ .replace("__GOLD__", str(history["G"])) \ .replace("__SILVER__", str(history["S"])) \ .replace("__BRONZE__", str(history["B"])) \ .replace("__HONOURABLE_MENTION__", str(history["H"])) \ .replace("__PARTICIPATIONS__", str(sum(history.values()))) beg, inter, adv, modsmo, special = [""] * 5 beg_anon, inter_anon, adv_anon, modsmo_anon, special_anon = [False] * 5 for row in contestant_grouped[user_id]: rowhtml = templates.get("contestants/profile_row_" + str(len(row["scores"]))) \ .replace("__MONTH__", row["month"]) \ .replace("__CONTEST_NAME__", row["contest_name"]) \ .replace("__TOTAL_SCORE__", "" if row["is_anonymous"] else str(row["total_score"])) \ .replace("__RANK__", "" if row["is_anonymous"] else str(row["rank"])) for i, x in enumerate(row["scores"]): rowhtml = rowhtml.replace(f"__SCORE_{i+1}__", "" if row["is_anonymous"] else str(x)) if not row["is_anonymous"]: if row["medal"] == "G": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/month/individual_gold")) elif row["medal"] == "S": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/month/individual_silver")) elif row["medal"] == "B": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/month/individual_bronze")) elif row["medal"] == "H": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/month/individual_honourable")) else: rowhtml = rowhtml.replace("__MEDAL__", "") else: rowhtml = rowhtml.replace("__MEDAL__", "") if row["contest_name"] == "Beginner": beg += rowhtml beg_anon = beg_anon or row["is_anonymous"] elif row["contest_name"] == "Intermediate": inter += rowhtml inter_anon = inter_anon or row["is_anonymous"] elif row["contest_name"] == "Advanced": adv += rowhtml adv_anon = adv_anon or row["is_anonymous"] elif row["contest_name"] == "MODSMO": modsmo += rowhtml modsmo_anon = modsmo_anon or row["is_anonymous"] else: special += rowhtml special_anon = special_anon or row["is_anonymous"] header4 = templates.get("contestants/profile_table_header_4") header6 = templates.get("contestants/profile_table_header_6") header_special = templates.get("contestants/profile_table_header_special") for code, text, anon, header in (("BEG", beg, beg_anon, header4), ("INT", inter, inter_anon, header4), ("ADV", adv, adv_anon, header4), ("MODSMO", modsmo, modsmo_anon, header6), ("SPECIAL", special, special_anon, header_special)): if text: html = html.replace( "__TABLE_HEADER_" + code + "__", header_special if code == "SPECIAL" else header) html = html.replace("__TABLE_" + code + "__", text) else: html = html.replace("__TABLE_HEADER_" + code + "__", "") html = html.replace("__TABLE_" + code + "__", "<dd>No participation yet.</dd>") html = html.replace( "__ANONYMOUS_MSG_" + code + "__", "Results of anonymous participations are hidden." if anon else "") html = templates.final_replace(html, "../..") util.writefile("../dest/contestants/" + user_id + "/index.html", html)
def run(year): print("Creating timeline/" + year + "/country") html = templates.get("timeline/year/country") html = templates.initial_replace(html, 1) yeardata = t_db_y[year] html = html.replace("__YEAR__", year) html = html.replace("__NUMBER__", yeardata["number"]) html = html.replace("__ORDINAL__", util.ordinal(yeardata["number"])) if year in previous_year: html = html.replace("__PREVIOUS_YEAR__", previous_year[year]) html = html.replace("__PREVIOUS_YEAR_STYLE__", "") else: html = html.replace("__PREVIOUS_YEAR_STYLE__", "display: none;") html = html.replace("__PREVIOUS_YEAR__", ".") # Google crawler fix if year in next_year: html = html.replace("__NEXT_YEAR__", next_year[year]) html = html.replace("__NEXT_YEAR_STYLE__", "") else: html = html.replace("__NEXT_YEAR_STYLE__", "display: none;") html = html.replace("__NEXT_YEAR__", ".") # Google crawler fix medals = {} if year in s_db_y: for row in s_db_y[year]: if row["code"] == "": # Country unknown continue if row["code"] not in medals: medals[row["code"]] = { "bestrank": int(row["rank"]), "bestrank>=": "≥" if row["rank>="] else "", "gold": 0, "silver": 0, "bronze": 0, "honourable": 0 } if row["medal"] == "G": medals[row["code"]]["gold"] += 1 elif row["medal"] == "S": medals[row["code"]]["silver"] += 1 elif row["medal"] == "B": medals[row["code"]]["bronze"] += 1 elif row["medal"] == "H": medals[row["code"]]["honourable"] += 1 def keyfn(code): m = medals[code] return (m["gold"], m["silver"], m["bronze"], m["honourable"], -m["bestrank"]) sortedcodes = reversed(sorted(medals, key=keyfn)) tablehtml = "" prevcode = "" prevrank = 0 for i, code in enumerate(sortedcodes): rowhtml = templates.get("timeline/year/country_row") rowhtml = rowhtml.replace("__CODE__", code) rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[code]) if prevcode != "" and keyfn(prevcode) == keyfn(code): rowhtml = rowhtml.replace("__RANK__", prevrank) else: rowhtml = rowhtml.replace("__RANK__", str(i + 1)) prevcode = code prevrank = str(i + 1) rowhtml = rowhtml.replace("__GOLD__", str(medals[code]["gold"])) rowhtml = rowhtml.replace("__SILVER__", str(medals[code]["silver"])) rowhtml = rowhtml.replace("__BRONZE__", str(medals[code]["bronze"])) rowhtml = rowhtml.replace("__HONOURABLE__", str(medals[code]["honourable"])) rowhtml = rowhtml.replace( "__BEST_RANK__", medals[code]["bestrank>="] + str(medals[code]["bestrank"])) tablehtml += rowhtml html = html.replace("__TABLE__", tablehtml) html = templates.final_replace(html, "../..") util.writefile("../timeline/" + year + "/country.html", html)
def run(year): print("Creating timeline/" + year + "/index") html = templates.get("timeline/year/index") html = templates.initial_replace(html, 1) yeardata = t_db_y[year] html = html.replace("__YEAR__", year) html = html.replace("__NUMBER__", yeardata["number"]) html = html.replace("__ORDINAL__", util.ordinal(yeardata["number"])) html = html.replace("__DATE__", yeardata["date"]) html = html.replace("__CODE__", yeardata["code"]) html = html.replace("__COUNTRY__", code_to_country[yeardata["code"]]) if "code2" in yeardata: html = html.replace("__CODE2__", yeardata["code2"]) html = html.replace("__COUNTRY2__", code_to_country[yeardata["code2"]]) html = html.replace("__CODE2_STYLE__", "") else: html = html.replace("__CODE2_STYLE__", "display: none;") html = html.replace("__CODE2__", ".") # Google crawler fix if yeardata["city"] != "": html = html.replace("__CITY__", yeardata["city"] + ",") else: html = html.replace("__CITY__", "") if year in previous_year: html = html.replace("__PREVIOUS_YEAR__", previous_year[year]) html = html.replace("__PREVIOUS_YEAR_STYLE__", "") else: html = html.replace("__PREVIOUS_YEAR_STYLE__", "display: none;") html = html.replace("__PREVIOUS_YEAR__", ".") # Google crawler fix if year in next_year: html = html.replace("__NEXT_YEAR__", next_year[year]) html = html.replace("__NEXT_YEAR_STYLE__", "") else: html = html.replace("__NEXT_YEAR_STYLE__", "display: none;") html = html.replace("__NEXT_YEAR__", ".") # Google crawler fix if yeardata["p_student"] != "": html = html.replace("__P_STUDENT_STYLE__", "") html = html.replace("__P_STUDENT__", yeardata["p_student"]) else: html = html.replace("__P_STUDENT_STYLE__", "display: none;") if yeardata["p_country"] != "": html = html.replace("__P_COUNTRY_STYLE__", "") html = html.replace("__P_COUNTRY__", yeardata["p_country"]) else: html = html.replace("__P_COUNTRY_STYLE__", "display: none;") if yeardata["homepage"] != "": html = html.replace("__HOMEPAGE_STYLE__", "") html = html.replace("__HOMEPAGE__", yeardata["homepage"]) else: html = html.replace("__HOMEPAGE_STYLE__", "display: none;") html = html.replace("__HOMEPAGE__", ".") # Google crawler fix gold = 0 silver = 0 bronze = 0 honourable = 0 if year in s_db_y: for studentdata in s_db_y[year]: if studentdata["medal"] == "G": gold += 1 elif studentdata["medal"] == "S": silver += 1 elif studentdata["medal"] == "B": bronze += 1 elif studentdata["medal"] == "H": honourable += 1 html = html.replace("__AWARDS_STYLE__", "") html = html.replace("__GOLD__", str(gold)) html = html.replace("__SILVER__", str(silver)) html = html.replace("__BRONZE__", str(bronze)) html = html.replace("__HONOURABLE__", str(honourable)) else: html = html.replace("__AWARDS_STYLE__", "display: none;") html = templates.final_replace(html, "../..") util.writefile("../timeline/" + year + "/index.html", html)
def run(): print("Creating 404") html = templates.get("404") html = templates.initial_replace(html, -1) html = templates.final_replace(html, ".") util.writefile("../404.html", html)
def run(year): print("Creating timeline/" + year + "/country") html = templates.get("timeline/year/country") html = templates.initial_replace(html, 1) yeardata = t_db_y[year] html = html.replace("__YEAR__", year) html = html.replace("__NUMBER__", yeardata["number"]) html = html.replace("__ORDINAL__", util.ordinal(yeardata["number"])) if year in previous_year: html = html.replace("__PREVIOUS_YEAR__", previous_year[year]) html = html.replace("__PREVIOUS_YEAR_STYLE__", "") else: html = html.replace("__PREVIOUS_YEAR_STYLE__", "display: none;") html = html.replace("__PREVIOUS_YEAR__", ".") # Google crawler fix if year in next_year: html = html.replace("__NEXT_YEAR__", next_year[year]) html = html.replace("__NEXT_YEAR_STYLE__", "") else: html = html.replace("__NEXT_YEAR_STYLE__", "display: none;") html = html.replace("__NEXT_YEAR__", ".") # Google crawler fix medals = {} if year in s_db_y: for row in s_db_y[year]: if row["code"] == "": # Country unknown continue if row["code"] not in medals: medals[row["code"]] = { "bestrank": int(row["rank"]), "bestrank>=": "≥" if row["rank>="] else "", "gold": 0, "silver": 0, "bronze": 0, "honourable": 0 } if row["medal"] == "G": medals[row["code"]]["gold"] += 1 elif row["medal"] == "S": medals[row["code"]]["silver"] += 1 elif row["medal"] == "B": medals[row["code"]]["bronze"] += 1 elif row["medal"] == "H": medals[row["code"]]["honourable"] += 1 def keyfn(code): m = medals[code] return (m["gold"], m["silver"], m["bronze"], m["honourable"], -m["bestrank"]) sortedcodes = reversed(sorted(medals, key = keyfn)) tablehtml = "" prevcode = "" prevrank = 0 for i, code in enumerate(sortedcodes): rowhtml = templates.get("timeline/year/country_row") rowhtml = rowhtml.replace("__CODE__", code) rowhtml = rowhtml.replace("__COUNTRY__", code_to_country[code]) if prevcode != "" and keyfn(prevcode) == keyfn(code): rowhtml = rowhtml.replace("__RANK__", prevrank) else: rowhtml = rowhtml.replace("__RANK__", str(i + 1)) prevcode = code prevrank = str(i + 1) rowhtml = rowhtml.replace("__GOLD__", str(medals[code]["gold"])) rowhtml = rowhtml.replace("__SILVER__", str(medals[code]["silver"])) rowhtml = rowhtml.replace("__BRONZE__", str(medals[code]["bronze"])) rowhtml = rowhtml.replace("__HONOURABLE__", str(medals[code]["honourable"])) rowhtml = rowhtml.replace("__BEST_RANK__", medals[code]["bestrank>="] + str(medals[code]["bestrank"])) tablehtml += rowhtml html = html.replace("__TABLE__", tablehtml) html = templates.final_replace(html, "../..") util.writefile("../timeline/" + year + "/country.html", html)
def run(month): print("Creating timeline/" + month + "/index") html = templates.get("timeline/month/index") html = templates.initial_replace(html, 1) monthdata = t_db_m[month] html = html.replace("__MONTH__", month) html = html.replace("__CONTEST_NAME__", monthdata["name"]) html = html.replace("__NUMBER__", monthdata["number"]) html = html.replace("__ORDINAL__", util.ordinal(monthdata["number"])) html = html.replace("__DATE__", monthdata["date"]) if month in previous_month: html = html.replace("__PREVIOUS_MONTH__", previous_month[month]) html = html.replace("__PREVIOUS_MONTH_STYLE__", "") else: html = html.replace("__PREVIOUS_MONTH_STYLE__", "display: none;") html = html.replace("__PREVIOUS_MONTH__", ".") # Google crawler fix if month in next_month: html = html.replace("__NEXT_MONTH__", next_month[month]) html = html.replace("__NEXT_MONTH_STYLE__", "") else: html = html.replace("__NEXT_MONTH_STYLE__", "display: none;") html = html.replace("__NEXT_MONTH__", ".") # Google crawler fix if monthdata["p_student"] != "": html = html.replace("__P_STUDENT_STYLE__", "") html = html.replace("__P_STUDENT__", monthdata["p_student"]) else: html = html.replace("__P_STUDENT_STYLE__", "display: none;") gold = 0 silver = 0 bronze = 0 honourable = 0 if month in contest_results: for studentdata in contest_results[month]: if studentdata["medal"] == "G": gold += 1 elif studentdata["medal"] == "S": silver += 1 elif studentdata["medal"] == "B": bronze += 1 elif studentdata["medal"] == "H": honourable += 1 html = html.replace("__AWARDS_STYLE__", "") html = html.replace("__GOLD__", str(gold)) html = html.replace("__SILVER__", str(silver)) html = html.replace("__BRONZE__", str(bronze)) html = html.replace("__HONOURABLE__", str(honourable)) else: html = html.replace("__AWARDS_STYLE__", "display: none;") if month in contest_results: html = html.replace("__INDIVIDUAL_STYLE__", "") num_problems = len(contest_results[month][0]["scores"]) header = templates.get(f"timeline/month/stats/header_{num_problems}") count = [[0 for _ in range(8)] for _ in range(num_problems)] for entry in contest_results[month]: for i, score in enumerate(entry["scores"]): count[i][score] += 1 nums = "" for score in range(8): rowhtml = templates.get(f"timeline/month/stats/num_{num_problems}") rowhtml = rowhtml.replace("__SCORE__", str(score)) for i in range(num_problems): rowhtml = rowhtml.replace(f"__P{i+1}__", str(count[i][score])) nums += rowhtml means = templates.get(f"timeline/month/stats/index_value_{num_problems}") \ .replace("__NAME__", "Mean") \ .replace("__STYLE__", "mean") maxs = templates.get(f"timeline/month/stats/index_value_{num_problems}") \ .replace("__NAME__", "Max") \ .replace("__STYLE__", "max") sigmas = templates.get(f"timeline/month/stats/index_value_{num_problems}") \ .replace("__NAME__", "σ") \ .replace("__STYLE__", "sigma") for i in range(num_problems): total = sum(count[i]) total_score = sum(score * count[i][score] for score in range(8)) mean = total_score / total means = means.replace(f"__P{i+1}__", "%.3f" % mean) max_score = max(score for score in range(8) if count[i][score] or score == 0) maxs = maxs.replace(f"__P{i+1}__", str(max_score)) variance = sum( pow((score - mean), 2) * count[i][score] for score in range(8)) / total sigmas = sigmas.replace(f"__P{i+1}__", "%.3f" % sqrt(variance)) html = html.replace("__STATS_STYLE__", "") \ .replace("__TABLE_HEADER__", header) \ .replace("__TABLE_NUM__", nums) \ .replace("__TABLE_MEAN__", means) \ .replace("__TABLE_MAX__", maxs) \ .replace("__TABLE_SIGMA__", sigmas) else: html = html.replace("__INDIVIDUAL_STYLE__", "display: none;") html = html.replace("__STATS_STYLE__", "display: none;") html = templates.final_replace(html, "../..") util.writefile("../dest/timeline/" + month + "/index.html", html)
def run(month): print("Creating timeline/" + month + "/individual") html = templates.get("timeline/month/individual") html = templates.initial_replace(html, 1) monthdata = t_db_m[month] html = html.replace("__MONTH__", month) html = html.replace("__CONTEST_NAME__", monthdata["name"]) html = html.replace("__NUMBER__", monthdata["number"]) html = html.replace("__ORDINAL__", util.ordinal(monthdata["number"])) if month in previous_month: html = html.replace("__PREVIOUS_MONTH__", previous_month[month]) html = html.replace("__PREVIOUS_MONTH_STYLE__", "") else: html = html.replace("__PREVIOUS_MONTH_STYLE__", "display: none;") html = html.replace("__PREVIOUS_MONTH__", ".") # Google crawler fix if month in next_month: html = html.replace("__NEXT_MONTH__", next_month[month]) html = html.replace("__NEXT_MONTH_STYLE__", "") else: html = html.replace("__NEXT_MONTH_STYLE__", "display: none;") html = html.replace("__NEXT_MONTH__", ".") # Google crawler fix tablehtml = "" anonymous_found = False if month in s_db_y: for row in s_db_y[month]: rowhtml = templates.get("timeline/month/individual_row_" + str(len(row["scores"]))) rowhtml = rowhtml.replace("__TOTAL_SCORE__", str(row["total_score"])) rowhtml = rowhtml.replace("__RANK__", str(row["rank"])) for i, x in enumerate(row["scores"]): rowhtml = rowhtml.replace(f"__SCORE_{i+1}__", str(x)) if row["medal"] == "G": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/month/individual_gold")) elif row["medal"] == "S": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/month/individual_silver")) elif row["medal"] == "B": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/month/individual_bronze")) elif row["medal"] == "H": rowhtml = rowhtml.replace( "__MEDAL__", templates.get("timeline/month/individual_honourable")) else: rowhtml = rowhtml.replace("__MEDAL__", "") if row["is_anonymous"]: rowhtml = rowhtml.replace("__NAME__", "") rowhtml = rowhtml.replace("__USER_ID__", "") tablehtml += rowhtml anonymous_found = True else: rowhtml = rowhtml.replace("__NAME__", row["name"]) rowhtml = rowhtml.replace("__USER_ID__", row["user-id"]) tablehtml += rowhtml header = "" script = "" if month in s_db_y and len(s_db_y[month]) >= 1: header = templates.get("timeline/month/individual_header_" + str(len(s_db_y[month][0]["scores"]))) script = templates.get("timeline/month/individual_script_" + str(len(s_db_y[month][0]["scores"]))) html = html.replace("__TABLE__", tablehtml) html = html.replace("__TABLE_HEADER__", header) html = html.replace("__SORT_SCRIPT__", script) html = html.replace( "__ANONYMOUS_MSG__", "The names of anonymous contestants are not shown." if anonymous_found else "") html = templates.final_replace(html, "../..") util.writefile("../dest/timeline/" + month + "/individual.html", html)
def run(code): print("Creating countries/" + code + "/index") html = templates.get("countries/code/index") html = templates.initial_replace(html, 2) codedata = c_db_c[code] html = html.replace("__CODE__", code) html = html.replace("__COUNTRY__", codedata["country"]) if codedata["website"] != "": html = html.replace("__CONTACT_STYLE__", "") html = html.replace("__NATIONAL_SITE__", codedata["website"]) if len(codedata["website"]) < 50: html = html.replace("__NATIONAL_SITE_TEXT__", codedata["website"]) else: html = html.replace("__NATIONAL_SITE_TEXT__", codedata["website"][0:50] + "...") else: html = html.replace("__CONTACT_STYLE__", "display: none;") html = html.replace("__NATIONAL_SITE__", ".") # Google crawler fix if code in previous_code: html = html.replace("__PREVIOUS_CODE__", previous_code[code]) html = html.replace("__PREVIOUS_CODE_STYLE__", "") else: html = html.replace("__PREVIOUS_CODE_STYLE__", "display: none;") html = html.replace("__PREVIOUS_CODE__", ".") # Google crawler fix if code in next_code: html = html.replace("__NEXT_CODE__", next_code[code]) html = html.replace("__NEXT_CODE_STYLE__", "") else: html = html.replace("__NEXT_CODE_STYLE__", "display: none;") html = html.replace("__NEXT_CODE__", ".") # Google crawler fix if code in t_db_c: hostshtml = "" for yeardata in t_db_c[code]: hosthtml = templates.get("countries/code/index_host") if yeardata["city"] != "": hosthtml = hosthtml.replace("__CITY__", " - " + yeardata["city"]) else: hosthtml = hosthtml.replace("__CITY__", "") if yeardata["homepage"] != "": homepagehtml = templates.get("countries/code/index_host_homepage") homepagehtml = homepagehtml.replace("__LINK__", yeardata["homepage"]) hosthtml = hosthtml.replace("__HOMEPAGE__", homepagehtml) else: hosthtml = hosthtml.replace("__HOMEPAGE__", "") hosthtml = hosthtml.replace("__YEAR__", yeardata["year"]) hostshtml += hosthtml html = html.replace("__HOST__", "<dt>IPhO Host</dt>" + hostshtml) else: html = html.replace("__HOST__", "") gold = 0 silver = 0 bronze = 0 honourable = 0 if code in s_db_c: for studentdata in s_db_c[code]: if studentdata["medal"] == "G": gold += 1 elif studentdata["medal"] == "S": silver += 1 elif studentdata["medal"] == "B": bronze += 1 elif studentdata["medal"] == "H": honourable += 1 html = html.replace("__GOLD__", str(gold)) html = html.replace("__SILVER__", str(silver)) html = html.replace("__BRONZE__", str(bronze)) html = html.replace("__HONOURABLE__", str(honourable)) html = templates.final_replace(html, "../..") util.writefile("../countries/" + code + "/index.html", html)
def run(): print("Creating index") html = templates.get("index") html = templates.initial_replace(html, 0) html = templates.final_replace(html, ".") util.writefile("../dest/index.html", html)
def run(month): print("Creating timeline/" + month + "/country") html = templates.get("timeline/month/country") html = templates.initial_replace(html, 1) monthdata = t_db_m[month] html = html.replace("__MONTH__", month) html = html.replace("__NUMBER__", monthdata["number"]) html = html.replace("__ORDINAL__", util.ordinal(monthdata["number"])) if month in previous_month: html = html.replace("__PREVIOUS_MONTH__", previous_month[month]) html = html.replace("__PREVIOUS_MONTH_STYLE__", "") else: html = html.replace("__PREVIOUS_MONTH_STYLE__", "display: none;") html = html.replace("__PREVIOUS_MONTH__", ".") # Google crawler fix if month in next_month: html = html.replace("__NEXT_MONTH__", next_month[month]) html = html.replace("__NEXT_MONTH_STYLE__", "") else: html = html.replace("__NEXT_MONTH_STYLE__", "display: none;") html = html.replace("__NEXT_MONTH__", ".") # Google crawler fix medals = {} if month in s_db_y: for row in s_db_y[month]: if row["name"] not in medals: medals[row["name"]] = { "bestrank": int(row["rank"]), "bestrank>=": "≥" if row["rank>="] else "", "gold": 0, "silver": 0, "bronze": 0, "honourable": 0 } if row["medal"] == "G": medals[row["name"]]["gold"] += 1 elif row["medal"] == "S": medals[row["name"]]["silver"] += 1 elif row["medal"] == "B": medals[row["name"]]["bronze"] += 1 elif row["medal"] == "H": medals[row["name"]]["honourable"] += 1 def keyfn(code): m = medals[code] return (-m["gold"], -m["silver"], -m["bronze"], -m["honourable"], m["bestrank"], code) sortedcodes = sorted(medals, key=keyfn) tablehtml = "" prevcode = "" prevrank = 0 for i, code in enumerate(sortedcodes): rowhtml = templates.get("timeline/month/country_row") rowhtml = rowhtml.replace("__CODE__", code) if prevcode != "" and keyfn(prevcode)[:-1] == keyfn(code)[:-1]: rowhtml = rowhtml.replace("__RANK__", prevrank) else: rowhtml = rowhtml.replace("__RANK__", str(i + 1)) prevcode = code prevrank = str(i + 1) rowhtml = rowhtml.replace("__GOLD__", str(medals[code]["gold"])) rowhtml = rowhtml.replace("__SILVER__", str(medals[code]["silver"])) rowhtml = rowhtml.replace("__BRONZE__", str(medals[code]["bronze"])) rowhtml = rowhtml.replace("__HONOURABLE__", str(medals[code]["honourable"])) rowhtml = rowhtml.replace( "__BEST_RANK__", medals[code]["bestrank>="] + str(medals[code]["bestrank"])) tablehtml += rowhtml html = html.replace("__TABLE__", tablehtml) html = templates.final_replace(html, "../..") util.writefile("../dest/timeline/" + month + "/country.html", html)
def run(): print("Creating index") html = templates.get("index") html = templates.initial_replace(html, 0) html = templates.final_replace(html, ".") util.writefile("../index.html", html)