2017-08-27 15:32:05 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
|
2017-09-15 20:11:44 +00:00
|
|
|
from Crypto.Util import asn1
|
|
|
|
from Crypto.PublicKey import RSA
|
|
|
|
from Crypto.Hash import SHA
|
|
|
|
from Crypto.Cipher import PKCS1_OAEP
|
2017-10-07 16:59:23 +00:00
|
|
|
from clint.textui import progress
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-09-18 14:33:11 +00:00
|
|
|
import requests
|
2017-09-15 20:11:44 +00:00
|
|
|
import base64
|
2017-09-18 13:49:42 +00:00
|
|
|
import itertools
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-09-21 09:19:48 +00:00
|
|
|
from . import googleplay_pb2, config, utils
|
2017-09-18 14:33:11 +00:00
|
|
|
|
2017-08-27 15:32:05 +00:00
|
|
|
ssl_verify = True
|
|
|
|
|
|
|
|
|
|
|
|
class LoginError(Exception):
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return repr(self.value)
|
|
|
|
|
|
|
|
|
|
|
|
class RequestError(Exception):
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return repr(self.value)
|
|
|
|
|
|
|
|
|
|
|
|
class GooglePlayAPI(object):
|
|
|
|
"""Google Play Unofficial API Class
|
|
|
|
|
|
|
|
Usual APIs methods are login(), search(), details(), bulkDetails(),
|
2017-09-16 17:12:06 +00:00
|
|
|
download(), browse(), reviews() and list()."""
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-09-17 10:29:42 +00:00
|
|
|
BASE = "https://android.clients.google.com/"
|
|
|
|
FDFE = BASE + "fdfe/"
|
|
|
|
UPLOADURL = FDFE + "uploadDeviceConfig"
|
|
|
|
SEARCHURL = FDFE + "search"
|
|
|
|
CHECKINURL = BASE + "checkin"
|
|
|
|
AUTHURL = BASE + "auth"
|
|
|
|
ACCOUNT = "HOSTED_OR_GOOGLE"
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-09-16 11:17:21 +00:00
|
|
|
def __init__(self, debug=False):
|
2017-10-12 17:46:20 +00:00
|
|
|
self.authSubToken = None
|
|
|
|
self.gsfId = None
|
2017-09-16 11:17:21 +00:00
|
|
|
self.lang = config.LANG
|
2017-08-27 15:32:05 +00:00
|
|
|
self.debug = debug
|
|
|
|
|
2017-09-08 14:26:37 +00:00
|
|
|
def encrypt_password(self, login, passwd):
|
2017-09-17 10:29:42 +00:00
|
|
|
"""Encrypt the password using the google publickey, using
|
|
|
|
the RSA encryption algorithm"""
|
2017-09-08 14:26:37 +00:00
|
|
|
|
2017-09-15 20:11:44 +00:00
|
|
|
binaryKey = base64.b64decode(config.GOOGLE_PUBKEY)
|
2017-09-18 14:33:11 +00:00
|
|
|
i = utils.readInt(binaryKey, 0)
|
|
|
|
modulus = utils.toBigInt(binaryKey[4:][0:i])
|
|
|
|
j = utils.readInt(binaryKey, i+4)
|
|
|
|
exponent = utils.toBigInt(binaryKey[i+8:][0:j])
|
2017-09-15 20:11:44 +00:00
|
|
|
|
|
|
|
seq = asn1.DerSequence()
|
|
|
|
seq.append(modulus)
|
|
|
|
seq.append(exponent)
|
|
|
|
|
|
|
|
publicKey = RSA.importKey(seq.encode())
|
|
|
|
cipher = PKCS1_OAEP.new(publicKey)
|
|
|
|
combined = login.encode() + b'\x00' + passwd.encode()
|
|
|
|
encrypted = cipher.encrypt(combined)
|
|
|
|
h = b'\x00' + SHA.new(binaryKey).digest()[0:4]
|
|
|
|
return base64.urlsafe_b64encode(h + encrypted)
|
2017-09-08 14:26:37 +00:00
|
|
|
|
2017-08-27 15:32:05 +00:00
|
|
|
def setAuthSubToken(self, authSubToken):
|
|
|
|
self.authSubToken = authSubToken
|
|
|
|
|
|
|
|
# put your auth token in config.py to avoid multiple login requests
|
|
|
|
if self.debug:
|
|
|
|
print("authSubToken: " + authSubToken)
|
|
|
|
|
2017-09-15 20:11:44 +00:00
|
|
|
def getDefaultHeaders(self):
|
2017-09-17 10:29:42 +00:00
|
|
|
"""Return the default set of request headers, which
|
2017-09-15 20:11:44 +00:00
|
|
|
can later be updated, based on the request type"""
|
|
|
|
|
|
|
|
headers = {
|
2017-10-12 17:46:20 +00:00
|
|
|
"Accept-Language": config.LANG.replace('_', '-'),
|
|
|
|
"X-DFE-Encoded-Targets": config.DFE_TARGETS,
|
|
|
|
"User-Agent": config.getUserAgent()
|
2017-09-15 20:11:44 +00:00
|
|
|
}
|
|
|
|
if self.gsfId is not None:
|
|
|
|
headers["X-DFE-Device-Id"] = "{0:x}".format(self.gsfId)
|
|
|
|
if self.authSubToken is not None:
|
|
|
|
headers["Authorization"] = "GoogleLogin auth=%s" % self.authSubToken
|
|
|
|
return headers
|
|
|
|
|
2017-10-09 19:34:40 +00:00
|
|
|
def checkin(self, email, ac2dmToken, device_codename):
|
2017-09-15 20:11:44 +00:00
|
|
|
headers = self.getDefaultHeaders()
|
|
|
|
headers["Content-Type"] = "application/x-protobuffer"
|
|
|
|
|
2017-10-09 19:34:40 +00:00
|
|
|
request = config.getAndroidCheckinRequest(device_codename)
|
2017-09-15 20:11:44 +00:00
|
|
|
|
|
|
|
stringRequest = request.SerializeToString()
|
2017-09-17 10:29:42 +00:00
|
|
|
res = requests.post(self.CHECKINURL, data=stringRequest,
|
|
|
|
headers=headers, verify=ssl_verify)
|
2017-09-15 20:11:44 +00:00
|
|
|
response = googleplay_pb2.AndroidCheckinResponse()
|
|
|
|
response.ParseFromString(res.content)
|
|
|
|
|
|
|
|
# checkin again to upload gfsid
|
|
|
|
request2 = googleplay_pb2.AndroidCheckinRequest()
|
|
|
|
request2.CopyFrom(request)
|
|
|
|
request2.id = response.androidId
|
|
|
|
request2.securityToken = response.securityToken
|
|
|
|
request2.accountCookie.append("[" + email + "]")
|
2017-10-04 09:32:56 +00:00
|
|
|
request2.accountCookie.append(ac2dmToken)
|
2017-09-15 20:11:44 +00:00
|
|
|
stringRequest = request2.SerializeToString()
|
2017-10-10 16:35:40 +00:00
|
|
|
requests.post(self.CHECKINURL, data=stringRequest,
|
|
|
|
headers=headers, verify=ssl_verify)
|
2017-09-15 20:11:44 +00:00
|
|
|
|
|
|
|
return response.androidId
|
|
|
|
|
|
|
|
def uploadDeviceConfig(self):
|
2017-09-17 10:29:42 +00:00
|
|
|
"""Upload the device configuration defined in the file
|
|
|
|
*device.properties* to the google account. Default device is a Google Nexus 6P"""
|
|
|
|
|
2017-09-15 20:11:44 +00:00
|
|
|
upload = googleplay_pb2.UploadDeviceConfigRequest()
|
2017-09-16 17:12:06 +00:00
|
|
|
upload.deviceConfiguration.CopyFrom(config.getDeviceConfig())
|
2017-09-15 20:11:44 +00:00
|
|
|
headers = self.getDefaultHeaders()
|
|
|
|
headers["X-DFE-Enabled-Experiments"] = "cl:billing.select_add_instrument_by_default"
|
|
|
|
headers["X-DFE-Unsupported-Experiments"] = "nocache:billing.use_charging_poller,market_emails,buyer_currency,prod_baseline,checkin.set_asset_paid_app_field,shekel_test,content_ratings,buyer_currency_in_app,nocache:encrypted_apk,recent_changes"
|
|
|
|
headers["X-DFE-Client-Id"] = "am-android-google"
|
|
|
|
headers["X-DFE-SmallestScreenWidthDp"] = "320"
|
|
|
|
headers["X-DFE-Filter-Level"] = "3"
|
|
|
|
stringRequest = upload.SerializeToString()
|
2017-09-17 10:29:42 +00:00
|
|
|
res = requests.post(self.UPLOADURL, data=stringRequest,
|
2017-09-15 20:11:44 +00:00
|
|
|
headers=headers, verify=ssl_verify)
|
2017-10-10 16:35:40 +00:00
|
|
|
googleplay_pb2.ResponseWrapper.FromString(res.content)
|
2017-09-15 20:11:44 +00:00
|
|
|
|
2017-10-09 19:34:40 +00:00
|
|
|
def login(self, email=None, password=None, gsfId=None, authSubToken=None, device_codename='angler'):
|
2017-09-16 11:17:21 +00:00
|
|
|
"""Login to your Google Account.
|
|
|
|
For first time login you should provide:
|
|
|
|
* email
|
|
|
|
* password
|
2017-10-04 09:32:56 +00:00
|
|
|
For the following logins you need to provide:
|
|
|
|
* gsfId
|
|
|
|
* authSubToken"""
|
|
|
|
if email is not None and password is not None:
|
2017-09-16 11:17:21 +00:00
|
|
|
# First time setup, where we obtain an ac2dm token and
|
|
|
|
# upload device information
|
2017-09-15 20:11:44 +00:00
|
|
|
|
2017-10-04 09:32:56 +00:00
|
|
|
encryptedPass = self.encrypt_password(email, password).decode('utf-8')
|
2017-09-15 20:11:44 +00:00
|
|
|
# AC2DM token
|
2017-08-27 15:32:05 +00:00
|
|
|
params = {
|
|
|
|
"Email": email,
|
2017-09-15 20:11:44 +00:00
|
|
|
"EncryptedPasswd": encryptedPass,
|
|
|
|
"service": "ac2dm",
|
|
|
|
"add_account": "1",
|
2017-09-17 10:29:42 +00:00
|
|
|
"accountType": self.ACCOUNT,
|
2017-08-27 15:32:05 +00:00
|
|
|
"has_permission": "1",
|
2017-09-15 20:11:44 +00:00
|
|
|
"app": "com.google.android.gsf",
|
2017-08-27 15:32:05 +00:00
|
|
|
"source": "android",
|
|
|
|
"device_country": "en",
|
|
|
|
"lang": self.lang,
|
2017-09-15 20:11:44 +00:00
|
|
|
"sdk_version": "25",
|
|
|
|
"client_sig": "38918a453d07199354f8b19af05ec6562ced5788"
|
2017-08-27 15:32:05 +00:00
|
|
|
}
|
2017-09-17 10:29:42 +00:00
|
|
|
response = requests.post(self.AUTHURL, data=params, verify=ssl_verify)
|
2017-08-27 15:32:05 +00:00
|
|
|
data = response.text.split()
|
|
|
|
params = {}
|
|
|
|
for d in data:
|
|
|
|
if "=" not in d:
|
|
|
|
continue
|
|
|
|
k, v = d.split("=")[0:2]
|
|
|
|
params[k.strip().lower()] = v.strip()
|
|
|
|
if "auth" in params:
|
2017-10-04 09:32:56 +00:00
|
|
|
ac2dmToken = params["auth"]
|
2017-08-27 15:32:05 +00:00
|
|
|
elif "error" in params:
|
2017-10-13 09:20:40 +00:00
|
|
|
if "NeedsBrowser" in params["error"]:
|
2017-10-13 15:00:10 +00:00
|
|
|
raise LoginError("Security check is needed, try to visit "
|
|
|
|
"https://accounts.google.com/b/0/DisplayUnlockCaptcha "
|
|
|
|
"to unlock, or setup an app-specific password")
|
2017-08-27 15:32:05 +00:00
|
|
|
raise LoginError("server says: " + params["error"])
|
|
|
|
else:
|
|
|
|
raise LoginError("Auth token not found.")
|
|
|
|
|
2017-10-09 19:34:40 +00:00
|
|
|
self.gsfId = self.checkin(email, ac2dmToken, device_codename)
|
2017-09-16 17:12:06 +00:00
|
|
|
if self.debug:
|
|
|
|
print("Google Services Framework Id: %s" % "{0:x}".format(self.gsfId))
|
2017-09-15 20:11:44 +00:00
|
|
|
self.getAuthSubToken(email, encryptedPass)
|
2017-09-16 17:12:06 +00:00
|
|
|
if self.debug:
|
|
|
|
print("Uploading device configuration")
|
2017-09-15 20:11:44 +00:00
|
|
|
self.uploadDeviceConfig()
|
2017-10-04 09:32:56 +00:00
|
|
|
elif gsfId is not None and authSubToken is not None:
|
|
|
|
# no need to initialize API
|
|
|
|
self.gsfId = gsfId
|
|
|
|
self.setAuthSubToken(authSubToken)
|
|
|
|
# check if token is valid with a simple search
|
|
|
|
self.search('firefox', 1, None)
|
|
|
|
else:
|
|
|
|
raise LoginError('Either (email,pass) or (gsfId, authSubToken) is needed')
|
2017-09-15 20:11:44 +00:00
|
|
|
|
|
|
|
def getAuthSubToken(self, email, passwd):
|
|
|
|
params = {
|
|
|
|
"Email": email,
|
|
|
|
"EncryptedPasswd": passwd,
|
2017-09-17 10:29:42 +00:00
|
|
|
"accountType": self.ACCOUNT,
|
2017-09-15 20:11:44 +00:00
|
|
|
"has_permission": "1",
|
|
|
|
"source": "android",
|
|
|
|
"device_country": "en",
|
|
|
|
"service": "androidmarket",
|
|
|
|
"app": "com.android.vending",
|
|
|
|
"lang": self.lang,
|
|
|
|
"sdk_version": "25",
|
|
|
|
"client_sig": "38918a453d07199354f8b19af05ec6562ced5788"
|
|
|
|
}
|
2017-09-17 10:29:42 +00:00
|
|
|
response = requests.post(self.AUTHURL, data=params, verify=ssl_verify)
|
2017-09-15 20:11:44 +00:00
|
|
|
data = response.text.split()
|
|
|
|
params = {}
|
|
|
|
for d in data:
|
|
|
|
if "=" not in d:
|
|
|
|
continue
|
|
|
|
k, v = d.split("=")[0:2]
|
|
|
|
params[k.strip().lower()] = v.strip()
|
|
|
|
if "auth" in params:
|
|
|
|
self.setAuthSubToken(params["auth"])
|
|
|
|
elif "error" in params:
|
|
|
|
raise LoginError("server says: " + params["error"])
|
|
|
|
else:
|
|
|
|
raise LoginError("Auth token not found.")
|
|
|
|
|
2017-08-27 15:32:05 +00:00
|
|
|
def executeRequestApi2(self, path, datapost=None,
|
|
|
|
post_content_type="application/x-www-form-urlencoded; charset=UTF-8"):
|
2017-10-10 16:35:40 +00:00
|
|
|
if self.authSubToken is None:
|
2017-09-18 13:49:42 +00:00
|
|
|
raise Exception("You need to login before executing any request")
|
2017-10-08 17:36:36 +00:00
|
|
|
headers = self.getDefaultHeaders()
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-10-08 17:36:36 +00:00
|
|
|
if datapost is not None:
|
|
|
|
headers["Content-Type"] = post_content_type
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-10-08 17:36:36 +00:00
|
|
|
url = self.FDFE + path
|
|
|
|
if datapost is not None:
|
|
|
|
response = requests.post(url, data=str(datapost),
|
|
|
|
headers=headers, verify=ssl_verify)
|
|
|
|
else:
|
|
|
|
response = requests.get(url, headers=headers,
|
|
|
|
verify=ssl_verify)
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-10-06 09:53:53 +00:00
|
|
|
message = googleplay_pb2.ResponseWrapper.FromString(response.content)
|
2017-09-18 13:49:42 +00:00
|
|
|
if message.commands.displayErrorMessage != "":
|
2017-09-18 14:33:11 +00:00
|
|
|
raise RequestError(message.commands.displayErrorMessage)
|
2017-08-27 15:32:05 +00:00
|
|
|
|
|
|
|
return message
|
|
|
|
|
2017-09-16 11:17:21 +00:00
|
|
|
def search(self, query, nb_result, offset=None):
|
2017-10-08 17:36:36 +00:00
|
|
|
""" Search the play store for an app.
|
|
|
|
|
|
|
|
nb_result is the maximum number of result to be returned.
|
|
|
|
|
|
|
|
offset is used to take result starting from an index.
|
|
|
|
"""
|
2017-10-10 16:35:40 +00:00
|
|
|
if self.authSubToken is None:
|
2017-09-18 13:49:42 +00:00
|
|
|
raise Exception("You need to login before executing any request")
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-10-05 18:39:24 +00:00
|
|
|
remaining = nb_result
|
|
|
|
output = []
|
|
|
|
|
|
|
|
nextPath = "search?c=3&q=%s" % requests.utils.quote(query)
|
2017-08-27 15:32:05 +00:00
|
|
|
if (offset is not None):
|
2017-10-05 18:39:24 +00:00
|
|
|
nextPath += "&o=%d" % int(offset)
|
2017-10-10 16:35:40 +00:00
|
|
|
while remaining > 0 and nextPath is not None:
|
2017-10-05 18:39:24 +00:00
|
|
|
currentPath = nextPath
|
|
|
|
data = self.executeRequestApi2(currentPath)
|
2017-10-08 17:36:36 +00:00
|
|
|
if len(data.preFetch) > 0:
|
|
|
|
response = data.preFetch[0].response
|
2017-10-05 18:39:24 +00:00
|
|
|
else:
|
2017-10-08 17:36:36 +00:00
|
|
|
response = data
|
|
|
|
if response.payload.HasField('searchResponse'):
|
|
|
|
# we still need to fetch the first page, so go to
|
|
|
|
# next loop iteration without decrementing counter
|
|
|
|
nextPath = response.payload.searchResponse.nextPageUrl
|
|
|
|
continue
|
|
|
|
|
|
|
|
cluster = response.payload.listResponse.cluster[0]
|
2017-10-05 18:39:24 +00:00
|
|
|
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]
|
|
|
|
|
2017-09-18 13:49:42 +00:00
|
|
|
return output
|
2017-08-27 15:32:05 +00:00
|
|
|
|
|
|
|
def details(self, packageName):
|
|
|
|
"""Get app details from a package name.
|
2017-10-08 17:50:29 +00:00
|
|
|
|
2017-08-27 15:32:05 +00:00
|
|
|
packageName is the app unique ID (usually starting with 'com.')."""
|
|
|
|
path = "details?doc=%s" % requests.utils.quote(packageName)
|
2017-10-08 17:50:29 +00:00
|
|
|
data = self.executeRequestApi2(path)
|
|
|
|
app = utils.fromDocToDictionary(data.payload.detailsResponse.docV2)
|
|
|
|
return app
|
2017-08-27 15:32:05 +00:00
|
|
|
|
|
|
|
def bulkDetails(self, packageNames):
|
|
|
|
"""Get several apps details from a list of package names.
|
|
|
|
|
|
|
|
This is much more efficient than calling N times details() since it
|
|
|
|
requires only one request.
|
|
|
|
|
|
|
|
packageNames is a list of app ID (usually starting with 'com.')."""
|
2017-09-18 14:33:11 +00:00
|
|
|
|
2017-08-27 15:32:05 +00:00
|
|
|
path = "bulkDetails"
|
|
|
|
req = googleplay_pb2.BulkDetailsRequest()
|
|
|
|
req.docid.extend(packageNames)
|
|
|
|
data = req.SerializeToString()
|
|
|
|
message = self.executeRequestApi2(path,
|
|
|
|
data.decode("utf-8"),
|
|
|
|
"application/x-protobuf")
|
2017-09-18 14:33:11 +00:00
|
|
|
response = message.payload.bulkDetailsResponse
|
|
|
|
detailsList = [entry.doc for entry in response.entry]
|
|
|
|
result = list(map(utils.fromDocToDictionary, detailsList))
|
|
|
|
return result
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-10-05 19:51:43 +00:00
|
|
|
def browse(self, cat=None, subCat=None):
|
2017-10-06 09:53:53 +00:00
|
|
|
"""Browse categories. If neither cat nor subcat are specified,
|
|
|
|
return a list of categories, otherwise it return a list of apps
|
|
|
|
using cat (category ID) and subCat (subcategory ID) as filters."""
|
2017-08-27 15:32:05 +00:00
|
|
|
path = "browse?c=3"
|
|
|
|
if cat is not None:
|
|
|
|
path += "&cat=%s" % requests.utils.quote(cat)
|
2017-10-05 19:51:43 +00:00
|
|
|
if subCat is not None:
|
|
|
|
path += "&ctr=%s" % requests.utils.quote(subCat)
|
|
|
|
data = self.executeRequestApi2(path)
|
2017-10-06 09:53:53 +00:00
|
|
|
output = []
|
|
|
|
|
|
|
|
if cat is None and subCat is None:
|
|
|
|
# result contains all categories available
|
|
|
|
for cat in data.payload.browseResponse.category:
|
2017-10-10 16:35:40 +00:00
|
|
|
elem = {'name': cat.name,
|
|
|
|
'dataUrl': cat.dataUrl,
|
|
|
|
'catId': cat.unknownCategoryContainer.categoryIdContainer.categoryId}
|
2017-10-06 09:53:53 +00:00
|
|
|
output.append(elem)
|
|
|
|
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:
|
2017-10-10 16:35:40 +00:00
|
|
|
apps = [a for a in doc.child]
|
|
|
|
apps = list(map(utils.fromDocToDictionary,
|
|
|
|
apps))
|
|
|
|
section = {'title': doc.title,
|
|
|
|
'docid': doc.docid,
|
|
|
|
'apps': apps}
|
2017-10-06 09:53:53 +00:00
|
|
|
output.append(section)
|
2017-10-05 19:51:43 +00:00
|
|
|
return output
|
2017-08-27 15:32:05 +00:00
|
|
|
|
|
|
|
def list(self, cat, ctr=None, nb_results=None, offset=None):
|
2017-10-06 09:53:53 +00:00
|
|
|
"""List apps for a specfic category *cat*.
|
2017-08-27 15:32:05 +00:00
|
|
|
|
|
|
|
If ctr (subcategory ID) is None, returns a list of valid subcategories.
|
|
|
|
|
|
|
|
If ctr is provided, list apps within this subcategory."""
|
|
|
|
path = "list?c=3&cat=%s" % requests.utils.quote(cat)
|
|
|
|
if ctr is not None:
|
|
|
|
path += "&ctr=%s" % requests.utils.quote(ctr)
|
|
|
|
if nb_results is not None:
|
|
|
|
path += "&n=%s" % requests.utils.quote(nb_results)
|
|
|
|
if offset is not None:
|
|
|
|
path += "&o=%s" % requests.utils.quote(offset)
|
2017-10-06 09:53:53 +00:00
|
|
|
data = self.executeRequestApi2(path)
|
|
|
|
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:
|
2017-10-10 16:35:40 +00:00
|
|
|
apps = [a for a in doc.child]
|
|
|
|
apps = list(map(utils.fromDocToDictionary,
|
|
|
|
apps))
|
|
|
|
output += apps
|
2017-10-06 09:53:53 +00:00
|
|
|
return output
|
2017-08-27 15:32:05 +00:00
|
|
|
|
|
|
|
def reviews(self, packageName, filterByDevice=False, sort=2,
|
|
|
|
nb_results=None, offset=None):
|
|
|
|
"""Browse reviews.
|
|
|
|
packageName is the app unique ID.
|
|
|
|
If filterByDevice is True, return only reviews for your device."""
|
|
|
|
path = "rev?doc=%s&sort=%d" % (requests.utils.quote(packageName), sort)
|
|
|
|
if (nb_results is not None):
|
|
|
|
path += "&n=%d" % int(nb_results)
|
|
|
|
if (offset is not None):
|
|
|
|
path += "&o=%d" % int(offset)
|
|
|
|
if(filterByDevice):
|
|
|
|
path += "&dfil=1"
|
2017-10-08 19:25:04 +00:00
|
|
|
data = self.executeRequestApi2(path)
|
2017-10-10 16:55:03 +00:00
|
|
|
output = []
|
|
|
|
for rev in data.payload.reviewResponse.getResponse.review:
|
|
|
|
author = {'personIdString': rev.author2.personIdString,
|
|
|
|
'personId': rev.author2.personId,
|
|
|
|
'name': rev.author2.name,
|
|
|
|
'profilePicUrl': rev.author2.urls.url,
|
|
|
|
'googlePlusUrl': rev.author2.googlePlusUrl}
|
|
|
|
review = {'documentVersion': rev.documentVersion,
|
|
|
|
'timestampMsec': rev.timestampMsec,
|
|
|
|
'starRating': rev.starRating,
|
|
|
|
'comment': rev.comment,
|
|
|
|
'commentId': rev.commentId,
|
|
|
|
'author': author}
|
|
|
|
output.append(review)
|
|
|
|
return output
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-10-07 16:21:05 +00:00
|
|
|
def delivery(self, packageName, versionCode,
|
2017-10-07 16:59:23 +00:00
|
|
|
offerType=1, downloadToken=None, progress_bar=False):
|
2017-10-07 16:21:05 +00:00
|
|
|
"""Download an already purchased app.
|
|
|
|
|
|
|
|
packageName is the app unique ID (usually starting with 'com.').
|
|
|
|
|
|
|
|
versionCode can be grabbed by using the details() method on the given
|
|
|
|
app."""
|
|
|
|
path = "delivery"
|
2017-10-10 16:35:40 +00:00
|
|
|
params = {'ot': str(offerType),
|
|
|
|
'doc': packageName,
|
|
|
|
'vc': str(versionCode)}
|
2017-10-07 16:21:05 +00:00
|
|
|
headers = self.getDefaultHeaders()
|
|
|
|
if downloadToken is not None:
|
|
|
|
params['dtok'] = downloadToken
|
|
|
|
url = "https://android.clients.google.com/fdfe/%s" % path
|
|
|
|
response = requests.get(url, headers=headers,
|
|
|
|
params=params, verify=ssl_verify)
|
|
|
|
resObj = googleplay_pb2.ResponseWrapper.FromString(response.content)
|
|
|
|
if resObj.commands.displayErrorMessage != "":
|
|
|
|
raise RequestError(resObj.commands.displayErrorMessage)
|
|
|
|
elif resObj.payload.deliveryResponse.appDeliveryData.downloadUrl == "":
|
|
|
|
raise RequestError('App not purchased')
|
|
|
|
else:
|
|
|
|
downloadUrl = resObj.payload.deliveryResponse.appDeliveryData.downloadUrl
|
|
|
|
cookie = resObj.payload.deliveryResponse.appDeliveryData.downloadAuthCookie[0]
|
|
|
|
cookies = {
|
|
|
|
str(cookie.name): str(cookie.value)
|
|
|
|
}
|
2017-10-07 16:59:23 +00:00
|
|
|
if not progress_bar:
|
|
|
|
return requests.get(downloadUrl, headers=headers,
|
2017-10-10 16:35:40 +00:00
|
|
|
cookies=cookies, verify=ssl_verify).content
|
2017-10-07 16:21:05 +00:00
|
|
|
|
2017-10-07 16:59:23 +00:00
|
|
|
response_content = bytes()
|
|
|
|
response = requests.get(downloadUrl, headers=headers, cookies=cookies, verify=ssl_verify, stream=True)
|
|
|
|
total_length = int(response.headers.get('content-length'))
|
2017-10-10 16:35:40 +00:00
|
|
|
chunk_size = 32 * (1<<10) # 32 KB
|
2017-10-08 12:43:53 +00:00
|
|
|
bar = progress.Bar(expected_size=(total_length >> 10))
|
|
|
|
for index, chunk in enumerate(response.iter_content(chunk_size=chunk_size)):
|
2017-10-07 16:59:23 +00:00
|
|
|
response_content += chunk
|
2017-10-08 12:43:53 +00:00
|
|
|
bar.show(index * chunk_size >> 10)
|
|
|
|
bar.done()
|
2017-10-07 16:59:23 +00:00
|
|
|
return response_content
|
|
|
|
|
2017-08-27 15:32:05 +00:00
|
|
|
def download(self, packageName, versionCode,
|
2017-10-07 16:59:23 +00:00
|
|
|
offerType=1, progress_bar=False):
|
2017-10-07 16:21:05 +00:00
|
|
|
"""Download an app and return its raw data (APK file). Free apps need
|
|
|
|
to be "purchased" first, in order to retrieve the download cookie.
|
|
|
|
If you want to download an already purchased app, use *delivery* method.
|
2017-08-27 15:32:05 +00:00
|
|
|
|
|
|
|
packageName is the app unique ID (usually starting with 'com.').
|
|
|
|
|
|
|
|
versionCode can be grabbed by using the details() method on the given
|
|
|
|
app."""
|
2017-09-16 12:57:21 +00:00
|
|
|
|
2017-10-10 16:35:40 +00:00
|
|
|
if self.authSubToken is None:
|
2017-09-18 13:49:42 +00:00
|
|
|
raise Exception("You need to login before executing any request")
|
2017-08-27 15:32:05 +00:00
|
|
|
|
2017-09-18 13:49:42 +00:00
|
|
|
path = "purchase"
|
2017-09-16 12:57:21 +00:00
|
|
|
headers = self.getDefaultHeaders()
|
|
|
|
params = {
|
|
|
|
'ot': str(offerType),
|
|
|
|
'doc': packageName,
|
|
|
|
'vc': str(versionCode)
|
2017-08-27 15:32:05 +00:00
|
|
|
}
|
2017-09-17 10:29:42 +00:00
|
|
|
url = self.FDFE + path
|
2017-09-16 12:57:21 +00:00
|
|
|
response = requests.post(url, headers=headers,
|
|
|
|
params=params, verify=ssl_verify)
|
|
|
|
|
|
|
|
resObj = googleplay_pb2.ResponseWrapper.FromString(response.content)
|
2017-09-16 17:12:06 +00:00
|
|
|
if resObj.commands.displayErrorMessage != "":
|
2017-09-18 14:33:11 +00:00
|
|
|
raise RequestError(resObj.commands.displayErrorMessage)
|
2017-09-16 12:57:21 +00:00
|
|
|
else:
|
|
|
|
dlToken = resObj.payload.buyResponse.downloadToken
|
2017-10-07 16:21:05 +00:00
|
|
|
return self.delivery(packageName, versionCode,
|
2017-10-07 16:59:23 +00:00
|
|
|
offerType, dlToken, progress_bar=progress_bar)
|
2017-09-16 12:57:21 +00:00
|
|
|
|
2017-10-09 19:34:40 +00:00
|
|
|
@staticmethod
|
|
|
|
def getDevicesCodenames():
|
|
|
|
return config.getDevicesCodenames()
|
2017-10-10 17:23:58 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getDevicesReadableNames():
|
|
|
|
return config.getDevicesReadableNames()
|