From e1acc14424504c2cdefac039297ee5927c686cbe Mon Sep 17 00:00:00 2001 From: Domenico Iezzi Date: Mon, 30 Oct 2017 17:30:10 +0100 Subject: [PATCH] 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. --- gpapi/config.py | 31 ++++++++++++++++--------------- gpapi/googleplay.py | 20 +++++++++++++++++--- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/gpapi/config.py b/gpapi/config.py index fd6fa98..94ae531 100644 --- a/gpapi/config.py +++ b/gpapi/config.py @@ -1,9 +1,10 @@ from . import googleplay_pb2 -import time -import os -import sys +from time import time +from os import path +from sys import version_info +from locale import getdefaultlocale -VERSION = sys.version_info[0] +VERSION = version_info[0] if VERSION == 2: import ConfigParser else: @@ -11,8 +12,6 @@ else: DFE_TARGETS = "CAEScFfqlIEG6gUYogFWrAISK1WDAg+hAZoCDgIU1gYEOIACFkLMAeQBnASLATlASUuyAyqCAjY5igOMBQzfA/IClwFbApUC4ANbtgKVAS7OAX8YswHFBhgDwAOPAmGEBt4OfKkB5weSB5AFASkiN68akgMaxAMSAQEBA9kBO7UBFE1KVwIDBGs3go6BBgEBAgMECQgJAQIEAQMEAQMBBQEBBAUEFQYCBgUEAwMBDwIBAgOrARwBEwMEAg0mrwESfTEcAQEKG4EBMxghChMBDwYGASI3hAEODEwXCVh/EREZA4sBYwEdFAgIIwkQcGQRDzQ2fTC2AjfVAQIBAYoBGRg2FhYFBwEqNzACJShzFFblAo0CFxpFNBzaAd0DHjIRI4sBJZcBPdwBCQGhAUd2A7kBLBVPngEECHl0UEUMtQETigHMAgUFCc0BBUUlTywdHDgBiAJ+vgKhAU0uAcYCAWQ/5ALUAw1UwQHUBpIBCdQDhgL4AY4CBQICjARbGFBGWzA1CAEMOQH+BRAOCAZywAIDyQZ2MgM3BxsoAgUEBwcHFia3AgcGTBwHBYwBAlcBggFxSGgIrAEEBw4QEqUCASsWadsHCgUCBQMD7QICA3tXCUw7ugJZAwGyAUwpIwM5AwkDBQMJA5sBCw8BNxBVVBwVKhebARkBAwsQEAgEAhESAgQJEBCZATMdzgEBBwG8AQQYKSMUkAEDAwY/CTs4/wEaAUt1AwEDAQUBAgIEAwYEDx1dB2wGeBFgTQ" -LANG = "en_US" -TIMEZONE = 'America/New_York' GOOGLE_PUBKEY = "AAAAgMom/1a/v0lblO2Ubrt60J2gcuXSljGFQXgcyZWveWLEwo6prwgi3iJIZdodyhKZQrNWp5nKJ3srRXcUW+F1BD3baEVGcmEgqaLZUNBjm057pKRI16kB0YppeGx5qIQ5QjKzsR8ETQbKLNWgRY0QRNVz34kMJR3P/LgHax/6rmf5AAAAAwEAAQ==" ACCOUNT = "HOSTED_OR_GOOGLE" @@ -20,8 +19,8 @@ ACCOUNT = "HOSTED_OR_GOOGLE" # if you want to add another phone, just create another section in # the file. Some configurations for common phones can be found here: # https://github.com/yeriomin/play-store-api/tree/master/src/main/resources -filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), - 'device.properties') +filepath = path.join(path.dirname(path.realpath(__file__)), + 'device.properties') if VERSION == 2: config = ConfigParser.ConfigParser() @@ -49,6 +48,8 @@ class DeviceBuilder(object): def __init__(self, device): self.device = {} + self.locale = getdefaultlocale()[0] + self.timezone = "Europe/Berlin" for (key, value) in config.items(device): self.device[key] = value @@ -72,10 +73,10 @@ class DeviceBuilder(object): "accountType": ACCOUNT, "has_permission": "1", "source": "android", - "device_country": LANG[0:2], + "device_country": self.locale[0:2], "service": "androidmarket", "app": "com.android.vending", - "lang": LANG, + "lang": self.locale, "sdk_version": self.device['build.version.sdk_int']} def getLoginParams(self, email, encryptedPass): @@ -87,16 +88,16 @@ class DeviceBuilder(object): "has_permission": "1", "app": "com.google.android.gsf", "source": "android", - "device_country": LANG[0:2], - "lang": LANG, + "device_country": self.locale[0:2], + "lang": self.locale, "sdk_version": self.device['build.version.sdk_int']} def getAndroidCheckinRequest(self): request = googleplay_pb2.AndroidCheckinRequest() request.id = 0 request.checkin.CopyFrom(self.getAndroidCheckin()) - request.locale = LANG - request.timeZone = TIMEZONE + request.locale = self.locale + request.timeZone = self.timezone request.version = 3 request.deviceConfiguration.CopyFrom(self.getDeviceConfig()) request.fragment = 0 @@ -148,7 +149,7 @@ class DeviceBuilder(object): androidBuild.buildProduct = self.device['build.product'] androidBuild.client = self.device['client'] androidBuild.otaInstalled = False - androidBuild.timestamp = int(time.time()) + androidBuild.timestamp = int(time()) androidBuild.googleServices = int(self.device['gsf.version']) return androidBuild diff --git a/gpapi/googleplay.py b/gpapi/googleplay.py index 18710f7..03f5fb9 100644 --- a/gpapi/googleplay.py +++ b/gpapi/googleplay.py @@ -84,7 +84,7 @@ class GooglePlayAPI(object): can later be updated, based on the request type""" headers = { - "Accept-Language": config.LANG.replace('_', '-'), + "Accept-Language": self.deviceBuilder.locale.replace('_', '-'), "X-DFE-Encoded-Targets": config.DFE_TARGETS, "User-Agent": self.deviceBuilder.getUserAgent() } @@ -261,7 +261,7 @@ class GooglePlayAPI(object): # strange behaviour, probably due to # expired token raise LoginError('Unexpected behaviour, probably expired ' - 'token') + 'token') cluster = response.payload.listResponse.cluster[0] if cluster.doc[0].containerMetadata.nextPageUrl != "": nextPath = cluster.doc[0].containerMetadata.nextPageUrl @@ -420,7 +420,7 @@ class GooglePlayAPI(object): cookies=cookies, verify=ssl_verify, stream=True, timeout=60) 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)) for index, chunk in enumerate(response.iter_content(chunk_size=chunk_size)): response_content += chunk @@ -536,6 +536,20 @@ class GooglePlayAPI(object): def changeDevice(self, 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 def getDevicesCodenames(): return config.getDevicesCodenames()