def getCachedData(cache, userAgent, remoteAddress, checkIn, checkout): if cache: newYorkHotelIds = memcache.get('newYorkHotelIds') if newYorkHotelIds is None: newYorkHotelIds = [] cacheKey = memcache.get('cacheKey') cacheLocation = memcache.get('cacheLocation') response = {'RESULT_NULL':'RESULT'} if memcache.get('fullCache') == None: response = json.load(urllib.urlopen(getSigUrl(userAgent, remoteAddress, cacheKey, cacheLocation, checkIn=None, checkout=None))) if str(response).find('RESULT_NULL') >= 0: memcache.add(key='fullCache', value='Full', time=14400) #cached for 4 hours else: hotels = [] try: hotels = response['HotelListResponse']['HotelList']['HotelSummary'] except: logging.error('url: %s', getSigUrl(userAgent, remoteAddress, cacheKey, cacheLocation, checkIn=None, checkout=None)) logging.error(str(response)) if not isinstance(hotels, types.ListType): hotels = [hotels] for hotel in hotels: if memcache.add(key=str(hotel['hotelId']), value=hotel, time=86400): newYorkHotelIds.append(hotel['hotelId']) cacheKey = response['HotelListResponse']['cacheKey'] cacheLocation = response['HotelListResponse']['cacheLocation'] memcache.set(key="newYorkHotelIds", value=newYorkHotelIds, time=86400) #cached for 24 hours memcache.set(key="cacheKey", value=cacheKey, time=300) #cached for 5 minutes memcache.set(key="cacheLocation", value=cacheLocation, time=300) logging.info('len of newYorkHotelIds: %s', len(newYorkHotelIds)) hotelId = choice(newYorkHotelIds) hotel = memcache.get(key=str(hotelId)) while hotel is None and len(newYorkHotelIds) > 0: if memcache.delete(str(hotelId)): newYorkHotelIds.remove(hotelId) logging.warning('Cached hotelId: %s was deleted.', str(hotelId)) memcache.set(key="newYorkHotelIds", value=newYorkHotelIds, time=86400) #cached for 24 hours hotelId = choice(newYorkHotelIds) hotel = memcache.get(key=str(hotelId)) else: logging.error('Failed to delete %s due to DELETE_NETWORK_FAILURE', str(hotelId)) if len(newYorkHotelIds) < 10: logging.error('Cache size below 10! Repopulating...') memcache.flush_all() hotel = getCachedData(cache, userAgent, remoteAddress, checkIn=checkIn, checkout=checkout) return hotel else: result = json.load(urllib.urlopen(getSigUrl(userAgent, remoteAddress, '', '', checkIn=checkIn, checkout=checkout))) hotels = result['HotelListResponse']['HotelList']['HotelSummary'] return choice(hotels)
def get(self): result = json.load(urllib.urlopen(getSigUrl())) hotels = result['HotelListResponse']['HotelList']['HotelSummary'] hotel = choice(hotels) self.response.write('<!DOCTYPE html>\n<html>\n<head>\n<title>Random Hotel</title>\n</head>\n<body>') self.response.write('<a href="/about.html">about</a> ') self.response.write('<a href="/">cached</a> ') self.response.write('<a href="/no_memcache/">not cached</a>\n') self.response.write('<h2>Random [New York] Hotel</h2>') self.response.write('<a href="' + hotel['deepLink'] + '"><h3>' + hotel['name'] + '</h3></a>\n') if 'thumbNailUrl' in hotel and hotel['thumbNailUrl'] is not None and hotel['thumbNailUrl'] != '': self.response.write('<img src="http://images.travelnow.com' + hotel['thumbNailUrl'] + '">\n') self.response.write(' ' + hotel['address1'] + ', ' + hotel['city'] + ', ' + hotel['stateProvinceCode']) self.response.write('<br>\n') if 'tripAdvisorRatingUrl' in hotel: self.response.write('TripAdvisor Rating: <img src="' + hotel['tripAdvisorRatingUrl'] + '">\n') self.response.write('<br><br><br><br><br>\n') self.response.write('<h4>JSON snippet from EAN API response</h4>\n') self.response.write('<pre>' + json.dumps(hotel, sort_keys = True, indent = 2) + '</pre>') self.response.write('\n</body>\n</html>')