33"""
44
55import json
6- import os
76import sys
87from datetime import datetime
8+ from pathlib import Path
99
1010import requests
1111
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
105119if __name__ == "__main__" :
0 commit comments