import requests
import string
import os
import shutil as sh
from argparse import ArgumentParser
from bs4 import BeautifulSoup
cf_url = 'http://codeforces.com/contest'
def_template = '/home/rmaccrimmon/Templates/contest.cc'
def write_io_files(io_divs, path, file_ext):
for i in range(len(io_divs)):
file_name = '.'.join([str(i), file_ext])
file_txt = io_divs[i].find('pre').text.strip().rstrip() + '\n'
with open(os.path.join(path, file_name), 'w') as f:
f.write(file_txt)
def init_problem_dirs(sample_divs, template, path):
letter = iter(string.ascii_uppercase)
for d in sample_divs:
l = next(letter)
prob_dir = os.path.join(path, l)
os.mkdir(prob_dir)
sh.copyfile(template, os.path.join(prob_dir, l + '.cc'))
write_io_files(d.find_all('div', class_='input'), prob_dir, 'in')
write_io_files(d.find_all('div', class_='output'), prob_dir, 'out')
if __name__=='__main__':
parser = ArgumentParser()
parser.add_argument('id', metavar='contest-id')
parser.add_argument('--template-file')
parser.add_argument('--run-script')
args = parser.parse_args()
template = def_template if args.template_file is None else args.template_file
os.mkdir(args.id)
if args.run_script is not None:
sh.copyfile(args.run_script, args.id + '/run')
url = '/'.join([cf_url, args.id, 'problems'])
bs = BeautifulSoup(requests.get(url).text, 'lxml')
divs = bs.find_all('div', class_='sample-test')
init_problem_dirs(divs, template, args.id)