Repository navigation
Expand file tree
/
Copy pathandroid_installs.py
More file actions
206 lines (170 loc) · 7.45 KB
/
Copy pathandroid_installs.py
File metadata and controls
206 lines (170 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
"""Update data/android/installed.csv from Play Console bulk "installs" reports
in Google Cloud Storage — automating the currently-manual Statistics export.
Play Console writes monthly CSV reports to a private GCS bucket
(`gs://pubsite_prod_rev_<id>/`). This reads the per-app installs *overview*
reports and writes the daily install base into `installed.csv`, preserving the
pre-report history and the manual Notes column.
Usage
-----
# inspect which report files exist (auth required, no writes)
uv run android_installs.py --bucket pubsite_prod_rev_XXXX list
# print the daily series as date,value (no writes)
uv run android_installs.py --bucket pubsite_prod_rev_XXXX csv
# update data/android/installed.csv in place (merges + keeps notes)
uv run android_installs.py --bucket pubsite_prod_rev_XXXX update
Auth
----
A Google service account added as a Play Console user with the "Download bulk
reports (read-only)" permission, so it can read the reports bucket. Provide the
key via `--credentials` or `GOOGLE_APPLICATION_CREDENTIALS`. Find the bucket in
Play Console -> Download reports -> Statistics -> "Copy Cloud Storage URI"
(the `pubsite_prod_rev_...` part).
Note: the install-base column defaults to "Active Device Installs" (the bulk
report's equivalent of the Statistics "Installed audience"). Validate against
the current installed.csv on the first real run; override with --metric-column
if needed.
"""
from __future__ import annotations
import csv
import io
import os
from urllib.parse import quote
import click
import requests
SCOPE = "https://www.googleapis.com/auth/devstorage.read_only"
GCS = "https://storage.googleapis.com/storage/v1"
DEFAULT_PACKAGE = "net.activitywatch.android"
CANON = "data/android/installed.csv"
# Consistent key location shared by local runs, agents, and CI.
DEFAULT_CREDENTIALS = os.path.expanduser("~/.config/activitywatch/play-sa.json")
DEFAULT_METRIC_COLUMN = "Active Device Installs"
INSTALLED_HEADER = ["Date", "Active Device Installs", "Notes"]
def _token(credentials: str | None) -> str:
import google.auth
from google.auth.transport.requests import Request
from google.oauth2 import service_account
credentials = credentials or (
DEFAULT_CREDENTIALS if os.path.exists(DEFAULT_CREDENTIALS) else None
)
if credentials:
creds = service_account.Credentials.from_service_account_file(
credentials, scopes=[SCOPE]
)
else:
creds, _ = google.auth.default(scopes=[SCOPE])
creds.refresh(Request())
return creds.token
def list_report_files(bucket: str, package: str, token: str) -> list[str]:
"""Names of the installs *overview* report objects for the app."""
prefix = f"stats/installs/installs_{package}_"
names, page = [], None
while True:
params = {"prefix": prefix}
if page:
params["pageToken"] = page
r = requests.get(
f"{GCS}/b/{bucket}/o",
params=params,
headers={"Authorization": f"Bearer {token}"},
timeout=60,
)
r.raise_for_status()
data = r.json()
names += [o["name"] for o in data.get("items", [])]
page = data.get("nextPageToken")
if not page:
break
return sorted(n for n in names if n.endswith("_overview.csv"))
def _download(bucket: str, name: str, token: str) -> bytes:
r = requests.get(
f"{GCS}/b/{bucket}/o/{quote(name, safe='')}",
params={"alt": "media"},
headers={"Authorization": f"Bearer {token}"},
timeout=120,
)
r.raise_for_status()
return r.content
def parse_report(raw: bytes, metric_column: str) -> dict[str, str]:
"""Parse one bulk installs report (UTF-16) into {date: install_base}."""
text = raw.decode("utf-16") # Play Console bulk reports are UTF-16 (LE + BOM)
out = {}
for row in csv.DictReader(io.StringIO(text)):
date = (row.get("Date") or "").strip()
value = (row.get(metric_column) or "").strip().replace(",", "")
if date and value:
out[date] = value
# Drop only the trailing-zero suffix: Play leaves not-yet-finalized recent
# days as "0", which would otherwise appear as spurious dips. Internal zeros
# (rare but valid — e.g. a reporting gap mid-series) are kept as-is.
dates = sorted(out)
while dates and out[dates[-1]] == "0":
del out[dates.pop()]
return out
def fetch_install_series(bucket, package, metric_column, credentials) -> dict[str, str]:
token = _token(credentials)
series: dict[str, str] = {}
for name in list_report_files(bucket, package, token):
series.update(parse_report(_download(bucket, name, token), metric_column))
return series
def merge_into_installed(series: dict[str, str], path: str = CANON) -> dict[str, list]:
"""Overlay the report's daily values onto installed.csv, keeping notes."""
rows: dict[str, list] = {}
try:
with open(path, newline="") as f:
reader = csv.reader(f)
next(reader, None) # header
for r in reader:
if r:
rows[r[0]] = [r[1], r[2] if len(r) > 2 else ""]
except FileNotFoundError:
pass
for date, value in series.items():
note = rows.get(date, ["", ""])[1]
rows[date] = [value, note]
return rows
def write_installed(rows: dict[str, list], path: str = CANON) -> None:
with open(path, "w", newline="") as f:
w = csv.writer(f, lineterminator="\n")
w.writerow(INSTALLED_HEADER)
for date in sorted(rows):
w.writerow([date, rows[date][0], rows[date][1]])
# --- CLI -------------------------------------------------------------------
@click.group(help=__doc__)
@click.option("--bucket", required=True, help="GCS bucket, e.g. pubsite_prod_rev_XXXX")
@click.option("--package", default=DEFAULT_PACKAGE, show_default=True)
@click.option("--credentials", envvar="GOOGLE_APPLICATION_CREDENTIALS",
help="Service-account JSON path (or GOOGLE_APPLICATION_CREDENTIALS).")
@click.option("--metric-column", default=DEFAULT_METRIC_COLUMN, show_default=True)
@click.pass_context
def cli(ctx, bucket, package, credentials, metric_column):
ctx.obj = dict(bucket=bucket, package=package,
credentials=credentials, metric_column=metric_column)
@cli.command("list")
@click.pass_obj
def list_cmd(o):
"""List the installs report files available in the bucket."""
for name in list_report_files(o["bucket"], o["package"], _token(o["credentials"])):
click.echo(name)
@cli.command("csv")
@click.pass_obj
def csv_cmd(o):
"""Print the daily install base as date,value (no writes)."""
series = fetch_install_series(o["bucket"], o["package"], o["metric_column"], o["credentials"])
for date in sorted(series):
click.echo(f"{date},{series[date]}")
@cli.command("update")
@click.option("--path", default=CANON, show_default=True)
@click.pass_obj
def update_cmd(o, path):
"""Update data/android/installed.csv in place (merge, keep notes)."""
series = fetch_install_series(o["bucket"], o["package"], o["metric_column"], o["credentials"])
if not series:
raise SystemExit("No install data found — check bucket/package/permissions.")
rows = merge_into_installed(series, path)
write_installed(rows, path)
dates = sorted(rows)
click.echo(f"Wrote {path}: {len(dates)} rows, {dates[0]}..{dates[-1]} "
f"(latest {rows[dates[-1]][0]})")
if __name__ == "__main__":
cli()