48 строки
1.6 KiB
Python
48 строки
1.6 KiB
Python
import os
|
|
from glob import glob, escape
|
|
from contextlib import contextmanager
|
|
|
|
from gplaycli.gplaycli import GPlaycli
|
|
|
|
class BadPackageNameException(Exception):
|
|
pass
|
|
|
|
class dotdict(dict):
|
|
"""dot.notation access to dictionary attributes"""
|
|
__getattr__ = dict.get
|
|
__setattr__ = dict.__setitem__
|
|
__delattr__ = dict.__delitem__
|
|
|
|
class APK:
|
|
def __init__(self, apk, conf={}, conf_file=None):
|
|
self.conf = conf
|
|
self.conf_file = conf_file
|
|
if 'play.google.com/store/apps/details?id=' in apk:
|
|
self.package_name = apk.split('play.google.com/store/apps/details?id=')[1]
|
|
else:
|
|
self.package_name = apk
|
|
|
|
def download(self):
|
|
cli = GPlaycli(args=dotdict(self.conf), config_file=self.conf_file)
|
|
cli.download_folder = 'out'
|
|
d = cli.download([self.package_name]) # returns the number of downloaded packages
|
|
if len(d) == 0:
|
|
raise BadPackageNameException(f'error downloading {self.package_name}')
|
|
|
|
fname = os.path.join('out', self.package_name + '.apk')
|
|
if os.path.isfile(fname):
|
|
self.file_name = fname
|
|
|
|
def check_dimension(self):
|
|
if os.path.getsize(self.file_name) > 50 * 1024 * 1023:
|
|
os.system('split -b 49M "{0}" "{1}"'.format(self.file_name, self.file_name))
|
|
os.remove(self.file_name)
|
|
return glob(escape(self.file_name) + '*')
|
|
|
|
@contextmanager
|
|
def send(self):
|
|
files = self.check_dimension() # split if size >= 50MB
|
|
yield files
|
|
for f in files: # removing old files
|
|
os.remove(f)
|