Add pagination
This commit is contained in:
parent
5fbc979f69
commit
0af784b4e4
1 changed files with 23 additions and 13 deletions
36
report.py
36
report.py
|
@ -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__":
|
||||
|
|
Loading…
Reference in a new issue