commit 263c1754e953869a3d5cbbcc158b56d709df27d2 Author: Nikolai Kristiansen Date: Wed Sep 27 13:31:33 2017 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..471d966 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.swp +.idea +*.pyc +data/ +venv/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ae48c04 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +Download and format Google Play stats as single JSON file + +## Requirements + +You need [gsutil](https://cloud.google.com/storage/docs/gsutil_install) + +## Usage + + python google_play.py ACCOUNT_ID \ No newline at end of file diff --git a/google_play.py b/google_play.py new file mode 100644 index 0000000..69c4051 --- /dev/null +++ b/google_play.py @@ -0,0 +1,53 @@ +import json +import subprocess + +import os +from csv import DictReader + +from collections import defaultdict + +import sys + +DATA_DIR = 'data' + + +def download_csvs(account_id): + if not os.path.exists(DATA_DIR): + os.mkdir(DATA_DIR) + + uri = 'gs://pubsite__rev_{}/stats/installs/*_overview.csv'.format(account_id) + cmd = ['gsutil', 'cp', '-r', uri, 'data'] + p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE) + out, err = p.communicate() + if p.returncode != 0: + print(err.decode('utf-8')) + exit(2) + + +def to_json(): + stats = defaultdict(list) + for dirpath, dnames, file_names in os.walk(DATA_DIR): + for file_name in file_names: + path = os.path.join(dirpath, file_name) + if path.endswith('.csv'): + with open(path, encoding='utf-16') as fp: + for line in DictReader(fp): + stats[line['Package Name']].append(line) + + # Sort + for pkg, lines in stats.items(): + stats[pkg] = sorted(lines, key=lambda x: x['Date']) + + # Write + for pkg, lines in stats.items(): + with open(os.path.join(DATA_DIR, '{}.json'.format(pkg)), 'w', encoding='utf-8') as fp: + json.dump(lines, fp, indent=4) + + +if __name__ == '__main__': + if len(sys.argv) != 2: + print("Usage: python google_play.py ACCOUNT_ID") + exit(1) + + download_csvs(sys.argv[1]) + to_json()