-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plausible-get.py
74 lines (62 loc) · 1.92 KB
/
plausible-get.py
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
"""
Script to download data from the Plausible API.
https://plausible.io/docs/stats-api#top-pages
"""
import os
import urllib3 # pip install "urllib3>=2"
# Get your API key from https://plausible.io/settings
API_KEY = TODO_ENTER_YOUR_API_KEY
HEADERS = {"Authorization": f"Bearer {API_KEY}"}
OUTPUT_DIR = "output"
def get_data(http: urllib3.PoolManager, prop: str) -> int:
print(f"Getting data for {prop}")
page = 1
requests = 0
while True:
url = (
"https://plausible.io/api/v1/stats/breakdown"
"?site_id=docs.python.org"
"&period=6mo"
f"&property={prop}"
f"&limit=1000&page={page}"
"&metrics=visitors,pageviews,bounce_rate,visit_duration,visits,events"
)
resp = http.request("GET", url, headers=HEADERS)
requests += 1
print(page, resp.status, len(resp.json()["results"]))
if not len(resp.json()["results"]):
break
output_filename = f"{OUTPUT_DIR}/{prop.replace(':', '-')}-{page:02d}.json"
with open(output_filename, "w") as f:
f.write(resp.data.decode())
page += 1
return requests
def main() -> None:
http = urllib3.PoolManager()
requests = 0
os.system(f"mkdir -p {OUTPUT_DIR}")
# https://plausible.io/docs/stats-api#properties
for prop in (
"event:page",
"visit:entry_page",
"visit:exit_page",
"visit:source",
"visit:referrer",
"visit:utm_medium",
"visit:utm_source",
"visit:utm_campaign",
"visit:utm_content",
"visit:utm_term",
"visit:device",
"visit:browser",
"visit:browser_version",
"visit:os",
"visit:os_version",
"visit:country",
"visit:region",
"visit:city",
):
requests += get_data(http, prop)
print(f"Total requests made: {requests}")
if __name__ == "__main__":
main()