#!/usr/bin/env python3
import os
import json
import requests
from bs4 import BeautifulSoup
from colorama import Fore
from colorama import Style
def upload():
config = json.load(open('config'))
get_url = 'https://schlangen.bytewerk.org/snake/edit/latest'
cookies = {'sessionid': config['session_id']}
r = requests.get(get_url, cookies=cookies)
cookies['csrftoken'] = r.cookies['csrftoken']
soup = BeautifulSoup(r.text, 'lxml')
xcsrf = soup.select_one('input[name="csrfmiddlewaretoken"]')['value']
code = open('bot.cpp').read()
post_url = 'https://schlangen.bytewerk.org/snake/edit/save'
form_data = {'action': 'run', 'code': code, 'comment': None, 'parent': config['snake_id']}
r = requests.post(post_url, headers={'X-CSRFToken': xcsrf, 'X-Requested-With': 'XMLHttpRequest'}, json=form_data, cookies=cookies)
print(r.text)
print('waiting for compilation...')
state = 'not_compiled'
while state == 'not_compiled':
r = requests.get('https://schlangen.bytewerk.org/api/v1/compile_state', cookies=cookies)
j = json.loads(r.text)
state = j['compile_state']
for l in j['build_log']:
fmt = f'{Fore.GREEN}' if not 'e' in l.keys() else f'{Fore.RED}'
fmt_reset = f'{Style.RESET_ALL}'
key = list(l.keys())[0]
print('%s%s%s' % (fmt, l[key], fmt_reset), end=)
while True:
try:
os.system('inotifywait -e modify bot.cpp')
upload()
except KeyboardInterrupt:
break