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)