Skip to content

Commit 1ff19e8

Browse files
committed
update to use pathlib in scrape_pbr example (but see also reports-gen PBR report for implemented code)
1 parent 2ae4fbd commit 1ff19e8

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

scripts/scrapers/scrape_pbr.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"""
44

55
import json
6-
import os
76
import sys
87
from datetime import datetime
8+
from pathlib import Path
99

1010
import requests
1111

@@ -68,6 +68,10 @@
6868
latitude
6969
longitude
7070
}
71+
meta {
72+
key
73+
value
74+
}
7175
}
7276
}
7377
}
@@ -92,14 +96,24 @@ def fetch_pbr_projects(output_path: str | None = None):
9296
projects = data["data"]["searchProjects"]["results"]
9397
print(f"Fetched {len(projects)} projects.")
9498

99+
timestamped_name = f"pbr_projects_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
100+
95101
if output_path is None:
96-
output_dir = os.path.join(os.path.expanduser("~"), "PBRProjects")
97-
os.makedirs(output_dir, exist_ok=True)
98-
output_path = os.path.join(output_dir, f"pbr_projects_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json")
102+
output_dir = Path.home() / "PBRProjects"
103+
output_dir.mkdir(parents=True, exist_ok=True)
104+
output_file = output_dir / timestamped_name
105+
elif Path(output_path).is_dir() or output_path.endswith(("/", "\\")):
106+
# If a directory is provided, write a timestamped JSON file into it.
107+
output_dir = Path(output_path)
108+
output_dir.mkdir(parents=True, exist_ok=True)
109+
output_file = output_dir / timestamped_name
110+
else:
111+
output_file = Path(output_path)
112+
output_file.parent.mkdir(parents=True, exist_ok=True)
99113

100-
with open(output_path, "w", encoding="utf-8") as f:
114+
with output_file.open("w", encoding="utf-8") as f:
101115
json.dump(projects, f, indent=2)
102-
print(f"Saved project list to {output_path}")
116+
print(f"Saved project list to {output_file}")
103117

104118

105119
if __name__ == "__main__":

0 commit comments

Comments
 (0)