1
Fork 0

Add pagination

This commit is contained in:
Jake Howard 2023-05-04 09:07:58 +01:00
parent 5fbc979f69
commit 0af784b4e4
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -2,6 +2,7 @@ import requests
import argparse
import os
import datetime
from itertools import count
PLAUSIBLE_HOSTNAME = os.environ.get("PLAUSIBLE_HOSTNAME", "plausible.io")
@ -14,25 +15,34 @@ def parse_args():
return parser.parse_args()
def get_pages(site_id: str, session: requests.Session):
today = datetime.datetime.now().date().isoformat()
for page_num in count(start=1):
response = session.get(f"https://{PLAUSIBLE_HOSTNAME}/api/v1/stats/breakdown", params={
"site_id": site_id,
"period": "custom",
"date": "2000-01-01," + today,
"property": "event:page",
"page": page_num
})
response.raise_for_status()
results = response.json()["results"]
if not results:
break
yield from results
def main():
args = parse_args()
session = requests.Session()
session.headers["Authorization"] = "Bearer " + os.environ["PLAUSIBLE_API_TOKEN"]
response = session.get(f"https://{PLAUSIBLE_HOSTNAME}/api/v1/stats/breakdown", params={
"site_id": args.site_id,
"period": "custom",
"date": "2000-01-01," + datetime.datetime.now().date().isoformat(),
"property": "event:page"
})
response.raise_for_status()
print(response.json())
for page in get_pages(args.site_id, session):
print(page["page"], page["visitors"])
if __name__ == "__main__":