Helper functions for response parsing

This commit is contained in:
Domenico Iezzi 2017-12-09 10:06:34 +01:00
parent 6a6c0b01d0
commit 55499a015e
No known key found for this signature in database
GPG Key ID: 7AC94D5DDA2FB7EE
2 changed files with 43 additions and 21 deletions

View File

@ -63,7 +63,6 @@ class GooglePlayAPI(object):
self.deviceBuilder.device['simoperator'] = sim_operator
if cell_operator is not None:
self.deviceBuilder.device['celloperator'] = cell_operator
# save last response text for error logging
def encrypt_password(self, login, passwd):
"""Encrypt the password using the google publickey, using
@ -298,32 +297,31 @@ class GooglePlayAPI(object):
while remaining > 0 and nextPath is not None:
currentPath = nextPath
data = self.executeRequestApi2(currentPath)
if len(data.preFetch) > 0:
if utils.hasPrefetch(data):
response = data.preFetch[0].response
else:
response = data
if response.payload.HasField('searchResponse'):
if utils.hasSearchResponse(response.payload):
# we still need to fetch the first page, so go to
# next loop iteration without decrementing counter
nextPath = response.payload.searchResponse.nextPageUrl
continue
if len(response.payload.listResponse.cluster) == 0:
# strange behaviour, probably due to
# expired token
raise LoginError('Unexpected behaviour, probably expired '
'token')
cluster = response.payload.listResponse.cluster[0]
if len(cluster.doc) == 0:
print('No results for query %s' % query)
break
if cluster.doc[0].containerMetadata.nextPageUrl != "":
nextPath = cluster.doc[0].containerMetadata.nextPageUrl
else:
nextPath = None
apps = list(chain.from_iterable([doc.child for doc in cluster.doc]))
output += list(map(utils.fromDocToDictionary, apps))
remaining -= len(apps)
if utils.hasListResponse(response.payload):
cluster = response.payload.listResponse.cluster
if len(cluster) == 0:
# strange behaviour, probably due to expired token
raise LoginError('Unexpected behaviour, probably expired '
'token')
cluster = cluster[0]
if len(cluster.doc) == 0:
break
if cluster.doc[0].containerMetadata.nextPageUrl != "":
nextPath = cluster.doc[0].containerMetadata.nextPageUrl
else:
nextPath = None
apps = list(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]

View File

@ -1,9 +1,9 @@
import struct
import sys
from . import googleplay_pb2
VERSION = sys.version_info[0]
def fromDocToDictionary(app):
return {"docId": app.docid,
"title": app.title,
@ -68,3 +68,27 @@ def toBigInt(byteArray):
decoded = struct.unpack("B", value)[0]
out = out | decoded << key * 8
return out
def hasPrefetch(response):
if type(response) is not googleplay_pb2.ResponseWrapper:
return False
try:
return response.HasField('preFetch')
except ValueError:
return False
def hasListResponse(payload):
if type(payload) is not googleplay_pb2.Payload:
return False
try:
return payload.HasField('listResponse')
except ValueError:
return False
def hasSearchResponse(payload):
if type(payload) is not googleplay_pb2.Payload:
return False
try:
return payload.HasField('searchResponse')
except ValueError:
return False