1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
import requests
import json
from bs4 import BeautifulSoup
from argparse import ArgumentParser
CONFIG_FILENAME = 'config.json'
def parse_args(languages):
parser = ArgumentParser(description='Problem file generator')
parser.add_argument('indexes', nargs='+', type=int,
help='problems indexes for which files are generated')
parser.add_argument('-l', '--language', choices=languages, required=True,
help='the file programming language')
return vars(parser.parse_args())
def read_config():
try:
with open(CONFIG_FILENAME, 'r') as file:
config = json.load(file)
except FileNotFoundError:
print(f'Error: file {CONFIG_FILENAME} not found')
exit(1)
return config
def write_problem(index, title, sub_title, content, language_config):
text = '\n'.join([language_config['comment']['top'], title, sub_title, content,
language_config['comment']['bottom']])
text = '\n'.join([language_config['comment']['prefix']
+ line for line in text.split('\n')])
slug = ''.join([c for c in title.lower().replace(' ', '_') if c.isalpha() or
c.isdigit() or c == '_'])
filename = (str(index).zfill(3) + '-'
+ slug + '.' + language_config['extension'])
try:
with open(filename, 'w') as file:
file.write(text)
print(f'{filename} created')
except:
print(f'Error: could not create file {filename}')
def fetch_problem(index, config, args):
url = config['url_format'].format(index=index)
print(f"fetching problem {index} at {url}...")
data = requests.get(url)
soup = BeautifulSoup(data.text, 'html.parser')
print('parsing data...')
data = soup.find('div', {'id': 'content'})
problem_title = data.h2.text
problem_sub_title = data.h3.text
problem_data = soup.find('div', {'class': 'problem_content'})
print('\nTitle:', problem_title)
print('Sub title:', problem_sub_title)
print('Text:', problem_data.text, '\n')
write_problem(index, problem_title, problem_sub_title, problem_data.text,
config['languages'][args['language']])
if __name__ == '__main__':
config = read_config()
args = parse_args(config['languages'].keys())
for p in args['indexes']:
fetch_problem(p, config, args)
|