summaryrefslogtreecommitdiff
path: root/boards/scrapers/tournamentcalculator/scrape.py
blob: 1d79d8a2c7a725974d328e07d7f2a4e75e5acd49 (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
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]
scoring_group = int(sys.argv[2]) if len(sys.argv) > 2 else 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'][scoring_group-1]['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).replace('10', 'T'))
            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')