summaryrefslogtreecommitdiff
path: root/scrape-boards.py
blob: f9e02cdac1a429e0dd4f7681dea3c4029e41734a (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
from bs4 import BeautifulSoup as bs
import bs4
import os
import sys

traveller_file = file(sys.argv[1])
traveller = bs(traveller_file, 'lxml')

print '% PBN 1.0'
print '[Generator "JFRTeamy-restorerer"]'
print '[Event "%s"]' % (traveller_file.name)

board_links = traveller.select('td.bdcc a.zb')
for board_link in board_links:
    if board_link.has_attr('href'):
        board_number = board_link.text.strip()
        board_file = open(
            os.path.join(
                os.path.dirname(traveller_file.name),
                board_link['href']
            )
        )
        board = bs(board_file, 'lxml')
        conditions = [
            c for c in
            board.select('td[valign="top"] h4')[0].contents
            if type(c) == bs4.element.NavigableString
        ]
        dealer = conditions[0]
        vulnerability = conditions[1].title()
        if len(vulnerability) < 3:
            vulnerability = vulnerability.upper()
        card_cells = board.select('td.w')
        if len(card_cells) == 4:
            cards = [
                [
                    line.replace('10', 'T').replace(' ', '').strip()
                    for line in c
                    if type(line) == bs4.element.NavigableString
                ][1::2] for c in card_cells
            ]
            print '[Board "%s"]' % board_number
            print '[Dealer "%s"]' % dealer
            print '[Vulnerable "%s"]' % vulnerability
            print '[Deal "N:%s %s %s %s"]' % (
                '.'.join(cards[0]),
                '.'.join(cards[2]),
                '.'.join(cards[3]),
                '.'.join(cards[1])
            )
            print