Skip to content

Commit

Permalink
Improvements to helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Domenico Iezzi committed Dec 9, 2017
1 parent 55499a0 commit 8dc2eb5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
37 changes: 21 additions & 16 deletions gpapi/googleplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def bulkDetails(self, packageNames):
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"
Expand All @@ -358,7 +358,7 @@ def bulkDetails(self, packageNames):
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]

Expand All @@ -372,27 +372,32 @@ def browse(self, cat=None, subCat=None):
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
return [{'name': c.name,
'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):
Expand Down
31 changes: 19 additions & 12 deletions gpapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8dc2eb5

Please sign in to comment.