def tracks(self): # We might be missing related items data entirely, in which case we start by issuing a lookup there. # TODO: This probably could be done as part of one lookup with the one about to be made. try: tracks = list(xp(self.data, 'RelatedItems')['c']['RelatedItem']) except KeyError: try: self._issueLookup() except LookupRequiredError: return [] try: tracks = list(xp(self.data, 'RelatedItems')['c']['RelatedItem']) page_count = int(xp(self.data, 'RelatedItems', 'RelatedItemPageCount')['v']) for i in range(1,page_count): page = i+1 self.countLookupCall('tracks') data = globalAmazon().item_lookup(ItemId=self.key, ResponseGroup='Large,RelatedItems', RelationshipType='Tracks', RelatedItemPage=str(page), timeout=MERGE_TIMEOUT) tracks.extend( xp(data, 'ItemLookupResponse', 'Items', 'Item', 'RelatedItems')['c']['RelatedItem'] ) track_d = {} for track in tracks: track_d[ int(xp(track, 'Item', 'ItemAttributes', 'TrackSequence')['v']) ] = { 'name' : xp(track, 'Item', 'ItemAttributes', 'Title')['v'], 'key' : xp(track, 'Item', 'ASIN')['v'], } return [ track_d[k] for k in sorted(track_d) ] except LookupRequiredException: return [] except Exception: # TODO: It seems possible that only one of the requests failed; shouldn't we keep the results of the others? report() return []
def _issueLookup(self): # Slightly ugly -- calling ResolverObject method just because we know all _AmazonObject implementations also # inherit from ResolverObject. TODO: Get rid of multiple inheritance! # We don't catch the LookupRequiredError here because if you are initializing a capped-lookup object without # passing in initial data, you are doing it wrong. self.countLookupCall('base data') raw = globalAmazon().item_lookup(timeout=MERGE_TIMEOUT, **self.__params) self.__data = xp(raw, 'ItemLookupResponse','Items','Item')
def __searchIndexLite(self, searchIndexData, queryText, results, timeout): searchResults = globalAmazon().item_search(timeout=timeout, SearchIndex=searchIndexData.searchIndexName, ResponseGroup=searchIndexData.responseGroups, Keywords=queryText, Count=25, priority='high') #print "\n\n\n\nAMAZON\n\n\n\n\n" #pprint(searchResults) #print "\n\n\n\nENDMAZON\n\n\n\n\n" indexResults = [] for item in _getSearchResults(searchResults): parsedItem = searchIndexData.proxyConstructor(item, maxLookupCalls=0) if parsedItem: indexResults.append(parsedItem) results[searchIndexData.searchIndexName] = indexResults
def entityProxyFromKey(self, key, **kwargs): try: lookupData = globalAmazon().item_lookup(ResponseGroup='Large', ItemId=key, timeout=MERGE_TIMEOUT) result = _getLookupResult(lookupData) kind = xp(result, 'ItemAttributes', 'ProductGroup')['v'].lower() logs.debug(kind) if kind == 'book' or kind == 'ebooks': return AmazonBook(key, result, 0) if kind == 'video games': return AmazonVideoGame(key, result, 0) return self.__constructMusicObjectFromResult(result, 0) except KeyError: logs.report() return None
def gen(): try: for params in queries: test = params.pop('test', lambda x: True) if 'SearchIndex' not in params: params['SearchIndex'] = 'All' if 'ResponseGroup' not in params: params['ResponseGroup'] = "ItemAttributes" results = globalAmazon().item_search(timeout=SEARCH_TIMEOUT, **params) for item in _getSearchResults(results): try: if test == None or test(item): yield xp(item, 'ASIN')['v'], item except Exception: pass except GeneratorExit: pass