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
# End of https://www.toptal.com/developers/gitignore/api/python
report-*.csv

View File

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