Steven Smith revised this gist . Go to revision
1 file changed, 1 deletion
toornamenter.py
| @@ -6,7 +6,6 @@ | |||
| 6 | 6 | import requests | |
| 7 | 7 | from argparse import ArgumentParser | |
| 8 | 8 | from os import environ | |
| 9 | - | from csv import reader | |
| 10 | 9 | ||
| 11 | 10 | x = "custom_field_create[{}]"# | |
| 12 | 11 | ||
Steven Smith revised this gist . Go to revision
1 file changed, 43 insertions
toornamenter.py(file created)
| @@ -0,0 +1,43 @@ | |||
| 1 | + | #!/usr/bin/env python3 | |
| 2 | + | # $ python3 toornamenter.py '1349047821760815104,Test label,test_label,1,1,1 | |
| 3 | + | # 1262174713922625536,Test label,test_label,1,1,1' | |
| 4 | + | # yes that's a newline in there. takes csv as long as it's purely comma separated values, | |
| 5 | + | # no quotes or escaped commas supported, sorry | |
| 6 | + | import requests | |
| 7 | + | from argparse import ArgumentParser | |
| 8 | + | from os import environ | |
| 9 | + | from csv import reader | |
| 10 | + | ||
| 11 | + | x = "custom_field_create[{}]"# | |
| 12 | + | ||
| 13 | + | def main(): | |
| 14 | + | """ toornament_id,"Label of field",slug_of_field,position(sequential)(int),required(boolean)(int),public(boolean)(int) """ | |
| 15 | + | parser = ArgumentParser() | |
| 16 | + | parser.add_argument("tids", help='newline-separated list of comma-separated data: toornament_id,"Label of field",slug_of_field,position(sequential)(int),required(boolean)(int),public(boolean)(int)') | |
| 17 | + | parser.add_argument("--email", default=environ.get("TOORNAMENT_EMAIL", None)) | |
| 18 | + | parser.add_argument("--password", default=environ.get("TOORNAMENT_PASSWORD", None)) | |
| 19 | + | args = parser.parse_args() | |
| 20 | + | fields = {} | |
| 21 | + | for field in args.tids.split("\n"): | |
| 22 | + | tid,label,slug,pos,req,pub = field.split(",") | |
| 23 | + | if not tid in fields: | |
| 24 | + | fields[tid] = [] | |
| 25 | + | fields[tid].append({ | |
| 26 | + | x.format("label"): label, | |
| 27 | + | x.format("machineName"): slug, | |
| 28 | + | x.format("position"): int(pos), | |
| 29 | + | x.format("required"): int(req), | |
| 30 | + | x.format("public"): int(pub) | |
| 31 | + | }) | |
| 32 | + | s = requests.Session() | |
| 33 | + | csrf1 = s.get("https://account.toornament.com/login/").text.split('name="_token" value="',1)[1].split('"',1)[0] | |
| 34 | + | login = s.post("https://account.toornament.com/login_check", data={"_username": args.email, "_password": args.password, "_token": csrf1}) | |
| 35 | + | for tid in fields: | |
| 36 | + | csrf2 = s.get("https://organizer.toornament.com/tournaments/{}/participants/settings/custom-field/create?target-type=player&type=text".format(tid)).text.split('name="custom_field_create[_token]" value="',1)[1].split('"',1)[0] | |
| 37 | + | for field in fields[tid]: | |
| 38 | + | field.update({x.format("_token"): csrf2}) | |
| 39 | + | field_req = s.post("https://organizer.toornament.com/tournaments/{}/participants/settings/custom-field/create".format(tid), params={"target-type": "player", "type": "text"}, data=field) | |
| 40 | + | print("Added {} to {}".format(repr(field), tid)) | |
| 41 | + | ||
| 42 | + | if __name__ == "__main__": | |
| 43 | + | main() | |