diff --git a/gpapi/googleplay.py b/gpapi/googleplay.py index a2d0bcf..8570ff5 100644 --- a/gpapi/googleplay.py +++ b/gpapi/googleplay.py @@ -347,7 +347,7 @@ class GooglePlayAPI(object): packageNames (list): a list of app IDs (usually starting with 'com.'). Returns: - a list of dictionaries containing docv1 data, or None + a list of dictionaries containing docv2 data, or None if the app doesn't exist""" path = "bulkDetails" @@ -358,7 +358,7 @@ class GooglePlayAPI(object): data.decode("utf-8"), "application/x-protobuf") response = message.payload.bulkDetailsResponse - return [None if not entry.HasField('doc') else + return [None if not utils.hasDoc(entry) else utils.fromDocToDictionary(entry.doc) for entry in response.entry] @@ -372,7 +372,6 @@ class GooglePlayAPI(object): if subCat is not None: path += "&ctr=%s" % requests.utils.quote(subCat) data = self.executeRequestApi2(path) - output = [] if cat is None and subCat is None: # result contains all categories available @@ -380,19 +379,25 @@ class GooglePlayAPI(object): 'dataUrl': c.dataUrl, 'catId': c.unknownCategoryContainer.categoryIdContainer.categoryId} for c in data.payload.browseResponse.category] - else: - # result contains apps of a specific category - # organized by sections - for pf in data.preFetch: - for cluster in pf.response.payload.listResponse.cluster: - for doc in cluster.doc: - apps = [a for a in doc.child] - apps = list(map(utils.fromDocToDictionary, - apps)) - section = {'title': doc.title, - 'docid': doc.docid, - 'apps': apps} - output.append(section) + + output = [] + clusters = [] + + if utils.hasPrefetch(data): + clusters = chain.from_iterable([pf.response.payload.listResponse.cluster + for pf in data.preFetch]) + + # result contains apps of a specific category + # organized by sections + for cluster in clusters: + for doc in cluster.doc: + apps = [a for a in doc.child] + apps = list(map(utils.fromDocToDictionary, + apps)) + section = {'title': doc.title, + 'docid': doc.docid, + 'apps': apps} + output.append(section) return output def list(self, cat, ctr=None, nb_results=None, offset=None): diff --git a/gpapi/utils.py b/gpapi/utils.py index 271ef5f..b5d3e73 100644 --- a/gpapi/utils.py +++ b/gpapi/utils.py @@ -69,26 +69,33 @@ def toBigInt(byteArray): out = out | decoded << key * 8 return out -def hasPrefetch(response): - if type(response) is not googleplay_pb2.ResponseWrapper: - return False +def hasPrefetch(obj): try: - return response.HasField('preFetch') + return len(obj.preFetch) > 0 except ValueError: return False -def hasListResponse(payload): - if type(payload) is not googleplay_pb2.Payload: - return False +def hasListResponse(obj): try: - return payload.HasField('listResponse') + return obj.HasField('listResponse') except ValueError: return False -def hasSearchResponse(payload): - if type(payload) is not googleplay_pb2.Payload: - return False +def hasSearchResponse(obj): try: - return payload.HasField('searchResponse') + return obj.HasField('searchResponse') except ValueError: return False + +def hasDoc(obj): + # doc an be a single object or a + # RepeatedComposite object + try: + existance = obj.HasField('doc') + except ValueError: + try: + existance = len(obj.doc) > 0 + except TypeError: + existance = False + + return existance