def GetPageCount(pagename): logging.debug("pagecount.GetPageCount(pagename='"+pagename+"')") memcache_id = KeyName(pagename) val = memcache.get(memcache_id) if (val != None): return val val = LoadPageCount(pagename) memcache_id = KeyName(pagename) memcache.set(memcache_id, val) return val
def GetPageCount(pagename): logging.debug("pagecount.GetPageCount(pagename='" + pagename + "')") memcache_id = KeyName(pagename) val = memcache.get(memcache_id) if (val != None): return val val = LoadPageCount(pagename) memcache_id = KeyName(pagename) memcache.set(memcache_id, val) return val
def geocode(addr, usecache=True, retrying=False): """convert a human-readable address into a "lat,long" value (string).""" loc = addr.lower().strip() # already geocoded-- just return if is_latlongzoom(loc): return loc if is_latlong(loc): # regexp allow missing comma # TODO: pick a smart default zoom, depending on population density. return loc + ",4" loc = re.sub(r'^[^0-9a-z]+', r'', loc) loc = re.sub(r'[^0-9a-z]+$', r'', loc) loc = re.sub(r'\s\s+', r' ', loc) memcache_key = "geocode:" + loc val = memcache.get(memcache_key) if usecache and val: logging.info("geocode: cache hit loc=" + loc + " val=" + val) return val if not retrying: params = urllib.urlencode({ 'address': loc.lower(), 'sensor': 'false', 'region': 'us', 'client': private_keys.MAPS_API_CLIENT_ID }) request = "/maps/api/geocode/json?%s" % params signature = sign_maps_api_request(request) fetchurl = "http://maps.googleapis.com" + request + "&signature=" + signature logging.info("geocode: cache miss, trying " + fetchurl) fetch_result = urlfetch.fetch(fetchurl, deadline=api.CONST_MAX_FETCH_DEADLINE) if fetch_result.status_code != 200: # fail and also don't cache logging.info("gecode: fail %s %s" % (str(fetch_result.status_code), fetch_result.content)) return "" res = fetch_result.content respcode, zoom, lat, lng = parse_geo_response(res) if respcode == '200': logging.info("geocode: success " + loc) val = lat + "," + lng + "," + zoom rev_geocode_json(lat, lng) memcache.set(memcache_key, val) return val logging.info("geocode: failed " + loc) return ""
def geocode(addr, usecache=True, retrying = False): """convert a human-readable address into a "lat,long" value (string).""" loc = addr.lower().strip() # already geocoded-- just return if is_latlongzoom(loc): return loc if is_latlong(loc): # regexp allow missing comma # TODO: pick a smart default zoom, depending on population density. return loc + ",4" loc = re.sub(r'^[^0-9a-z]+', r'', loc) loc = re.sub(r'[^0-9a-z]+$', r'', loc) loc = re.sub(r'\s\s+', r' ', loc) memcache_key = "geocode:" + loc val = memcache.get(memcache_key) if usecache and val: logging.info("geocode: cache hit loc=" + loc + " val=" + val) return val if not retrying: params = urllib.urlencode( {'address':loc.lower(), 'sensor':'false', 'region':'us', 'client':private_keys.MAPS_API_CLIENT_ID }) request = "/maps/api/geocode/json?%s" % params signature = sign_maps_api_request(request) fetchurl = "http://maps.googleapis.com" + request + "&signature=" + signature logging.info("geocode: cache miss, trying " + fetchurl) fetch_result = urlfetch.fetch(fetchurl, deadline = api.CONST_MAX_FETCH_DEADLINE) if fetch_result.status_code != 200: # fail and also don't cache logging.info("gecode: fail %s %s" % (str(fetch_result.status_code), fetch_result.content)) return "" res = fetch_result.content respcode, zoom, lat, lng = parse_geo_response(res) if respcode == '200': logging.info("geocode: success " + loc) val = lat + "," + lng + "," + zoom rev_geocode_json(lat, lng) memcache.set(memcache_key, val) return val logging.info("geocode: failed " + loc) return ""
def load_friends(self): key_suffix = self.account_type + ":" + self.user_id key = 'friends:' + key_suffix total_key = 'total_friends:' + key_suffix self.friends = memcache.get(key) self.total_friends = memcache.get(total_key) if not self.friends: self.friends = self.get_friends_by_url() memcache.set(key, self.friends, time=USERINFO_CACHE_TIME) memcache.set(total_key, self.total_friends, time=USERINFO_CACHE_TIME) return self.friends
def get_user(request): for cls in (TestUser, FriendConnectUser, FacebookUser): cookie = cls.get_cookie() if cookie: key = 'cookie:' + cookie user = memcache.get(key) if not user: try: user = cls(request) memcache.set(key, user, time=USERINFO_CACHE_TIME) except: # This hides all errors from the Facebook client library # TODO(doll): Hand back an error message to the user logging.exception( "Facebook or Friend Connect client exception.") return None return user
def geocode(addr, usecache=True, retrying = False): """convert a human-readable address into a "lat,long" value (string).""" loc = addr.lower().strip() # already geocoded-- just return if is_latlongzoom(loc): return loc if is_latlong(loc): # regexp allow missing comma # TODO: pick a smart default zoom, depending on population density. return loc + ",4" loc = re.sub(r'^[^0-9a-z]+', r'', loc) loc = re.sub(r'[^0-9a-z]+$', r'', loc) loc = re.sub(r'\s\s+', r' ', loc) memcache_key = "geocode:" + loc val = memcache.get(memcache_key) if usecache and val: logging.info("geocode: cache hit loc=" + loc + " val=" + val) return val if not retrying: params = urllib.urlencode( {'q':loc.lower(), 'output':'csv', 'oe':'utf8', 'sensor':'false', 'gl':'us', 'client':private_keys.MAPS_API_CLIENT_ID }) fetchurl = "http://maps.google.com/maps/geo?%s" % params logging.info("geocode: cache miss, trying " + fetchurl) fetch_result = urlfetch.fetch(fetchurl, deadline = api.CONST_MAX_FETCH_DEADLINE) if fetch_result.status_code != 200: # fail and also don't cache logging.info("gecode: fail %s %s" % (str(fetch_result.status_code), fetch_result.content)) return "" res = fetch_result.content logging.info("geocode: maps responded %s" % res) respcode, zoom, lat, lng = parse_geo_response(res) if respcode == '200': logging.info("geocode: success " + loc) val = lat+","+lng+","+zoom memcache.set(memcache_key, val) return val if retrying or respcode == '620': params = urllib.urlencode( {'q':loc.lower(), }) fetchurl = "http://pipes.appspot.com/geo?%s" % params fetch_result = urlfetch.fetch(fetchurl, deadline = api.CONST_MAX_FETCH_DEADLINE) res = fetch_result.content logging.info("geocode: datastore responded %s" % res) respcode, zoom, lat, lng = parse_geo_response(res) if respcode == '200': val = lat+","+lng+","+zoom memcache.set(memcache_key, val) return val if respcode == '620' and not retrying: logging.info("geocode: retrying " + loc) return geocode(addr, usecache, True) logging.info("geocode: failed " + loc) return ""
def geocode(addr, usecache=False, retrying=False): """convert a human-readable address into a "lat,long" value (string).""" loc = addr.lower().strip() # already geocoded-- just return if is_latlongzoom(loc): return loc if is_latlong(loc): # regexp allow missing comma # TODO: pick a smart default zoom, depending on population density. return loc + ",4" loc = re.sub(r'^[^0-9a-z]+', r'', loc) loc = re.sub(r'[^0-9a-z]+$', r'', loc) loc = re.sub(r'\s\s+', r' ', loc) memcache_key = "geocode:" + loc val = memcache.get(memcache_key) if usecache and val: logging.info("geocode: cache hit loc=" + loc + " val=" + val) return val if not retrying: params = urllib.urlencode({ 'q': loc.lower(), 'output': 'csv', 'oe': 'utf8', 'sensor': 'false', 'gl': 'us', 'key': 'ABQIAAAAPwa6P0RAONGDnDVWIoz60RS_XVdtR9vJUHoImLNBbcMuXbr6qRRCTJ1XM9Je76qJSqsr_4HKGKJ65A' }) fetchurl = "http://maps.google.com/maps/geo?%s" % params logging.info("geocode: cache miss, trying " + fetchurl) fetch_result = urlfetch.fetch(fetchurl) if fetch_result.status_code != 200: # fail and also don't cache return "" res = fetch_result.content logging.info("geocode: maps responded %s" % res) respcode, zoom, lat, lng = parse_geo_response(res) if respcode == '200': logging.info("geocode: success " + loc) val = respcode + "," + zoom + "," + lat + "," + lng memcache.set(memcache_key, val) return val if retrying or respcode == '620': params = urllib.urlencode({ 'q': loc.lower(), }) fetchurl = "http://pipes.appspot.com/geo?%s" % params fetch_result = urlfetch.fetch(fetchurl, deadline=api.CONST_MAX_FETCH_DEADLINE) res = fetch_result.content logging.info("geocode: datastore responded %s" % res) respcode, zoom, lat, lng = parse_geo_response(res) if respcode == '200': val = lat + "," + lng + "," + zoom memcache.set(memcache_key, val) return val if respcode == '620' and not retrying: logging.info("geocode: retrying " + loc) return geocode(addr, usecache, True) logging.info("geocode: failed " + loc) return ""