summaryrefslogtreecommitdiff
path: root/backup-tc.py
blob: 1287ae1857ed72df390c9e77b08b36c1612062d4 (plain)
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
import json, os, sys
import requests

url = sys.argv[1]
output_dir = sys.argv[2]

settings = json.load(open(os.path.join(output_dir, 'settings.json')))

files_to_download = set()

for board in settings['BoardsScoringGroups']:
    for group in board['Value']:
        for prefix in ['d', 'sg']:
            files_to_download.add('%s%d-%d.json' % (prefix, board['Key'], group))
        files_to_download.add('p%d.html' % (board['Key']))

round_jsons = set()

for round_number in settings['RoundsNumbers']:
    round_results = 'o%d-%d.json' % (
        round_number / 1000, round_number % 1000)
    files_to_download.add(round_results)
    round_jsons.add(round_results)
    for participant in settings['ParticipantNumers']:
        files_to_download.add('h%d.json' % (participant))

def fetch_file(url, output_dir, remote_file):
    file_url = requests.compat.urljoin(url + '/', remote_file)
    try:
        r = requests.get(file_url)
        r.raise_for_status()
        with open(os.path.join(output_dir, remote_file), 'w') as output_file:
            output_file.write(r.content)
            print(file_url)
    except Exception as e:
        print 'WARNING: %s' % (e)

for remote_file in files_to_download:
    fetch_file(url, output_dir, remote_file)

files_to_download = set()

for round_json in round_jsons:
    round_file = os.path.join(output_dir, round_json)
    if os.path.exists(round_file):
        round_data = json.load(open(round_file))
        for r in round_data['Results']:
            for s in r['Segments']:
                files_to_download.add('s%s-%d-%d-%d.json' % (
                    r['TableFull'],
                    round_data['Session'], round_data['Round'], s['Segment']
                ))

for remote_file in files_to_download:
    fetch_file(url, output_dir, remote_file)