diff options
author | emkael <emkael@tlen.pl> | 2022-04-05 10:22:01 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2022-04-05 10:22:01 +0200 |
commit | 4fdb2b726dc9f95e95ead5d9a45f03851f160629 (patch) | |
tree | 72586c89835aca7f601666e555891844bc373de8 /boards/scrapers | |
parent | cd418f19e42c946c7216005d3dce97c545d120fc (diff) |
TC boards scraper
Diffstat (limited to 'boards/scrapers')
-rw-r--r-- | boards/scrapers/tournamentcalculator/requirements.txt | 1 | ||||
-rw-r--r-- | boards/scrapers/tournamentcalculator/scrape.py | 50 |
2 files changed, 51 insertions, 0 deletions
diff --git a/boards/scrapers/tournamentcalculator/requirements.txt b/boards/scrapers/tournamentcalculator/requirements.txt new file mode 100644 index 0000000..a743bbe --- /dev/null +++ b/boards/scrapers/tournamentcalculator/requirements.txt @@ -0,0 +1 @@ +requests==2.27.1 diff --git a/boards/scrapers/tournamentcalculator/scrape.py b/boards/scrapers/tournamentcalculator/scrape.py new file mode 100644 index 0000000..d82c778 --- /dev/null +++ b/boards/scrapers/tournamentcalculator/scrape.py @@ -0,0 +1,50 @@ +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') |