Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import requests
- import re
- import sys
- from collections import defaultdict
- # maximum number of losses before elimination
- MAX_LOSSES = 3 - 1
- if len(sys.argv)>1:
- year = int(sys.argv[1])
- url = "http://arimaa.com/arimaa/wc/%i/showGames.cgi" % year
- page = requests.get(url).text
- else:
- page = sys.stdin.read()
- # The regex matches eg.
- # <nobr>* <a href="javascript:playerPage('novacat')">novacat</a><br><font size=-1>2002.3</font></nobr>
- # <nobr> <a href="javascript:playerPage('RmznA')">RmznA</a><br><font size=-1>1819.7</font></nobr>
- # <nobr><a href="javascript:playerPage('rabbits')">rabbits</a> *<br><font size=-1>2259.6</font></nobr>
- regex = r"""(?P<line>.+?playerPage\('(?P<player>[\w_]+)'\)">(?P=player)</a>.+?<font size=-1>)"""
- # "-BYE-" is sometimes used as a player, but doesn't match the player group
- regex+=r"|(?P<bye>(?:bye|>-BYE-<))"
- #break the games into rounds ("2nd place", "3rd place" etc. won't be matched)
- regex+=r"|(?:Round (?P<round>\d+))"
- regex = re.compile(regex)
- matches = re.finditer(regex, page)
- nb_loss_for = defaultdict(lambda:0)
- rounds = dict()
- round_id = -1
- while True:
- try:
- match_next = matches.next()
- # Rounds can come left-to-right or right-to-left.
- # The titles are always above the games.
- if match_next.group("round"):
- if round_id > 0:
- rounds[round_id] = new_round
- round_id = int(match_next.group("round"))
- new_round = []
- continue
- match_gold = match_next
- match_silver = matches.next()
- if match_silver.group("bye") or match_gold.group("bye"):
- continue
- gold = match_gold.group("player")
- silver = match_silver.group("player")
- if "*" in match_gold.group("line"):
- result = "g"
- elif "*" in match_silver.group("line"):
- result = "s"
- else:
- result = "?"
- new_round.append( (gold,silver,result) )
- except StopIteration:
- rounds[round_id] = new_round
- break
- for round_id in sorted(rounds):
- print "\n### Round %i ###" % round_id
- for gold, silver, result in rounds[round_id]:
- if (nb_loss_for[gold] > MAX_LOSSES):
- swiss = "!"
- else:
- swiss = ""
- if result=="g":
- nb_loss_for[silver]+=1
- elif result=="s":
- nb_loss_for[gold]+=1
- print "%-24s%-24s%s" % (swiss + gold, silver, result)
Advertisement
Add Comment
Please, Sign In to add comment