1
Fork 0

Output results to CSV

This commit is contained in:
Jake Howard 2023-05-04 09:12:45 +01:00
parent 0af784b4e4
commit 3bba01a3dd
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 9 additions and 2 deletions

2
.gitignore vendored
View file

@ -174,3 +174,5 @@ poetry.toml
pyrightconfig.json pyrightconfig.json
# End of https://www.toptal.com/developers/gitignore/api/python # End of https://www.toptal.com/developers/gitignore/api/python
report-*.csv

View file

@ -3,6 +3,7 @@ import argparse
import os import os
import datetime import datetime
from itertools import count from itertools import count
import csv
PLAUSIBLE_HOSTNAME = os.environ.get("PLAUSIBLE_HOSTNAME", "plausible.io") PLAUSIBLE_HOSTNAME = os.environ.get("PLAUSIBLE_HOSTNAME", "plausible.io")
@ -19,6 +20,7 @@ def get_pages(site_id: str, session: requests.Session):
today = datetime.datetime.now().date().isoformat() today = datetime.datetime.now().date().isoformat()
for page_num in count(start=1): for page_num in count(start=1):
print("Fetching page", page_num)
response = session.get(f"https://{PLAUSIBLE_HOSTNAME}/api/v1/stats/breakdown", params={ response = session.get(f"https://{PLAUSIBLE_HOSTNAME}/api/v1/stats/breakdown", params={
"site_id": site_id, "site_id": site_id,
"period": "custom", "period": "custom",
@ -41,8 +43,11 @@ def main():
session = requests.Session() session = requests.Session()
session.headers["Authorization"] = "Bearer " + os.environ["PLAUSIBLE_API_TOKEN"] session.headers["Authorization"] = "Bearer " + os.environ["PLAUSIBLE_API_TOKEN"]
for page in get_pages(args.site_id, session): with open(f"report-{args.site_id}.csv", "w") as f:
print(page["page"], page["visitors"]) writer = csv.DictWriter(f, fieldnames=["page", "visitors"])
for page in get_pages(args.site_id, session):
writer.writerow(page)
if __name__ == "__main__": if __name__ == "__main__":