gplaycli-3.27/ 0000775 0000000 0000000 00000000000 13620342720 0013250 5 ustar 00root root 0000000 0000000 gplaycli-3.27/.github/ 0000775 0000000 0000000 00000000000 13620342720 0014610 5 ustar 00root root 0000000 0000000 gplaycli-3.27/.github/ISSUE_TEMPLATE.md 0000664 0000000 0000000 00000000735 13620342720 0017322 0 ustar 00root root 0000000 0000000 I WON'T ANSWER IF YOU DON'T PROVIDE SUCH DETAILS. COPY-PASTING IS NOT ENOUGH, I'M NOT A JEDI (YET).
Please provide those informations:
- [ ] Operating System (ie Ubuntu 16.04)
- [ ] Python version when running `gplaycli` (should be 3+)
- [ ] GPlayCli version via `gplaycli -v`
- [ ] The way you installed `gplaycli`: `git clone`, `git clone and setup.py`, `pip install gplaycli`, `apt install gplaycli`, ...
- [ ] The authentication method: (own) credentials or (own) token.
gplaycli-3.27/.github/workflows/ 0000775 0000000 0000000 00000000000 13620342720 0016645 5 ustar 00root root 0000000 0000000 gplaycli-3.27/.github/workflows/pythonpackage.yml 0000664 0000000 0000000 00000001411 13620342720 0022222 0 ustar 00root root 0000000 0000000 name: Python package
on: [push]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: [3.8]
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest
python -m pip install .
- name: Test with pytest
env:
GMAIL_ADDR: ${{ secrets.GMAIL_ADDR }}
GMAIL_PWD: ${{ secrets.GMAIL_PWD }}
run: |
python tests/config-edit.py gplaycli.conf
python -m pytest tests
gplaycli-3.27/.gitignore 0000664 0000000 0000000 00000000014 13620342720 0015233 0 ustar 00root root 0000000 0000000 *.pyc
*.swp
gplaycli-3.27/LICENSE.md 0000664 0000000 0000000 00000001437 13620342720 0014661 0 ustar 00root root 0000000 0000000 #GPlay-Cli
==========
Copyleft (C) 2015 Matlink
Hardly based on GooglePlayDownloader https://framagit.org/tuxicoman/googleplaydownloader
Copyright (C) 2013 Tuxicoman
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see .
gplaycli-3.27/Makefile 0000664 0000000 0000000 00000001364 13620342720 0014714 0 ustar 00root root 0000000 0000000 SHELL := /bin/bash
PYTHON=$(shell which python3)
GPG=$(shell which gpg2)
TWINE=$(shell which twine)
VERSION=$(shell $(PYTHON) setup.py --version)
GPGID=186BB3CA
source:
$(PYTHON) setup.py sdist
sign:
$(GPG) --detach-sign --default-key $(GPGID) -a dist/GPlayCli-$(VERSION).tar.gz
publish: clean source sign
$(TWINE) upload dist/GPlayCli-$(VERSION).tar.gz dist/GPlayCli-$(VERSION).tar.gz.asc
clean:
$(PYTHON) setup.py clean
rm -rf build/ MANIFEST dist GPlayCli.egg-info debian/{gplaycli,python-module-stampdir} debian/gplaycli.{debhelper.log,postinst.debhelper,prerm.debhelper,substvars} *.tar.gz* deb_dist
find . -name '*.pyc' -delete
patch:
patch setup.py debian/0001-conf-install-path.patch
deb: patch
mkdir -p packages
bash debian/build.sh gplaycli-3.27/README.md 0000664 0000000 0000000 00000011506 13620342720 0014532 0 ustar 00root root 0000000 0000000 # gplaycli [](https://github.com/matlink/gplaycli/actions)
GPlayCli is a command line tool to search, install, update Android applications from the Google Play Store.
$ usage: gplaycli [-h] [-V] [-v] [-s SEARCH] [-d AppID [AppID ...]] [-y] [-l FOLDER] [-P] [-av] [-a] [-F FILE]
[-u FOLDER] [-f FOLDER] [-dc DEVICE_CODENAME] [-t] [-tu TOKEN_URL] [-ts TOKEN_STR] [-g GSF_ID]
[-c CONF_FILE] [-p] [-L]
A Google Play Store Apk downloader and manager for command line
optional arguments:
-h, --help show this help message and exit
-V, --version Print version number and exit
-v, --verbose Be verbose
-s SEARCH, --search SEARCH
Search the given string in Google Play Store
-d AppID [AppID ...], --download AppID [AppID ...]
Download the Apps that map given AppIDs
-y, --yes Say yes to all prompted questions
-l FOLDER, --list FOLDER
List APKS in the given folder, with details
-P, --paid Also search for paid apps
-av, --append-version
Append versionstring to APKs when downloading
-a, --additional-files
Enable the download of additional files
-F FILE, --file FILE Load packages to download from file, one package per line
-u FOLDER, --update FOLDER
Update all APKs in a given folder
-f FOLDER, --folder FOLDER
Where to put the downloaded Apks, only for -d command
-dc DEVICE_CODENAME, --device-codename DEVICE_CODENAME
The device codename to fake
-t, --token Instead of classical credentials, use the tokenize version
-tu TOKEN_URL, --token-url TOKEN_URL
Use the given tokendispenser URL to retrieve a token
-ts TOKEN_STR, --token-str TOKEN_STR
Supply token string by yourself, need to supply GSF_ID at the same time
-g GSF_ID, --gsfid GSF_ID
Supply GSF_ID by yourself, need to supply token string at the same time
-c CONF_FILE, --config CONF_FILE
Use a different config file than gplaycli.conf
-p, --progress Prompt a progress bar while downloading packages
-L, --log Enable logging of apps status in separate logging files
Credentials
===========
## Warning
Token authentication is currently out of order since the default token dispenser instance has probably been blacklisted from Google servers.
By default, gplaycli fetches a token from a token dispenser server located at https://matlink.fr/token/email/gsfid to login in Google Play. If you want to use another token dispenser server, change its URL in the configuration file (depends on the way you installed it). If you want to use your own Google credentials, put
token=False
in the config file and type in your credentials in
gmail_address=
gmail_password=
variables.
Changelog
=========
See https://github.com/matlink/gplaycli/releases for releases and changelogs
Installation
============
Pip
---
- Best way to install it is using pip3: `pip3 install gplaycli` or `pip3 install gplaycli --user` if you are non-root
- Cleanest way is using virtualenv: `virtualenv gplaycli; cd gplaycli; source bin/activate`, then either `pip3 install gplaycli` or `git clone https://github.com/matlink/gplaycli && pip3 install ./gplaycli/`. Make sure `virtualenv` is initialized with Python 3. If it's not, use `virtualenv -p python3`.
Debian installation
--------------------
Releases are available here https://github.com/matlink/gplaycli/releases/ as debian packages. If you prefer not to use debian packaging, check the following method.
Requirements
----------
Works on GNU/Linux or Windows with `pip` and Python 3. First of all, ensure these packages are installed on your system :
- python3-dev package -> `apt-get install python3-dev`
- libffi package -> `apt-get install libffi-dev`
- libssl-dev -> `apt-get install libssl-dev` (for pypi's `cryptography` compilation)
- python (>=3)
Then, you need to install it with some needed libraries using either `pip3 install gplaycli` or `python3 setup.py install` after cloning it, then it will be available with `gplaycli` command. If you don't want to install it, only install requirements with `pip3 install -r requirements.txt` and use it as it.
If you want to use your own Google credentials, simply change them in the `gplaycli.conf` file with your own settings.
If you plan to use it with F-Droid-server, remember that fdroidserver needs Java (more precisely the 'jar' command) to work.
Uninstall
=========
Use `pip uninstall gplaycli`, and remove conf with `rm -rf /etc/gplaycli`. Should be clean, except python dependencies for gplaycli.
gplaycli-3.27/gplaycli.conf 0000664 0000000 0000000 00000000316 13620342720 0015723 0 ustar 00root root 0000000 0000000 [Credentials]
gmail_address=
gmail_password=
#keyring_service=gplaycli
token=False
token_url=https://matlink.fr/token/email/gsfid
[Cache]
token=~/.cache/gplaycli/token
[Locale]
locale=en_GB
timezone=CEST
gplaycli-3.27/gplaycli/ 0000775 0000000 0000000 00000000000 13620342720 0015054 5 ustar 00root root 0000000 0000000 gplaycli-3.27/gplaycli/__init__.py 0000664 0000000 0000000 00000000000 13620342720 0017153 0 ustar 00root root 0000000 0000000 gplaycli-3.27/gplaycli/__main__.py 0000664 0000000 0000000 00000000104 13620342720 0017141 0 ustar 00root root 0000000 0000000 from gplaycli.gplaycli import *
if __name__ == '__main__':
main()
gplaycli-3.27/gplaycli/gplaycli.py 0000775 0000000 0000000 00000054504 13620342720 0017245 0 ustar 00root root 0000000 0000000 #! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
GPlay-Cli
Copyleft (C) 2015 Matlink
Hardly based on GooglePlayDownloader https://framagit.org/tuxicoman/googleplaydownloader
Copyright (C) 2013 Tuxicoman
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.
You should have received a copy of the GNU Affero General Public License along with this program. If not,
see .
"""
import os
import sys
import json
import enum
import logging
import argparse
import requests
import configparser
from gpapi.googleplay import GooglePlayAPI, LoginError, RequestError
from google.protobuf.message import DecodeError
from pkg_resources import get_distribution, DistributionNotFound
from pyaxmlparser import APK
from . import util
from . import hooks
try:
import keyring
HAVE_KEYRING = True
except ImportError:
HAVE_KEYRING = False
try:
__version__ = '%s [Python%s] ' % (get_distribution('gplaycli').version, sys.version.split()[0])
except DistributionNotFound:
__version__ = 'unknown: gplaycli not installed (version in setup.py)'
logger = logging.getLogger(__name__) # default level is WARNING
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("[%(levelname)s] %(message)s"))
logger.addHandler(handler)
logger.propagate = False
class ERRORS(enum.IntEnum):
"""
Contains constant errors for Gplaycli
"""
SUCCESS = 0
TOKEN_DISPENSER_AUTH_ERROR = 5
TOKEN_DISPENSER_SERVER_ERROR = 6
KEYRING_NOT_INSTALLED = 10
CANNOT_LOGIN_GPLAY = 15
class GPlaycli:
"""
Object which handles Google Play connection
search and download.
GPlaycli can be used as an API with parameters
token_enable, token_url, config and with methods
retrieve_token(), connect(),
download(), search().
"""
def __init__(self, args=None, config_file=None):
# no config file given, look for one
if config_file is None:
# default local user configs
cred_paths_list = [
'gplaycli.conf',
os.path.expanduser("~") + '/.config/gplaycli/gplaycli.conf',
'/etc/gplaycli/gplaycli.conf'
]
config_file = None
for filepath in cred_paths_list:
if os.path.isfile(filepath):
config_file = filepath
break
if config_file is None:
logger.warn("No configuration file found at %s, using default values" % cred_paths_list)
self.api = None
self.token_passed = False
config = configparser.ConfigParser()
if config_file:
config.read(config_file)
self.gmail_address = config.get('Credentials', 'gmail_address', fallback=None)
self.gmail_password = config.get('Credentials', 'gmail_password', fallback=None)
self.token_enable = config.getboolean('Credentials', 'token', fallback=True)
self.token_url = config.get('Credentials', 'token_url', fallback='https://matlink.fr/token/email/gsfid')
self.keyring_service = config.get('Credentials', 'keyring_service', fallback=None)
self.tokencachefile = os.path.expanduser(config.get("Cache", "token", fallback="token.cache"))
self.yes = config.getboolean('Misc', 'accept_all', fallback=False)
self.verbose = config.getboolean('Misc', 'verbose', fallback=False)
self.append_version = config.getboolean('Misc', 'append_version', fallback=False)
self.progress_bar = config.getboolean('Misc', 'progress', fallback=False)
self.logging_enable = config.getboolean('Misc', 'enable_logging', fallback=False)
self.addfiles_enable = config.getboolean('Misc', 'enable_addfiles', fallback=False)
self.device_codename = config.get('Device', 'codename', fallback='bacon')
self.locale = config.get("Locale", "locale", fallback="en_GB")
self.timezone = config.get("Locale", "timezone", fallback="CEST")
if not args: return
# if args are passed, override defaults
if args.yes is not None:
self.yes = args.yes
if args.verbose is not None:
self.verbose = args.verbose
if self.verbose:
logger.setLevel(logging.INFO)
logger.info('GPlayCli version %s', __version__)
logger.info('Configuration file is %s', config_file)
if args.append_version is not None:
self.append_version = args.append_version
if args.progress is not None:
self.progress_bar = args.progress
if args.update is not None:
self.download_folder = args.update
if args.log is not None:
self.logging_enable = args.log
if args.device_codename is not None:
self.device_codename = args.device_codename
logger.info('Device is %s', self.device_codename)
if args.additional_files is not None:
self.addfiles_enable = args.additional_files
if args.token is not None:
self.token_enable = args.token
if self.token_enable is not None:
if args.token_url is not None:
self.token_url = args.token_url
if (args.token_str is not None) and (args.gsfid is not None):
self.token = args.token_str
self.gsfid = args.gsfid
self.token_passed = True
elif args.token_str is None and args.gsfid is None:
pass
else:
raise TypeError("Token string and GSFID have to be passed at the same time.")
if self.logging_enable:
self.success_logfile = "apps_downloaded.log"
self.failed_logfile = "apps_failed.log"
self.unavail_logfile = "apps_not_available.log"
########## Public methods ##########
def retrieve_token(self, force_new=False):
"""
Return a token. If a cached token exists,
it will be used. Else, or if force_new=True,
a new token is fetched from the token-dispenser
server located at self.token_url.
"""
self.token, self.gsfid, self.device = self.get_cached_token()
if (self.token is not None and not force_new and self.device == self.device_codename):
logger.info("Using cached token.")
return
logger.info("Retrieving token ...")
url = '/'.join([self.token_url, self.device_codename])
logger.info("Token URL is %s", url)
response = requests.get(url)
if response.text == 'Auth error':
logger.error('Token dispenser auth error, probably too many connections')
sys.exit(ERRORS.TOKEN_DISPENSER_AUTH_ERROR)
elif response.text == "Server error":
logger.error('Token dispenser server error')
sys.exit(ERRORS.TOKEN_DISPENSER_SERVER_ERROR)
elif len(response.text) != 88: # other kinds of errors
logger.error('Unknown error: %s', response.text)
sys.exit(ERRORS.TOKEN_DISPENSER_SERVER_ERROR)
self.token, self.gsfid = response.text.split(" ")
logger.info("Token: %s", self.token)
logger.info("GSFId: %s", self.gsfid)
self.write_cached_token(self.token, self.gsfid, self.device_codename)
@hooks.connected
def download(self, pkg_todownload):
"""
Download apks from the pkg_todownload list
pkg_todownload -- list either of app names or
of tuple of app names and filepath to write them
Example: ['org.mozilla.focus','org.mozilla.firefox'] or
[('org.mozilla.focus', 'org.mozilla.focus.apk'),
('org.mozilla.firefox', 'download/org.mozilla.firefox.apk')]
"""
success_downloads = []
failed_downloads = []
unavail_downloads = []
# case where no filenames have been provided
for index, pkg in enumerate(pkg_todownload):
if isinstance(pkg, str):
pkg_todownload[index] = [pkg, None]
# remove whitespaces before and after package name
pkg_todownload[index][0] = pkg_todownload[index][0].strip()
# Check for download folder
download_folder = self.download_folder
if not os.path.isdir(download_folder):
os.makedirs(download_folder, exist_ok=True)
# BulkDetails requires only one HTTP request
# Get APK info from store
details = list()
for pkg in pkg_todownload:
try:
detail = self.api.details(pkg[0])
details.append(detail)
except RequestError as request_error:
failed_downloads.append((pkg, request_error))
if any([d is None for d in details]):
logger.info("Token has expired while downloading. Retrieving a new one.")
self.refresh_token()
details = self.api.bulkDetails([pkg[0] for pkg in pkg_todownload])
for position, (detail, item) in enumerate(zip(details, pkg_todownload)):
packagename, filename = item
if filename is None:
if self.append_version:
filename = "%s-v.%s.apk" % (detail['docid'], detail['details']['appDetails']['versionString'])
else:
filename = "%s.apk" % detail['docid']
logger.info("%s / %s %s", 1+position, len(pkg_todownload), packagename)
# Download
try:
if detail['offer'][0]['checkoutFlowRequired']:
method = self.api.delivery
else:
method = self.api.download
data_iter = method(packagename, expansion_files=self.addfiles_enable)
success_downloads.append(packagename)
except IndexError as exc:
logger.error("Error while downloading %s : this package does not exist, "
"try to search it via --search before",
packagename)
unavail_downloads.append((item, exc))
continue
except Exception as exc:
logger.error("Error while downloading %s : %s", packagename, exc)
failed_downloads.append((item, exc))
continue
filepath = os.path.join(download_folder, filename)
#if file exists, continue
if self.append_version and os.path.isfile(filepath):
logger.info("File %s already exists, skipping.", filename)
continue
additional_data = data_iter['additionalData']
total_size = int(data_iter['file']['total_size'])
chunk_size = int(data_iter['file']['chunk_size'])
try:
with open(filepath, "wb") as fbuffer:
bar = util.progressbar(expected_size=total_size, hide=not self.progress_bar)
for index, chunk in enumerate(data_iter['file']['data']):
fbuffer.write(chunk)
bar.show(index * chunk_size)
bar.done()
if additional_data:
for obb_file in additional_data:
obb_filename = "%s.%s.%s.obb" % (obb_file["type"], obb_file["versionCode"], data_iter["docId"])
obb_filename = os.path.join(download_folder, obb_filename)
obb_total_size = int(obb_file['file']['total_size'])
obb_chunk_size = int(obb_file['file']['chunk_size'])
with open(obb_filename, "wb") as fbuffer:
bar = util.progressbar(expected_size=obb_total_size, hide=not self.progress_bar)
for index, chunk in enumerate(obb_file["file"]["data"]):
fbuffer.write(chunk)
bar.show(index * obb_chunk_size)
bar.done()
except IOError as exc:
logger.error("Error while writing %s : %s", packagename, exc)
failed_downloads.append((item, exc))
success_items = set(success_downloads)
failed_items = set([item[0] for item, error in failed_downloads])
unavail_items = set([item[0] for item, error in unavail_downloads])
to_download_items = set([item[0] for item in pkg_todownload])
self.write_logfiles(success_items, failed_items, unavail_items)
self.print_failed(failed_downloads + unavail_downloads)
return to_download_items - failed_items
@hooks.connected
def search(self, search_string, free_only=True, include_headers=True):
"""
Search the given string search_string on the Play Store.
search_string -- the string to search on the Play Store
free_only -- True if only costless apps should be searched for
include_headers -- True if the result table should show column names
"""
try:
results = self.api.search(search_string)
except IndexError:
results = []
if not results:
logger.info("No result")
return
all_results = []
if include_headers:
# Name of the columns
col_names = ["Title", "Creator", "Size", "Downloads", "Last Update", "AppID", "Version", "Rating"]
all_results.append(col_names)
# Compute results values
for doc in results:
for cluster in doc["child"]:
for app in cluster["child"]:
# skip that app if it not free
# or if it's beta (pre-registration)
if ('offer' not in app # beta apps (pre-registration)
or free_only
and app['offer'][0]['checkoutFlowRequired'] # not free to download
):
continue
details = app['details']['appDetails']
detail = [app['title'],
app['creator'],
util.sizeof_fmt(int(details['installationSize']))
if int(details['installationSize']) > 0 else 'N/A',
details['numDownloads'],
details['uploadDate'],
app['docid'],
details['versionCode'],
"%.2f" % app["aggregateRating"]["starRating"]
]
all_results.append(detail)
# Print a nice table
col_width = []
for column_indice in range(len(all_results[0])):
col_length = max([len("%s" % row[column_indice]) for row in all_results])
col_width.append(col_length + 2)
for result in all_results:
for indice, item in enumerate(result):
out = "".join(str(item).strip().ljust(col_width[indice]))
try:
print(out, end='')
except UnicodeEncodeError:
out = out.encode('utf-8', errors='replace')
print(out, end='')
print()
return all_results
########## End public methods ##########
########## Internal methods ##########
def connect(self):
"""
Connect GplayCli to the Google Play API.
If self.token_enable=True, the token from
self.retrieve_token is used. Else, classical
credentials are used. They might be stored
into the keyring if the keyring package
is installed.
"""
self.api = GooglePlayAPI(locale=self.locale, timezone=self.timezone, device_codename=self.device_codename)
if self.token_enable:
self.retrieve_token()
return self.connect_token()
else:
return self.connect_credentials()
def connect_token(self):
if self.token_passed:
logger.info("Using passed token to connect to API")
else:
logger.info("Using auto retrieved token to connect to API")
try:
self.api.login(authSubToken=self.token, gsfId=int(self.gsfid, 16))
except (ValueError, IndexError, LoginError, DecodeError, SystemError, RequestError):
logger.info("Token has expired or is invalid. Retrieving a new one...")
self.retrieve_token(force_new=True)
self.connect()
return True, None
def connect_credentials(self):
logger.info("Using credentials to connect to API")
if self.gmail_password:
logger.info("Using plaintext password")
password = self.gmail_password
elif self.keyring_service and HAVE_KEYRING:
password = keyring.get_password(self.keyring_service, self.gmail_address)
elif self.keyring_service and not HAVE_KEYRING:
logger.error("You asked for keyring service but keyring package is not installed")
return False, ERRORS.KEYRING_NOT_INSTALLED
try:
self.api.login(email=self.gmail_address, password=password)
except LoginError as e:
logger.error("Bad authentication, login or password incorrect (%s)", e)
return False, ERRORS.CANNOT_LOGIN_GPLAY
return True, None
def get_cached_token(self):
"""
Retrieve a cached token, gsfid and device if exist.
Otherwise return None.
"""
try:
cache_dic = json.loads(open(self.tokencachefile).read())
token = cache_dic['token']
gsfid = cache_dic['gsfid']
device = cache_dic['device']
except (IOError, ValueError): # cache file does not exists or is corrupted
token = None
gsfid = None
device = None
logger.error('Cache file does not exists or is corrupted')
return token, gsfid, device
def write_cached_token(self, token, gsfid, device):
"""
Write the given token, gsfid and device
to the self.tokencachefile file.
Path and file are created if missing.
"""
cachedir = os.path.dirname(self.tokencachefile)
if not cachedir:
cachedir = os.getcwd()
# creates cachedir if not exists
if not os.path.exists(cachedir):
os.makedirs(cachedir, exist_ok=True)
with open(self.tokencachefile, 'w') as tcf:
tcf.write(json.dumps({'token' : token,
'gsfid' : gsfid,
'device' : device}))
def prepare_analyse_apks(self):
"""
Gather apks to further check for update
"""
list_of_apks = util.list_folder_apks(self.download_folder)
if not list_of_apks:
return
logger.info("Checking apks ...")
to_update = self.analyse_local_apks(list_of_apks, self.download_folder)
return self.prepare_download_updates(to_update)
@hooks.connected
def analyse_local_apks(self, list_of_apks, download_folder):
"""
Analyse apks in the list list_of_apks
to check for updates and download updates
in the download_folder folder.
"""
list_apks_to_update = []
package_bunch = []
version_codes = []
unavail_items = []
UNAVAIL = "This app is not available in the Play Store"
for filename in list_of_apks:
filepath = os.path.join(download_folder, filename)
logger.info("Analyzing %s", filepath)
apk = APK(filepath)
packagename = apk.package
package_bunch.append(packagename)
version_codes.append(util.vcode(apk.version_code))
# BulkDetails requires only one HTTP request
# Get APK info from store
details = self.api.bulkDetails(package_bunch)
for detail, packagename, filename, apk_version_code in zip(details, package_bunch, list_of_apks, version_codes):
# this app is not in the play store
if not detail:
unavail_items.append(((packagename, filename), UNAVAIL))
continue
store_version_code = detail['details']['appDetails']['versionCode']
# Compare
if apk_version_code < store_version_code:
# Add to the download list
list_apks_to_update.append([packagename, filename, apk_version_code, store_version_code])
self.write_logfiles(None, None, [item[0][0] for item in unavail_items])
self.print_failed(unavail_items)
return list_apks_to_update
def prepare_download_updates(self, list_apks_to_update):
"""
Ask confirmation before updating apks
"""
if not list_apks_to_update:
print("Everything is up to date !")
return False
pkg_todownload = []
# Ask confirmation before downloading
print("The following applications will be updated :")
for packagename, filename, apk_version_code, store_version_code in list_apks_to_update:
print("%s Version : %s -> %s" % (filename, apk_version_code, store_version_code))
pkg_todownload.append([packagename, filename])
if not self.yes:
print("Do you agree?")
return_value = input('y/n ?')
if self.yes or return_value == 'y':
logger.info("Downloading ...")
downloaded_packages = self.download(pkg_todownload)
return_string = ' '.join(downloaded_packages)
print("Updated: %s" % return_string)
return True
@staticmethod
def print_failed(failed_downloads):
"""
Print/log failed downloads from failed_downloads
"""
# Info message
if not failed_downloads:
return
else:
message = "A few packages could not be downloaded :\n"
for pkg, exception in failed_downloads:
package_name, filename = pkg
if filename is not None:
message += "%s : %s\n" % (filename, package_name)
else:
message += "%s\n" % package_name
message += "%s\n" % exception
logger.error(message)
def write_logfiles(self, success, failed, unavail):
"""
Write success failed and unavail list to
logfiles
"""
if not self.logging_enable:
return
for result, logfile in [(success, self.success_logfile), (failed, self.failed_logfile), (unavail, self.unavail_logfile)]:
if not result:
continue
with open(logfile, 'w') as _buffer:
for package in result:
print(package, file=_buffer)
########## End internal methods ##########
def main():
"""
Main function.
Parse command line arguments
"""
parser = argparse.ArgumentParser(description="A Google Play Store Apk downloader and manager for command line")
parser.add_argument('-V', '--version', help="Print version number and exit", action='store_true')
parser.add_argument('-v', '--verbose', help="Be verbose", action='store_true')
parser.add_argument('-s', '--search', help="Search the given string in Google Play Store", metavar="SEARCH")
parser.add_argument('-d', '--download', help="Download the Apps that map given AppIDs", metavar="AppID", nargs="+")
parser.add_argument('-y', '--yes', help="Say yes to all prompted questions", action='store_true')
parser.add_argument('-l', '--list', help="List APKS in the given folder, with details", metavar="FOLDER")
parser.add_argument('-P', '--paid', help="Also search for paid apps", action='store_true', default=False)
parser.add_argument('-av', '--append-version', help="Append versionstring to APKs when downloading", action='store_true')
parser.add_argument('-a', '--additional-files', help="Enable the download of additional files", action='store_true', default=False)
parser.add_argument('-F', '--file', help="Load packages to download from file, one package per line", metavar="FILE")
parser.add_argument('-u', '--update', help="Update all APKs in a given folder", metavar="FOLDER")
parser.add_argument('-f', '--folder', help="Where to put the downloaded Apks, only for -d command", metavar="FOLDER", nargs=1, default=['.'])
parser.add_argument('-dc', '--device-codename', help="The device codename to fake", choices=GooglePlayAPI.getDevicesCodenames(), metavar="DEVICE_CODENAME")
parser.add_argument('-t', '--token', help="Instead of classical credentials, use the tokenize version", action='store_true', default=None)
parser.add_argument('-tu', '--token-url', help="Use the given tokendispenser URL to retrieve a token", metavar="TOKEN_URL")
parser.add_argument('-ts', '--token-str', help="Supply token string by yourself, need to supply GSF_ID at the same time", metavar="TOKEN_STR")
parser.add_argument('-g', '--gsfid', help="Supply GSF_ID by yourself, need to supply token string at the same time", metavar="GSF_ID")
parser.add_argument('-c', '--config', help="Use a different config file than gplaycli.conf", metavar="CONF_FILE", nargs=1)
parser.add_argument('-p', '--progress', help="Prompt a progress bar while downloading packages", action='store_true')
parser.add_argument('-L', '--log', help="Enable logging of apps status in separate logging files", action='store_true', default=False)
if len(sys.argv) < 2:
sys.argv.append("-h")
args = parser.parse_args()
if args.version:
print(__version__)
return
cli = GPlaycli(args, args.config)
if args.list:
print(util.list_folder_apks(args.list))
if args.update:
cli.prepare_analyse_apks()
return
if args.search:
cli.verbose = True
cli.search(args.search, not args.paid)
if args.file:
args.download = util.load_from_file(args.file)
if args.download is not None:
if args.folder is not None:
cli.download_folder = args.folder[0]
cli.download(args.download)
if __name__ == '__main__':
main()
gplaycli-3.27/gplaycli/hooks.py 0000664 0000000 0000000 00000000435 13620342720 0016553 0 ustar 00root root 0000000 0000000 def connected(function):
"""
Decorator that checks the api status
before doing any request
"""
def check_connection(self, *args, **kwargs):
if self.api is None or self.api.authSubToken is None:
self.connect()
return function(self, *args, **kwargs)
return check_connection
gplaycli-3.27/gplaycli/util.py 0000664 0000000 0000000 00000006445 13620342720 0016414 0 ustar 00root root 0000000 0000000 import os
import sys
import math
import time
def sizeof_fmt(num):
if not num:
return "00.00KB"
log = int(math.log(num, 1024))
return "%.2f%s" % (num/(1024**log), ['B ','KB','MB','GB','TB'][log])
def load_from_file(filename):
return [package.strip('\r\n') for package in open(filename).readlines()]
def list_folder_apks(folder):
"""
List apks in the given folder
"""
list_of_apks = [filename for filename in os.listdir(folder) if filename.endswith(".apk")]
return list_of_apks
def vcode(string_vcode):
"""
return integer of version
base can be 10 or 16
"""
base = 10
if string_vcode.startswith('0x'):
base = 16
return int(string_vcode, base)
### progress bar ###
""" copyright https://github.com/kennethreitz/clint """
class progressbar(object):
def __init__(self, label='', width=32, hide=None, empty_char=' ',
filled_char='#', expected_size=None, every=1, eta_interval=1,
eta_sma_window=9, stream=sys.stderr, bar_template='%s[%s%s] %s/%s - %s %s/s\r'):
self.label = label
self.width = width
self.hide = hide
# Only show bar in terminals by default (better for piping, logging etc.)
if hide is None:
try:
self.hide = not stream.isatty()
except AttributeError: # output does not support isatty()
self.hide = True
self.empty_char = empty_char
self.filled_char = filled_char
self.expected_size = expected_size
self.every = every
self.start = time.time()
self.ittimes = []
self.eta = 0
self.etadelta = time.time()
self.etadisp = self.format_time(self.eta)
self.last_progress = 0
self.eta_interval = eta_interval
self.eta_sma_window = eta_sma_window
self.stream = stream
self.bar_template = bar_template
self.speed = 0
if (self.expected_size):
self.show(0)
def show(self, progress, count=None):
if count is not None:
self.expected_size = count
if self.expected_size is None:
raise Exception("expected_size not initialized")
self.last_progress = progress
if (time.time() - self.etadelta) > self.eta_interval:
self.etadelta = time.time()
self.ittimes = (self.ittimes[-self.eta_sma_window:]
+ [-(self.start - time.time()) / (progress+1)])
self.eta = (sum(self.ittimes) / float(len(self.ittimes))
* (self.expected_size - progress))
self.etadisp = self.format_time(self.eta)
self.speed = 1 / (sum(self.ittimes) / float(len(self.ittimes)))
x = int(self.width * progress / self.expected_size)
if not self.hide:
if ((progress % self.every) == 0 or # True every "every" updates
(progress == self.expected_size)): # And when we're done
self.stream.write(self.bar_template % (
self.label, self.filled_char * x,
self.empty_char * (self.width - x), sizeof_fmt(progress),
sizeof_fmt(self.expected_size), self.etadisp, sizeof_fmt(self.speed)))
self.stream.flush()
def done(self):
self.elapsed = time.time() - self.start
elapsed_disp = self.format_time(self.elapsed)
if not self.hide:
# Print completed bar with elapsed time
self.stream.write(self.bar_template % (
self.label, self.filled_char * self.width,
self.empty_char * 0, sizeof_fmt(self.last_progress),
sizeof_fmt(self.expected_size), elapsed_disp, sizeof_fmt(self.speed)))
self.stream.write('\n')
self.stream.flush()
@staticmethod
def format_time(seconds):
return time.strftime('%H:%M:%S', time.gmtime(seconds))
gplaycli-3.27/requirements.txt 0000664 0000000 0000000 00000000032 13620342720 0016527 0 ustar 00root root 0000000 0000000 gpapi>=0.4.4
pyaxmlparser
gplaycli-3.27/setup.py 0000664 0000000 0000000 00000001154 13620342720 0014763 0 ustar 00root root 0000000 0000000 from setuptools import setup
import os
import sys
setup(name='gplaycli',
version='3.27',
description='GPlayCli, a Google play downloader command line interface',
author="Matlink",
author_email="matlink@matlink.fr",
url="https://github.com/matlink/gplaycli",
license="AGPLv3",
entry_points={
'console_scripts': [
'gplaycli = gplaycli.gplaycli:main',
],
},
packages=[
'gplaycli',
],
package_dir={
'gplaycli': 'gplaycli',
},
data_files=[
[os.path.expanduser('~')+'/.config/gplaycli', ['gplaycli.conf']],
],
install_requires=[
'gpapi>=0.4.4',
'pyaxmlparser',
],
)
gplaycli-3.27/tests/ 0000775 0000000 0000000 00000000000 13620342720 0014412 5 ustar 00root root 0000000 0000000 gplaycli-3.27/tests/config-edit.py 0000664 0000000 0000000 00000000452 13620342720 0017155 0 ustar 00root root 0000000 0000000 import os
import sys
addr = os.environ['GMAIL_ADDR']
pwd = os.environ['GMAIL_PWD']
filename = sys.argv[1]
config = open(filename).read()
config = config.replace('gmail_address=', 'gmail_address=' + addr).replace('gmail_password=', 'gmail_password=' + pwd)
print(config, file=open(filename, 'w')) gplaycli-3.27/tests/test_cli.py 0000664 0000000 0000000 00000005045 13620342720 0016576 0 ustar 00root root 0000000 0000000 import os
import sys
import hashlib
import pytest
import subprocess as sp
import re
import json
ENC = sys.getdefaultencoding()
ERR = 'replace'
TESTAPK='org.mozilla.focus'
UPDATEAPK=os.path.join("tests", "org.mozilla.focus.20112247.apk")
TOKENFILE=os.path.expanduser('~/.cache/gplaycli/token')
RE_APPEND_VERSION=re.compile("^"+TESTAPK.replace('.', r'\.')+r"-v.[A-z0-9.-]+\.apk$")
def call(args):
proc = sp.run(args.split(), stdout=sp.PIPE, stderr=sp.PIPE)
print(proc.stdout.decode(ENC, ERR), file=sys.stdout)
print(proc.stderr.decode(ENC, ERR), file=sys.stderr)
return proc
def nblines(comp_proc):
return len(comp_proc.stdout.decode(ENC, ERR).splitlines(True))
def download_apk(append_version = False):
if append_version:
call("gplaycli -av -vd %s" % TESTAPK)
else:
call("gplaycli -vd %s" % TESTAPK)
def checksum(apk):
return hashlib.sha256(open(apk, 'rb').read()).hexdigest()
def test_download():
if os.path.isfile(TOKENFILE):
os.remove(TOKENFILE)
download_apk()
assert os.path.isfile("%s.apk" % TESTAPK)
def test_download_version():
if os.path.isfile(TOKENFILE):
os.remove(TOKENFILE)
download_apk(append_version = True)
found = False
for f in os.listdir():
if RE_APPEND_VERSION.match(f):
found = True
if not found:
pytest.fail("Could not find package with version appended")
#def test_alter_token():
# cache_dict = json.loads(open(TOKENFILE).read())
# cache_dict['token'] = ' ' + cache_dict['token'][1:]
# with open(TOKENFILE, 'w') as outfile:
# print(json.dumps(cache_dict), file=outfile)
# download_apk()
# assert os.path.isfile("%s.apk" % TESTAPK)
def test_update(apk=UPDATEAPK):
before = checksum(apk)
call("gplaycli -vyu tests")
after = checksum(apk)
assert after != before
def test_search(string='fire'):
c = call("gplaycli -s %s" % (string))
assert c.returncode == 0
def test_search2(string='com.yogavpn'):
c = call("gplaycli -s %s" % string)
assert c.returncode == 0
assert nblines(c) >= 0
def test_search3(string='com.yogavpn'):
c = call("gplaycli -s %s" % (string))
assert c.returncode == 0
def test_another_device(device='hammerhead'):
call("gplaycli -vd %s -dc %s" % (TESTAPK, device))
assert os.path.isfile("%s.apk" % TESTAPK)
def test_download_additional_files(apk='com.mapswithme.maps.pro', device='angler'):
call("gplaycli -d %s -a -dc %s" % (apk, device))
assert os.path.isfile("%s.apk" % apk)
files = [f for f in os.listdir() if os.path.isfile(f)]
assert any([f.endswith('%s.obb' % apk) and f.startswith('main') for f in files])
assert any([f.endswith('%s.obb' % apk) and f.startswith('patch') for f in files])
gplaycli-3.27/tests/test_init.py 0000664 0000000 0000000 00000002141 13620342720 0016764 0 ustar 00root root 0000000 0000000 import sys
import os
sys.path.insert(0, os.path.abspath('.'))
from gplaycli import gplaycli
gpc = gplaycli.GPlaycli()
token_url = "https://matlink.fr/token/email/gsfid"
def test_default_settings():
assert gpc.yes == False
assert gpc.verbose == False
assert gpc.progress_bar == False
assert gpc.device_codename == 'bacon'
def test_connection_credentials():
try: # You are travis
if os.environ['TRAVIS_PULL_REQUEST'] != "false": # If current job is a Pull Request
print("Job is pull request. Won't check credentials")
return
except KeyError: # You are not travis
pass
gpc.token_enable = False
gpc.gmail_address = os.environ['GMAIL_ADDR']
gpc.gmail_password = os.environ['GMAIL_PWD']
success, error = gpc.connect()
assert error is None
assert success == True
#def test_connection_token():
# gpc.token_enable = True
# gpc.token_url = token_url
# gpc.retrieve_token(force_new=True)
# success, error = gpc.connect()
# assert error is None
# assert success == True
def test_download_focus():
gpc.progress_bar = True
gpc.download_folder = os.path.abspath('.')
gpc.download(['org.mozilla.focus'])