Advertisement
Guest User

bggcompare.py

a guest
Jul 14th, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import urllib2
  3. import time
  4. import sys
  5.  
  6. def get_collection(username):
  7.     base_url = 'http://www.boardgamegeek.com/xmlapi2/'
  8.     get_url = 'collection?username={0}&subtype=boardgame&own=1'.format(username)
  9.     bggfile = urllib2.urlopen(base_url + get_url)
  10.     bggxml = bggfile.read()
  11.    
  12.    
  13.     bggfile.close()
  14.  
  15.     # find returns -1 if it fails to find something
  16.     # BGG takes time to render a user's list - if it does, it returns a nessage page
  17.     # check for that message, and if we see it, wait 5 seconds and try again
  18.     if bggxml.find('Your request for this collection has been accepted and will be processed.') != -1:
  19.         time.sleep(5)
  20.         game_list = get_collection(username)
  21.     else:
  22.         soup = BeautifulSoup(bggxml)
  23.         game_list = [game.string for game in soup.findAll('name')]
  24.        
  25.     return game_list
  26.  
  27.  
  28. user1 = sys.argv[1]
  29. user2 = sys.argv[2]
  30. print 'fetching games for ' + user1 + ' and ' + user2
  31.  
  32. # super simple approach to make sure we get the real results, since BGG takes a few seconds to render a collection
  33. # to XML
  34. user1coll = get_collection(user1)
  35. user2coll = get_collection(user2)
  36.  
  37. print user1 + ' has the following games which ' + user2 + 'does not:'
  38. print set(user1coll).difference(set(user2coll))
  39. print '*********'
  40. print user1 + ' and ' + user2 + 'have the following games in common:'
  41. print set(user1coll).intersection(set(user2coll))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement