Initial commit

This commit is contained in:
Nikolai Kristiansen committed 2017-09-27 13:31:33 +02:00
commit 263c1754e9
3 files changed
+67

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
*.swp
.idea
*.pyc
data/
venv/
+9
View File
@@ -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
+53
View File
@@ -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()