Skip to content

Instantly share code, notes, and snippets.

@RedwolfPrograms

RedwolfPrograms/update.py Secret

Created Apr 10, 2021
Embed
What would you like to do?
import requests
import re
import os
answers = []
has_more = True
if not os.path.isdir("contestants"):
raise "contestantsn't"
for f in os.listdir("contestants"):
if not os.path.isdir("contestants/" + f):
os.remove("contestants/" + f)
while has_more:
response = requests.get("https://api.stackexchange.com/2.2/questions/223202/answers?site=codegolf&filter=!bN4iJhwDifN*s1")
if response.status_code != 200:
raise response.status_code
json = response.json()
has_more = json["has_more"]
answers.extend(json["items"])
arena_imports = []
arena_list = []
for a in answers:
if a["body"][:4] != "<h1>" and a["body"][:4] != "<h2>":
raise "namen't"
name = a["body"][4:a["body"].index("</h" + a["body"][2] + ">")]
start = a["body"].index("<code>")
finish = a["body"].index("</code>")
program = a["body"][start + 6:finish]
short_name = re.sub(r"[^a-z0-9]+", "_", name.lower())
file = open("contestants/" + short_name + ".py", "w")
file.write(program)
file.close()
arena_imports.append("from contestants import " + short_name)
arena_list.append(" (\"" + name + "\", " + short_name + ".strategy, " + short_name + ".turn),")
arena = open("arena.py", "r")
old = arena.read()
arena.close()
old_lines = old.splitlines()
new_lines = old_lines[0:3] + [""] + arena_imports + ["", "contestants = ["] + arena_list + old_lines[old_lines.index("]"):]
arena = open("arena.py", "w")
arena.write("\n".join(new_lines))
arena.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment