Skip to content

Commit

Permalink
Dinamically change locale and timezone
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Domenico Iezzi committed Oct 30, 2017
1 parent 0227cb1 commit e1acc14
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
31 changes: 16 additions & 15 deletions gpapi/config.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
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:
import configparser


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"

# parse phone config from the file 'device.properties'.
# 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()
Expand Down Expand Up @@ -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

Expand All @@ -72,10 +73,10 @@ def getAuthParams(self, email, passwd):
"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):
Expand All @@ -87,16 +88,16 @@ def getLoginParams(self, email, encryptedPass):
"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
Expand Down Expand Up @@ -148,7 +149,7 @@ def getAndroidBuild(self):
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

Expand Down
20 changes: 17 additions & 3 deletions gpapi/googleplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def getDefaultHeaders(self):
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()
}
Expand Down Expand Up @@ -261,7 +261,7 @@ def search(self, query, nb_result, offset=None):
# 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
Expand Down Expand Up @@ -420,7 +420,7 @@ def _deliver_data(self, url, cookies, progress_bar):
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
Expand Down Expand Up @@ -536,6 +536,20 @@ def download(self, packageName, versionCode, offerType=1,
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()
Expand Down

0 comments on commit e1acc14

Please sign in to comment.