diff --git a/gpapi/googleplay.py b/gpapi/googleplay.py index a9ed7eb..1fce18f 100644 --- a/gpapi/googleplay.py +++ b/gpapi/googleplay.py @@ -274,19 +274,31 @@ class GooglePlayAPI(object): def search(self, query, nb_result, offset=None): if self.authSubToken == None: raise Exception("You need to login before executing any request") - path = "search?c=3&q=%s" % requests.utils.quote(query) + remaining = nb_result + output = [] + + nextPath = "search?c=3&q=%s" % requests.utils.quote(query) if (offset is not None): - path += "&o=%d" % int(offset) + nextPath += "&o=%d" % int(offset) + while remaining > 0 and nextPath != None: + currentPath = nextPath + data = self.executeRequestApi2(currentPath) + if len(data.preFetch) == 0: + cluster = data.payload.listResponse.cluster[0] + else: + cluster = data.preFetch[0].response.payload.listResponse.cluster[0] + if cluster.doc[0].containerMetadata.nextPageUrl != "": + nextPath = cluster.doc[0].containerMetadata.nextPageUrl + else: + nextPath = None + apps = list(itertools.chain.from_iterable([doc.child for doc in cluster.doc])) + output += list(map(utils.fromDocToDictionary, apps)) + remaining -= len(apps) + + if len(output) > nb_result: + output = output[:nb_result] - data = self.executeRequestApi2(path) - # TODO: can response contain more than 1 cluster? - cluster = data.preFetch[0].response.payload.listResponse.cluster[0] - # cluster has more than 1 doc usually, and each doc has some - # childs representing the applications. So we chain together every child - # of every doc - apps = itertools.chain.from_iterable([doc.child for doc in cluster.doc]) - output = list(map(utils.fromDocToDictionary, apps)) return output def details(self, packageName): diff --git a/test.py b/test.py index a0d2fb6..6854eab 100644 --- a/test.py +++ b/test.py @@ -8,32 +8,32 @@ PASSWD = "fjgozwjmkwyvvutt" testApps = ['com.cpuid.cpu_z'] server = GooglePlayAPI(debug=True) -try: - print('\nLogging in with email and password\n') - server.login(EMAIL, PASSWD, None, None) - gsfId = server.gsfId - authSubToken = server.authSubToken +print('\nLogging in with email and password\n') +server.login(EMAIL, PASSWD, None, None) +gsfId = server.gsfId +authSubToken = server.authSubToken - print('\nNow trying secondary login with ac2dm token and gsfId saved\n') - server = GooglePlayAPI(debug=True) - server.login(None, None, gsfId, authSubToken) +print('\nNow trying secondary login with ac2dm token and gsfId saved\n') +server = GooglePlayAPI(debug=True) +server.login(None, None, gsfId, authSubToken) - apps = server.search('telegram', 1, None) - print('\nFound those apps:\n') - for a in apps: - print(a['docId']) - docid = apps[0]['docId'] - version = apps[0]['versionCode'] - print('\nTelegram docid is: %s\n' % docid) - print('\nAttempting to download %s\n' % docid) - fl = server.download(docid, version) - with open(docid + '.apk', 'wb') as f: - f.write(fl) - print('\nDownload successful\n') - f.close() - print('\nGetting details for %s\n' % testApps[0]) - bulk = server.bulkDetails(testApps) - print(bulk) -except Exception as e: - print(str(e)) - sys.exit(1) +apps = server.search('telegram', 34, None) + +print('nb_result: 34') +print('number of results: %d' % len(apps)) + +print('\nFound those apps:\n') +for a in apps: + print(a['docId']) +docid = apps[0]['docId'] +version = apps[0]['versionCode'] +print('\nTelegram docid is: %s\n' % docid) +print('\nAttempting to download %s\n' % docid) +fl = server.download(docid, version) +with open(docid + '.apk', 'wb') as f: + f.write(fl) + print('\nDownload successful\n') + f.close() +print('\nGetting details for %s\n' % testApps[0]) +bulk = server.bulkDetails(testApps) +print(bulk)