Dinamically change locale and timezone

During DeviceBuilder initialization, retrieve locale from system using
python `locale` module, and set timezone to `Europe/Berlin`. Added some
helpers to get and set those values at runtime.
This commit is contained in:
Domenico Iezzi 2017-10-30 17:30:10 +01:00
parent 0227cb1cf2
commit e1acc14424
No known key found for this signature in database
GPG Key ID: 7AC94D5DDA2FB7EE
2 changed files with 33 additions and 18 deletions

View File

@ -1,9 +1,10 @@
from . import googleplay_pb2 from . import googleplay_pb2
import time from time import time
import os from os import path
import sys from sys import version_info
from locale import getdefaultlocale
VERSION = sys.version_info[0] VERSION = version_info[0]
if VERSION == 2: if VERSION == 2:
import ConfigParser import ConfigParser
else: else:
@ -11,8 +12,6 @@ else:
DFE_TARGETS = "CAEScFfqlIEG6gUYogFWrAISK1WDAg+hAZoCDgIU1gYEOIACFkLMAeQBnASLATlASUuyAyqCAjY5igOMBQzfA/IClwFbApUC4ANbtgKVAS7OAX8YswHFBhgDwAOPAmGEBt4OfKkB5weSB5AFASkiN68akgMaxAMSAQEBA9kBO7UBFE1KVwIDBGs3go6BBgEBAgMECQgJAQIEAQMEAQMBBQEBBAUEFQYCBgUEAwMBDwIBAgOrARwBEwMEAg0mrwESfTEcAQEKG4EBMxghChMBDwYGASI3hAEODEwXCVh/EREZA4sBYwEdFAgIIwkQcGQRDzQ2fTC2AjfVAQIBAYoBGRg2FhYFBwEqNzACJShzFFblAo0CFxpFNBzaAd0DHjIRI4sBJZcBPdwBCQGhAUd2A7kBLBVPngEECHl0UEUMtQETigHMAgUFCc0BBUUlTywdHDgBiAJ+vgKhAU0uAcYCAWQ/5ALUAw1UwQHUBpIBCdQDhgL4AY4CBQICjARbGFBGWzA1CAEMOQH+BRAOCAZywAIDyQZ2MgM3BxsoAgUEBwcHFia3AgcGTBwHBYwBAlcBggFxSGgIrAEEBw4QEqUCASsWadsHCgUCBQMD7QICA3tXCUw7ugJZAwGyAUwpIwM5AwkDBQMJA5sBCw8BNxBVVBwVKhebARkBAwsQEAgEAhESAgQJEBCZATMdzg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GLE_PUBKEY = "AAAAgMom/1a/v0lblO2Ubrt60J2gcuXSljGFQXgcyZWveWLEwo6prwgi3iJIZdodyhKZQrNWp5nKJ3srRXcUW+F1BD3baEVGcmEgqaLZUNBjm057pKRI16kB0YppeGx5qIQ5QjKzsR8ETQbKLNWgRY0QRNVz34kMJR3P/LgHax/6rmf5AAAAAwEAAQ=="
ACCOUNT = "HOSTED_OR_GOOGLE" ACCOUNT = "HOSTED_OR_GOOGLE"
@ -20,8 +19,8 @@ ACCOUNT = "HOSTED_OR_GOOGLE"
# if you want to add another phone, just create another section in # if you want to add another phone, just create another section in
# the file. Some configurations for common phones can be found here: # the file. Some configurations for common phones can be found here:
# https://github.com/yeriomin/play-store-api/tree/master/src/main/resources # https://github.com/yeriomin/play-store-api/tree/master/src/main/resources
filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), filepath = path.join(path.dirname(path.realpath(__file__)),
'device.properties') 'device.properties')
if VERSION == 2: if VERSION == 2:
config = ConfigParser.ConfigParser() config = ConfigParser.ConfigParser()
@ -49,6 +48,8 @@ class DeviceBuilder(object):
def __init__(self, device): def __init__(self, device):
self.device = {} self.device = {}
self.locale = getdefaultlocale()[0]
self.timezone = "Europe/Berlin"
for (key, value) in config.items(device): for (key, value) in config.items(device):
self.device[key] = value self.device[key] = value
@ -72,10 +73,10 @@ class DeviceBuilder(object):
"accountType": ACCOUNT, "accountType": ACCOUNT,
"has_permission": "1", "has_permission": "1",
"source": "android", "source": "android",
"device_country": LANG[0:2], "device_country": self.locale[0:2],
"service": "androidmarket", "service": "androidmarket",
"app": "com.android.vending", "app": "com.android.vending",
"lang": LANG, "lang": self.locale,
"sdk_version": self.device['build.version.sdk_int']} "sdk_version": self.device['build.version.sdk_int']}
def getLoginParams(self, email, encryptedPass): def getLoginParams(self, email, encryptedPass):
@ -87,16 +88,16 @@ class DeviceBuilder(object):
"has_permission": "1", "has_permission": "1",
"app": "com.google.android.gsf", "app": "com.google.android.gsf",
"source": "android", "source": "android",
"device_country": LANG[0:2], "device_country": self.locale[0:2],
"lang": LANG, "lang": self.locale,
"sdk_version": self.device['build.version.sdk_int']} "sdk_version": self.device['build.version.sdk_int']}
def getAndroidCheckinRequest(self): def getAndroidCheckinRequest(self):
request = googleplay_pb2.AndroidCheckinRequest() request = googleplay_pb2.AndroidCheckinRequest()
request.id = 0 request.id = 0
request.checkin.CopyFrom(self.getAndroidCheckin()) request.checkin.CopyFrom(self.getAndroidCheckin())
request.locale = LANG request.locale = self.locale
request.timeZone = TIMEZONE request.timeZone = self.timezone
request.version = 3 request.version = 3
request.deviceConfiguration.CopyFrom(self.getDeviceConfig()) request.deviceConfiguration.CopyFrom(self.getDeviceConfig())
request.fragment = 0 request.fragment = 0
@ -148,7 +149,7 @@ class DeviceBuilder(object):
androidBuild.buildProduct = self.device['build.product'] androidBuild.buildProduct = self.device['build.product']
androidBuild.client = self.device['client'] androidBuild.client = self.device['client']
androidBuild.otaInstalled = False androidBuild.otaInstalled = False
androidBuild.timestamp = int(time.time()) androidBuild.timestamp = int(time())
androidBuild.googleServices = int(self.device['gsf.version']) androidBuild.googleServices = int(self.device['gsf.version'])
return androidBuild return androidBuild

