Changed Locale and TimeZone initialization

Before, if locale wasn't specified as a parameter, it was picked from
the system running gpapi. Similar thing happened for timezone, but its
value was hardcoded in config.py.
To avoid problems related to missing or unavailable apps, this commit
enforces users to provide a locale and a timezone value. Without them,
gpapi can't be initialized.
This commit is contained in:
Domenico Iezzi 2018-01-25 11:36:25 +01:00
parent d7a322dd82
commit 24a3e1a09b
No known key found for this signature in database
GPG Key ID: 7AC94D5DDA2FB7EE
3 changed files with 26 additions and 29 deletions

View File

@ -2,7 +2,6 @@ from . import googleplay_pb2
from time import time from time import time
from os import path from os import path
from sys import version_info from sys import version_info
from locale import getdefaultlocale
from re import match from re import match
VERSION = version_info[0] VERSION = version_info[0]
@ -37,50 +36,53 @@ def getDevicesCodenames():
def getDevicesReadableNames(): def getDevicesReadableNames():
"""Returns codename and readable name for each device""" """Returns codename and readable name for each device"""
sections = getDevicesCodenames() return [{'codename': s,
output = [] 'readableName': config.get(s).get('userreadablename')}
for s in sections: for s in getDevicesCodenames()]
output.append({'codename': s,
'readableName': config[s]['userreadablename']})
return output
class DeviceBuilder(object): class DeviceBuilder(object):
def __init__(self, device): def __init__(self, device):
self.device = {} self.device = {}
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
def setLocale(self, locale): def setLocale(self, locale):
# test if provided locale is valid # test if provided locale is valid
if locale is None or type(locale) is not str: if locale is None or type(locale) is not str:
# try to fetch it from system raise Exception('Wrong locale supplied')
locale = getdefaultlocale()[0]
# getdefaultlocale may return None, we need another check
if locale is None:
locale = ''
# check if locale matches the structure of a common # check if locale matches the structure of a common
# value like "en_US" # value like "en_US"
if match(r'[a-z]{2}\_[A-Z]{2}', locale) is None: if match(r'[a-z]{2}\_[A-Z]{2}', locale) is None:
locale = 'en_US' raise Exception('Wrong locale supplied')
self.locale = locale self.locale = locale
def set_timezone(self, timezone):
if timezone is None or type(timezone) is not str:
timezone = self.device.get('timezone')
if timezone is None:
raise Exception('Wrong timezone supplied')
self.timezone = timezone
def getUserAgent(self): def getUserAgent(self):
return ("Android-Finsky/8.1.72.S-all [6] [PR] 165478484 (" version_string = self.device.get('vending.versionstring')
if version_string is None:
version_string = '8.4.19.V-all [0] [FP] 175058788'
return ("Android-Finsky/{versionString} ("
"api=3" "api=3"
",versionCode={versionCode}" ",versionCode={versionCode}"
",sdk={sdk}" ",sdk={sdk}"
",device={device}" ",device={device}"
",hardware={hardware}" ",hardware={hardware}"
",product={product}" ",product={product}"
"").format(versionCode=self.device['vending.version'], "").format(versionString=version_string,
sdk=self.device['build.version.sdk_int'], versionCode=self.device.get('vending.version'),
device=self.device['build.device'], sdk=self.device.get('build.version.sdk_int'),
hardware=self.device['build.hardware'], device=self.device.get('build.device'),
product=self.device['build.product']) hardware=self.device.get('build.hardware'),
product=self.device.get('build.product'))
def getAuthParams(self, email, passwd): def getAuthParams(self, email, passwd):
return {"Email": email, return {"Email": email,

View File

@ -49,7 +49,6 @@ class GooglePlayAPI(object):
def __init__(self, debug=False, device_codename='bacon', def __init__(self, debug=False, device_codename='bacon',
locale=None, timezone=None, locale=None, timezone=None,
sim_operator=None, cell_operator=None,
proxies_config=None): proxies_config=None):
self.authSubToken = None self.authSubToken = None
self.gsfId = None self.gsfId = None
@ -57,12 +56,7 @@ class GooglePlayAPI(object):
self.proxies_config = proxies_config self.proxies_config = proxies_config
self.deviceBuilder = config.DeviceBuilder(device_codename) self.deviceBuilder = config.DeviceBuilder(device_codename)
self.deviceBuilder.setLocale(locale) self.deviceBuilder.setLocale(locale)
if timezone is not None: self.deviceBuilder.set_timezone(timezone)
self.deviceBuilder.timezone = timezone
if sim_operator is not None:
self.deviceBuilder.device['simoperator'] = sim_operator
if cell_operator is not None:
self.deviceBuilder.device['celloperator'] = cell_operator
def encrypt_password(self, login, passwd): def encrypt_password(self, login, passwd):
"""Encrypt the password using the google publickey, using """Encrypt the password using the google publickey, using
@ -489,6 +483,7 @@ class GooglePlayAPI(object):
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,
stream=True,
timeout=60, timeout=60,
proxies=self.proxies_config).content proxies=self.proxies_config).content
response_content = bytes() response_content = bytes()

View File

@ -9,7 +9,7 @@ ap.add_argument('-p', '--password', dest='password', help='google password')
args = ap.parse_args() args = ap.parse_args()
server = GooglePlayAPI(debug=True) server = GooglePlayAPI(debug=True, locale='it_IT')
# LOGIN # LOGIN
@ -19,7 +19,7 @@ gsfId = server.gsfId
authSubToken = server.authSubToken authSubToken = server.authSubToken
print('\nNow trying secondary login with ac2dm token and gsfId saved\n') print('\nNow trying secondary login with ac2dm token and gsfId saved\n')
server = GooglePlayAPI(debug=True) server = GooglePlayAPI(debug=True, locale='it_IT')
server.login(None, None, gsfId, authSubToken) server.login(None, None, gsfId, authSubToken)
# SEARCH # SEARCH