download_utils.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import os
  2. from glob import glob, escape
  3. from contextlib import contextmanager
  4. from gplaycli.gplaycli import GPlaycli
  5. class BadPackageNameException(Exception):
  6. pass
  7. class dotdict(dict):
  8. """dot.notation access to dictionary attributes"""
  9. __getattr__ = dict.get
  10. __setattr__ = dict.__setitem__
  11. __delattr__ = dict.__delitem__
  12. class APK:
  13. def __init__(self, apk, conf={}, conf_file=None):
  14. self.conf = conf
  15. self.conf_file = conf_file
  16. if 'play.google.com/store/apps/details?id=' in apk:
  17. self.package_name = apk.split('play.google.com/store/apps/details?id=')[1]
  18. else:
  19. self.package_name = apk
  20. def download(self):
  21. cli = GPlaycli(args=dotdict(self.conf), config_file=self.conf_file)
  22. cli.download_folder = 'out'
  23. d = cli.download([self.package_name]) # returns the number of downloaded packages
  24. if len(d) == 0:
  25. raise BadPackageNameException(f'error downloading {self.package_name}')
  26. fname = os.path.join('out', self.package_name + '.apk')
  27. if os.path.isfile(fname):
  28. self.file_name = fname
  29. def check_dimension(self):
  30. if os.path.getsize(self.file_name) > 50 * 1024 * 1023:
  31. os.system('split -b 49M "{0}" "{1}"'.format(self.file_name, self.file_name))
  32. os.remove(self.file_name)
  33. return glob(escape(self.file_name) + '*')
  34. @contextmanager
  35. def send(self):
  36. files = self.check_dimension() # split if size >= 50MB
  37. yield files
  38. for f in files: # removing old files
  39. os.remove(f)