View File

@ -84,7 +84,7 @@ class GooglePlayAPI(object):
can later be updated, based on the request type""" can later be updated, based on the request type"""
headers = { headers = {
"Accept-Language": config.LANG.replace('_', '-'), "Accept-Language": self.deviceBuilder.locale.replace('_', '-'),
"X-DFE-Encoded-Targets": config.DFE_TARGETS, "X-DFE-Encoded-Targets": config.DFE_TARGETS,
"User-Agent": self.deviceBuilder.getUserAgent() "User-Agent": self.deviceBuilder.getUserAgent()
} }
@ -261,7 +261,7 @@ class GooglePlayAPI(object):
# strange behaviour, probably due to # strange behaviour, probably due to
# expired token # expired token
raise LoginError('Unexpected behaviour, probably expired ' raise LoginError('Unexpected behaviour, probably expired '
'token') 'token')
cluster = response.payload.listResponse.cluster[0] cluster = response.payload.listResponse.cluster[0]
if cluster.doc[0].containerMetadata.nextPageUrl != "": if cluster.doc[0].containerMetadata.nextPageUrl != "":
nextPath = cluster.doc[0].containerMetadata.nextPageUrl nextPath = cluster.doc[0].containerMetadata.nextPageUrl
@ -420,7 +420,7 @@ class GooglePlayAPI(object):
cookies=cookies, verify=ssl_verify, cookies=cookies, verify=ssl_verify,
stream=True, timeout=60) stream=True, timeout=60)
total_length = int(response.headers.get('content-length')) total_length = int(response.headers.get('content-length'))
chunk_size = 32 * (1<<10) # 32 KB chunk_size = 32 * (1 << 10) # 32 KB
bar = progress.Bar(expected_size=(total_length >> 10)) bar = progress.Bar(expected_size=(total_length >> 10))
for index, chunk in enumerate(response.iter_content(chunk_size=chunk_size)): for index, chunk in enumerate(response.iter_content(chunk_size=chunk_size)):
response_content += chunk response_content += chunk
@ -536,6 +536,20 @@ class GooglePlayAPI(object):
def changeDevice(self, device_codename): def changeDevice(self, device_codename):
self.deviceBuilder = config.DeviceBuilder(device_codename) self.deviceBuilder = config.DeviceBuilder(device_codename)
# Helpers
def getLocale(self):
return self.deviceBuilder.locale
def setLocale(self, locale):
self.deviceBuilder.locale = locale
def getTimeZone(self):
return self.deviceBuilder.timezone
def setTimeZone(self, timezone):
self.deviceBuilder.timezone = timezone
@staticmethod @staticmethod
def getDevicesCodenames(): def getDevicesCodenames():
return config.getDevicesCodenames() return config.getDevicesCodenames()