harveyy

Get text data from arimaa.com WC pages

Feb 9th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. #!/usr/bin/python
  2. import requests
  3. import re
  4. import sys
  5. from collections import defaultdict
  6.  
  7. # maximum number of losses before elimination
  8. MAX_LOSSES = 3 - 1
  9.  
  10. if len(sys.argv)>1:
  11.     year = int(sys.argv[1])
  12.     url = "http://arimaa.com/arimaa/wc/%i/showGames.cgi" % year
  13.     page = requests.get(url).text
  14. else:
  15.     page = sys.stdin.read()
  16.  
  17. # The regex matches eg.
  18. #    <nobr>* <a href="javascript:playerPage('novacat')">novacat</a><br><font size=-1>2002.3</font></nobr>
  19. #    <nobr> <a href="javascript:playerPage('RmznA')">RmznA</a><br><font size=-1>1819.7</font></nobr>
  20. #     <nobr><a href="javascript:playerPage('rabbits')">rabbits</a> *<br><font size=-1>2259.6</font></nobr>
  21. regex = r"""(?P<line>.+?playerPage\('(?P<player>[\w_]+)'\)">(?P=player)</a>.+?<font size=-1>)"""
  22. # "-BYE-" is sometimes used as a player, but doesn't match the player group
  23. regex+=r"|(?P<bye>(?:bye|>-BYE-<))"
  24. #break the games into rounds ("2nd place", "3rd place" etc. won't be matched)
  25. regex+=r"|(?:Round (?P<round>\d+))"
  26.  
  27. regex  = re.compile(regex)
  28. matches = re.finditer(regex, page)
  29.  
  30. nb_loss_for = defaultdict(lambda:0)
  31. rounds = dict()
  32. round_id = -1
  33.  
  34. while True:
  35.     try:
  36.         match_next = matches.next()
  37.         # Rounds can come left-to-right or right-to-left.
  38.         # The titles are always above the games.
  39.         if match_next.group("round"):
  40.             if round_id > 0:
  41.                 rounds[round_id] = new_round
  42.             round_id =  int(match_next.group("round"))
  43.             new_round =  []
  44.             continue
  45.         match_gold = match_next
  46.         match_silver = matches.next()
  47.         if match_silver.group("bye") or match_gold.group("bye"):
  48.             continue
  49.        
  50.         gold = match_gold.group("player")
  51.         silver = match_silver.group("player")
  52.        
  53.         if "*" in match_gold.group("line"):
  54.             result = "g"
  55.         elif "*" in match_silver.group("line"):
  56.             result = "s"
  57.         else:
  58.             result = "?"
  59.         new_round.append( (gold,silver,result) )
  60.  
  61.     except StopIteration:
  62.         rounds[round_id] = new_round
  63.         break
  64.    
  65. for round_id in sorted(rounds):
  66.     print "\n### Round %i ###"  % round_id
  67.     for gold, silver, result in rounds[round_id]:
  68.         if (nb_loss_for[gold] > MAX_LOSSES):
  69.             swiss = "!"
  70.         else:
  71.             swiss = ""
  72.         if result=="g":
  73.             nb_loss_for[silver]+=1
  74.         elif result=="s":
  75.             nb_loss_for[gold]+=1
  76.         print "%-24s%-24s%s" % (swiss + gold, silver, result)
Advertisement
Add Comment
Please, Sign In to add comment