Some code optimizations

Changed nested for loops with list comprehensions and maps. Also some
small fixes to indentation
This commit is contained in:
Domenico Iezzi 2017-11-11 21:59:14 +01:00
parent 6a9f29c6ce
commit ac2d07a8c9
No known key found for this signature in database
GPG Key ID: 7AC94D5DDA2FB7EE

View File

@ -344,8 +344,7 @@ class GooglePlayAPI(object):
packageName is the app unique ID (usually starting with 'com.').""" packageName is the app unique ID (usually starting with 'com.')."""
path = "details?doc=%s" % requests.utils.quote(packageName) path = "details?doc=%s" % requests.utils.quote(packageName)
data = self.executeRequestApi2(path) data = self.executeRequestApi2(path)
app = utils.fromDocToDictionary(data.payload.detailsResponse.docV2) return utils.fromDocToDictionary(data.payload.detailsResponse.docV2)
return app
def bulkDetails(self, packageNames): def bulkDetails(self, packageNames):
"""Get several apps details from a list of package names. """Get several apps details from a list of package names.
@ -369,14 +368,9 @@ class GooglePlayAPI(object):
data.decode("utf-8"), data.decode("utf-8"),
"application/x-protobuf") "application/x-protobuf")
response = message.payload.bulkDetailsResponse response = message.payload.bulkDetailsResponse
result = [] return [None if not entry.HasField('doc') else
for entry in response.entry: utils.fromDocToDictionary(entry.doc)
if not entry.HasField('doc'): for entry in response.entry]
result.append(None)
else:
appDetails = utils.fromDocToDictionary(entry.doc)
result.append(appDetails)
return result
def browse(self, cat=None, subCat=None): def browse(self, cat=None, subCat=None):
"""Browse categories. If neither cat nor subcat are specified, """Browse categories. If neither cat nor subcat are specified,
@ -392,11 +386,10 @@ class GooglePlayAPI(object):
if cat is None and subCat is None: if cat is None and subCat is None:
# result contains all categories available # result contains all categories available
for cat in data.payload.browseResponse.category: return [{'name': c.name,
elem = {'name': cat.name, 'dataUrl': c.dataUrl,
'dataUrl': cat.dataUrl, 'catId': c.unknownCategoryContainer.categoryIdContainer.categoryId}
'catId': cat.unknownCategoryContainer.categoryIdContainer.categoryId} for c in data.payload.browseResponse.category]
output.append(elem)
else: else:
# result contains apps of a specific category # result contains apps of a specific category
# organized by sections # organized by sections
@ -426,22 +419,19 @@ class GooglePlayAPI(object):
if offset is not None: if offset is not None:
path += "&o=%s" % requests.utils.quote(offset) path += "&o=%s" % requests.utils.quote(offset)
data = self.executeRequestApi2(path) data = self.executeRequestApi2(path)
output = []
if ctr is None: if ctr is None:
# list subcategories # list subcategories
for pf in data.preFetch: clusters = chain.from_iterable([pf.response.payload.listResponse.cluster
for cluster in pf.response.payload.listResponse.cluster: for pf in data.preFetch])
for doc in cluster.doc: docs = chain.from_iterable([c.doc for c in clusters])
output.append(doc.docid) return [d.docid for d in docs]
else: else:
# list apps for specific subcat # list apps for specific subcat
for cluster in data.payload.listResponse.cluster: docs = chain.from_iterable([c.doc for c in
for doc in cluster.doc: data.payload.listResponse.cluster])
apps = [a for a in doc.child] childs = chain.from_iterable([d.child for d in docs])
apps = list(map(utils.fromDocToDictionary, return [utils.fromDocToDictionary(c)
apps)) for c in childs]
output += apps
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):
@ -485,9 +475,9 @@ class GooglePlayAPI(object):
def _deliver_data(self, url, cookies, progress_bar): def _deliver_data(self, url, cookies, progress_bar):
headers = self.getDefaultHeaders() headers = self.getDefaultHeaders()
if not progress_bar: if not progress_bar:
return requests.get(url, headers=headers, return requests.get(url, headers=headers,
cookies=cookies, verify=ssl_verify, cookies=cookies, verify=ssl_verify,
timeout=60).content timeout=60).content
response_content = bytes() response_content = bytes()
response = requests.get(url, headers=headers, response = requests.get(url, headers=headers,
cookies=cookies, verify=ssl_verify, cookies=cookies, verify=ssl_verify,
@ -599,11 +589,9 @@ class GooglePlayAPI(object):
path = "purchase" path = "purchase"
headers = self.getDefaultHeaders() headers = self.getDefaultHeaders()
params = { params = {'ot': str(offerType),
'ot': str(offerType), 'doc': packageName,
'doc': packageName, 'vc': str(versionCode)}
'vc': str(versionCode)
}
url = self.FDFE + path url = self.FDFE + path
response = requests.post(url, headers=headers, response = requests.post(url, headers=headers,
params=params, verify=ssl_verify, params=params, verify=ssl_verify,