from bs4 import BeautifulSoup as bs import bs4 import os import sys traveller_file = open(sys.argv[1], encoding='utf8') 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() dealer = ['W', 'N', 'E', 'S'][int(board_number) % 4] with open( os.path.join( os.path.dirname(traveller_file.name), board_link['href'] ), encoding='utf8' ) as board_file: board = bs(board_file, 'lxml') card_cells = board.select('td.w') # ordinary JFR if len(card_cells) != 4: # ukrywacz'ed JFR (TDD with a single table) cell_brs = board.select('td br') for br in cell_brs: cell = br.parent if cell.name == 'td' and cell not in card_cells: card_cells.append(cell) card_strings = [ [ line.replace('10', 'T').replace(' ', '').strip() for line in c if type(line) == bs4.element.NavigableString ] for c in card_cells ] # ordinary JFR has 8 strings per cell, TDD makes it 4 cards = [c if len(c) == 4 else c[1::2] for c in card_strings] print('[Board "%s"]' % board_number) print('[Dealer "%s"]' % dealer) print('[Deal "N:%s %s %s %s"]' % ( '.'.join(cards[0]), '.'.join(cards[2]), '.'.join(cards[3]), '.'.join(cards[1]) )) print() traveller_file.close()