Fixed browse() and list() functions + update test

Signed-off-by: Domenico Iezzi <domenico.iezzi.201@gmail.com>
This commit is contained in:
Domenico Iezzi 2017-10-06 11:53:53 +02:00
parent cf05125cb8
commit cfc2de89ce
2 changed files with 63 additions and 23 deletions

View File

@ -262,9 +262,8 @@ class GooglePlayAPI(object):
else: else:
response = requests.get(url, headers=headers, response = requests.get(url, headers=headers,
verify=ssl_verify) verify=ssl_verify)
data = response.content
message = googleplay_pb2.ResponseWrapper.FromString(data) message = googleplay_pb2.ResponseWrapper.FromString(response.content)
if message.commands.displayErrorMessage != "": if message.commands.displayErrorMessage != "":
raise RequestError(message.commands.displayErrorMessage) raise RequestError(message.commands.displayErrorMessage)
self._try_register_preFetch(message) self._try_register_preFetch(message)
@ -329,31 +328,40 @@ class GooglePlayAPI(object):
return result return result
def browse(self, cat=None, subCat=None): def browse(self, cat=None, subCat=None):
"""Browse categories. """Browse categories. If neither cat nor subcat are specified,
cat (category ID) and ctr (subcategory ID) are used as filters.""" return a list of categories, otherwise it return a list of apps
using cat (category ID) and subCat (subcategory ID) as filters."""
path = "browse?c=3" path = "browse?c=3"
if cat is not None: if cat is not None:
path += "&cat=%s" % requests.utils.quote(cat) path += "&cat=%s" % requests.utils.quote(cat)
if subCat is not None: if subCat is not None:
path += "&ctr=%s" % requests.utils.quote(subCat) path += "&ctr=%s" % requests.utils.quote(subCat)
data = self.executeRequestApi2(path) data = self.executeRequestApi2(path)
output = {} output = []
if len(data.preFetch) > 0: if cat is None and subCat is None:
# get Play Store showcase categories # result contains all categories available
# (like Top Trending, Recently Updated ...) for cat in data.payload.browseResponse.category:
for preFetch in data.preFetch: elem = { 'name': cat.name,
doc = preFetch.response.payload.listResponse.cluster[0].doc 'dataUrl': cat.dataUrl,
if len(doc) == 0: 'catId': cat.unknownCategoryContainer.categoryIdContainer.categoryId }
continue output.append(elem)
categoryTitle = doc[0].title else:
output[categoryTitle] = list(map(utils.fromDocToDictionary, # result contains apps of a specific category
[apps for apps in doc[0].child])) # organized by sections
for pf in data.preFetch:
for cluster in pf.response.payload.listResponse.cluster:
for doc in cluster.doc:
section = { 'title': doc.title,
'docid': doc.docid,
'apps': list(map(utils.fromDocToDictionary,
[a for a in doc.child])) }
output.append(section)
return output return output
def list(self, cat, ctr=None, nb_results=None, offset=None): def list(self, cat, ctr=None, nb_results=None, offset=None):
"""List apps. """List apps for a specfic category *cat*.
If ctr (subcategory ID) is None, returns a list of valid subcategories. If ctr (subcategory ID) is None, returns a list of valid subcategories.
@ -365,8 +373,22 @@ class GooglePlayAPI(object):
path += "&n=%s" % requests.utils.quote(nb_results) path += "&n=%s" % requests.utils.quote(nb_results)
if offset is not None: if offset is not None:
path += "&o=%s" % requests.utils.quote(offset) path += "&o=%s" % requests.utils.quote(offset)
message = self.executeRequestApi2(path) data = self.executeRequestApi2(path)
return message.payload.listResponse output = []
if ctr is None:
# list subcategories
for pf in data.preFetch:
for cluster in pf.response.payload.listResponse.cluster:
for doc in cluster.doc:
output.append(doc.docid)
else:
# list apps for specific subcat
for cluster in data.payload.listResponse.cluster:
for doc in cluster.doc:
output += list(map(utils.fromDocToDictionary,
[a for a in doc.child]))
return output
def reviews(self, packageName, filterByDevice=False, sort=2, def reviews(self, packageName, filterByDevice=False, sort=2,
nb_results=None, offset=None): nb_results=None, offset=None):

28
test.py
View File

@ -21,7 +21,7 @@ server.login(None, None, gsfId, authSubToken)
# SEARCH # SEARCH
apps = server.search('telegram', 34, None) apps = server.search('termux', 34, None)
print('nb_result: 34') print('nb_result: 34')
print('number of results: %d' % len(apps)) print('number of results: %d' % len(apps))
@ -35,7 +35,7 @@ for a in apps:
docid = apps[0]['docId'] docid = apps[0]['docId']
version = apps[0]['versionCode'] version = apps[0]['versionCode']
print('\nTelegram docid is: %s\n' % docid) print('\nTermux docid is: %s\n' % docid)
print('\nAttempting to download %s\n' % docid) print('\nAttempting to download %s\n' % docid)
fl = server.download(docid, version) fl = server.download(docid, version)
with open(docid + '.apk', 'wb') as f: with open(docid + '.apk', 'wb') as f:
@ -48,10 +48,28 @@ with open(docid + '.apk', 'wb') as f:
print('\nGetting details for %s\n' % testApps[0]) print('\nGetting details for %s\n' % testApps[0])
bulk = server.bulkDetails(testApps) bulk = server.bulkDetails(testApps)
print(bulk) print(bulk)
print()
# BROWSE # BROWSE
browse = server.browse(cat='MUSIC_AND_AUDIO') print('\nBrowse play store categories\n')
print(browse.keys()) browse = server.browse()
for b in browse:
print(b['name'])
print('\nBrowsing the %s category\n' % browse[0]['catId'])
browseCat = server.browse(browse[0]['catId'])
for b in browseCat:
print('%s subcategory with %d apps' % (b['title'],len(b['apps'])))
# LIST
cat = 'MUSIC_AND_AUDIO'
print('\nList %s subcategories\n' % cat)
catList = server.list(cat)
for c in catList:
print(c)
print('\nList %s apps for %s category\n' % (catList[0],cat))
appList = server.list(cat, catList[0])
for app in appList:
print(app['docId'])