from google.appengine.api import memcache import logging from google.appengine.api import urlfetch from google.appengine.ext import db from xml.etree import ElementTree as ET import datetime class Collection(db.Model): ts = db.DateTimeProperty(required=True) coll_xml = db.TextProperty(required=True) treeobj = None class Game: def __init__(self, type, id): self.id = "%s/%s" % (type, id) class BGG: def __init__(self): self.idtogame = {} def note(self, game): self.idtogame[game.id] = game bgg = BGG() def GetGames(coll): games = [] for game in coll.findall("item"): g = Game(game.get("objecttype"), game.get("objectid")) g.name = game.find("name").text if(game.find("stats").get("rating")): g.rating = float(game.find("stats").get("rating")) else: g.rating = 0 if(game.find("stats").get("bggrating")): g.bggrating = float(game.find("stats").get("bggrating")) else: g.bggrating = 0 if(game.find("stats").get("rawaveragerating")): g.bggraw = float(game.find("stats").get("rawaveragerating")) else: g.bggraw = 0 g.owners = int(game.find("stats").get("numowned")) g.own = game.find("status").get("own") == "1" bgg.note(g) games.append(g) return games def CollTree(coll): return ET.fromstring(unicode(coll.coll_xml).encode('ascii', 'xmlcharrefreplace')) def GetCollection(user): logging.debug("Starting GetCollection of %s" % user) cached = memcache.get(user) if(cached): logging.debug("Using memcached collection for %s" % user) coll_xml = cached return GetGames(CollTree(Collection(ts=datetime.datetime.now(), coll_xml=coll_xml, key_name=user))) usercoll = Collection.get(db.Key.from_path("Collection", user)) if(usercoll): age = datetime.datetime.now()-usercoll.ts if(age > datetime.timedelta(1)): print "Stale data, fetching anew" return GetGames(CollTree(FetchAndStoreCollection(user))) else: if(not cached): memcache.add(key=user, value=usercoll.coll_xml, time=3600) return GetGames(CollTree(usercoll)) else: return GetGames(CollTree(FetchAndStoreCollection(user))) def FetchAndStoreCollection(user): logging.debug("Starting URLfetch of %s" % user) rawxml = urlfetch.fetch("http://www.boardgamegeek.com/xmlapi/collection/%s?all=1" % user).content logging.debug("Completed URLfetch of %s" % user) coll_xml = db.Text(rawxml, encoding="utf8") memcache.add(key=user, value=coll_xml, time=3600) usercoll = Collection(ts=datetime.datetime.now(), coll_xml=coll_xml, key_name=user) usercoll.put() return usercoll