Output results to CSV
This commit is contained in:
parent
0af784b4e4
commit
3bba01a3dd
2 changed files with 9 additions and 2 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -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
|
||||||
|
|
|
@ -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"]
|
||||||
|
|
||||||
|
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):
|
for page in get_pages(args.site_id, session):
|
||||||
print(page["page"], page["visitors"])
|
writer.writerow(page)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue