Created
February 10, 2010 20:11
-
-
Save blinks/300787 to your computer and use it in GitHub Desktop.
Choose a random game from your BGG collection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.6 | |
""" | |
Choose a random game from your BGG collection. For example: | |
$ ./randgame.py blinks | |
Downloading collection for blinks... | |
Filtering collection of 66 for 2 players... | |
Filtering remaining 53 for 90 minutes... | |
Filtering remaining 39 for rating 6.0... | |
Choosing from 29 games... -> Fluxx <- | |
""" | |
from __future__ import print_function | |
import csv | |
import logging | |
import random | |
import urllib | |
logging.basicConfig(level=logging.WARN) | |
log = logging.getLogger('randgame') | |
def main(*args, **opts): | |
print('Downloading collection for %s...' % ', '.join(args)) | |
collection = [] | |
for username in args: | |
collection.extend(csv.DictReader(urllib.urlopen(link('blinks')))) | |
if not collection: | |
print('The users you listed have no owned games on BGG.') | |
return | |
players = int(opts.get('players', 2)) | |
print('Filtering collection of %i for %i players...' | |
% (len(collection), players)) | |
collection = [game for game in collection | |
if players >= int(game['minplayers']) | |
and players <= int(game['maxplayers'])] | |
if not collection: | |
print('None of the games in the collection can be played with your group.') | |
return | |
time = int(opts.get('time', 90)) | |
print('Filtering remaining %i for %i minutes...' | |
% (len(collection), time)) | |
collection = [game for game in collection if time >= int(game['playingtime'])] | |
if not collection: | |
print("You don't have enough time to play any of these games.") | |
return | |
rating = float(opts.get('rating', 6.0)) | |
print('Filtering remaining %i for minimum rating %0.1f...' | |
% (len(collection), rating)) | |
collection = [game for game in collection if rating <= float(game['rating'])] | |
if not collection: | |
print('No games left to choose from! Relax your constraints a bit, eh?') | |
return | |
print('Choosing from remaining %i games...' | |
% len(collection), end=' ') | |
choice = random.choice(collection) | |
print('-> %(objectname)s <-' % choice) | |
def link(username): | |
"""Return a link to a BGG user's collection, in CSV format.""" | |
return ('http://boardgamegeek.com/geekcollection.php?' + urllib.urlencode({ | |
'action': 'exportcsv', 'subtype': 'boardgame', 'username': username})) | |
def flags(): | |
from optparse import OptionParser | |
parser = OptionParser() | |
parser.set_defaults(players=2, time=90, rating=6.0) | |
parser.add_option('-p', '--players', action='store', dest='players', | |
help='how many players are available?') | |
parser.add_option('-t', '--playing-time', action='store', dest='time', | |
help='how much time do you have to play, in minutes?') | |
parser.add_option('-r', '--min-rating', action='store', dest='rating', | |
help='how low of a personal rating would still be acceptable?') | |
parser.add_option('--debug', | |
action='store_const', const=logging.DEBUG, dest='logLevel') | |
parser.add_option('-v', '--verbose', | |
action='store_const', const=logging.INFO, dest='logLevel') | |
parser.add_option('-q', '--quiet', | |
action='store_const', const=logging.WARN, dest='logLevel') | |
opts, args = parser.parse_args() | |
log.setLevel(opts.logLevel) | |
return opts, args | |
if __name__ == '__main__': | |
opts, args = flags() | |
main(*args, **opts.__dict__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment