import json import requests import sys import time from urllib import parse results_url = parse.urlparse(sys.argv[1]) tournament_name = results_url.path.strip('/').split('/')[-1] def get_json_content(remote_path): return requests.get(url=parse.urljoin(results_url.geturl(), remote_path), params={'_': time.time()}).json() settings = get_json_content('settings.json') board_numbers = settings['BoardsNumbers'] board_sets = [] current_board_set = [] prev_board_number = 0 for board in sorted(board_numbers): board_data = get_json_content('p%d.json' % board)['ScoringGroups'][0]['Distribution'] if board_data['_numberAsPlayed'] < prev_board_number: board_sets.append(current_board_set) current_board_set = [] current_board_set.append(board_data) prev_board_number = board_data['_numberAsPlayed'] board_sets.append(current_board_set) for idx, board_set in enumerate(board_sets): output_filename = '%s-%d.pbn' % (tournament_name, idx+1) with open(output_filename, 'w') as output_file: output_file.write('% PBN 1.0\n') output_file.write('[Generator "TC-scraper"]\n') output_file.write('[Event "%s %d/%d"]\n' % (tournament_name, idx+1, len(board_sets))) for board in board_set: board_number = board['_numberAsPlayed'] board_record = [] for hand in ['N', 'E', 'S', 'W']: hand_record = [] for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']: hand_record.append(board['_handRecord']['Hand%s' % (hand)][suit]) board_record.append('.'.join(hand_record)) output_file.write('[Board "%d"]\n' % (board_number)) output_file.write('[Dealer "%s"]\n' % (['W', 'N', 'E', 'S'][board_number % 4])) output_file.write('[Deal "N:%s"]\n' % (' '.join(board_record))) output_file.write('\n')