-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.py
46 lines (34 loc) · 1.42 KB
/
main2.py
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
import json
import os
from itertools import groupby
from operator import itemgetter
from helper.api import CodeWarsApi
from helper.kata import KataParser
with open('./setup.json') as fin:
setup = json.load(fin)
with open('./source.html') as fin:
file = fin.read()
base_dir = setup['download_folder']
extensions = setup['file_extensions']
parser = KataParser(file)
katas = parser.parse_katas()
api = CodeWarsApi(setup['codewars']['api_key'])
print('Exporting katas...')
for i, kata in enumerate(katas):
print('\r{}/{} katas exported.'.format(i + 1, len(katas)), end='')
kata_description = api.get_kata_description(kata.kata_id)
language_source_codes = kata.get_languages_and_source_codes
# print(language_source_codes)
for language, source_codes in groupby(language_source_codes, itemgetter(0)):
for j, language_source_code in enumerate(source_codes, 1):
file_dir = os.path.join(
base_dir, language, kata.difficulty, kata.title,
)
print(file_dir)
if not os.path.exists(file_dir):
os.makedirs(file_dir)
filename = 'solution_' + str(j) + extensions.get(language, '')
with open(os.path.join(file_dir, filename), 'w') as fout:
fout.write(itemgetter(1)(language_source_code))
with open(os.path.join(file_dir, 'README.md'), 'w') as fout:
fout.write(kata_description)