99JSON_FOLDER = "jsonFiles"
1010RAW_BASE_URL = f"https://raw.githubusercontent.com/{ REPO_ORG } /{ REPO_NAME } /{ BRANCH } /{ JSON_FOLDER } "
1111
12- def generate_sitemap ( ):
13- # Load all sitemap entries
12+ def generate_sitemap_for_folder ( folder_path , folder_name ):
13+ """Generates a sitemap for a specific folder."""
1414 sitemap_entries = []
15- # Iterate through subfolders in the JSON folder
16- for root , dirs , files in os .walk (JSON_FOLDER ):
17- for file_name in files :
18- if file_name .endswith (".json" ):
19- json_path = os .path .join (root , file_name )
20- try :
21- with open (json_path , "r" , encoding = "utf-8" ) as f :
22- data = json .load (f )
23- # Extract URL and frequency from the JSON file
24- url = data .get ("url" , f"{ RAW_BASE_URL } /{ os .path .relpath (json_path , JSON_FOLDER )} " )
25- frequency = data .get ("frequency" , "never" ) # Default to 'weekly' if not specified
26- lastmod = datetime .now ().strftime ("%Y-%m-%d" ) # Use current date as last modified
27-
28- # Append the entry to the sitemap
29- sitemap_entries .append ({
30- "url" : url ,
31- "lastmod" : lastmod ,
32- "changefreq" : frequency
33- })
34- except json .JSONDecodeError :
35- print (f"Error decoding JSON in file: { json_path } " )
36- except Exception as e :
37- print (f"Unexpected error with file { json_path } : { e } " )
15+ for file_name in os .listdir (folder_path ):
16+ if file_name .endswith (".json" ):
17+ file_path = os .path .join (folder_path , file_name )
18+ with open (file_path , "r" , encoding = "utf-8" ) as f :
19+ data = json .load (f )
20+ url = data .get ("url" , f"{ RAW_BASE_URL } /{ folder_name } /{ file_name } " )
21+ frequency = data .get ("frequency" , "never" )
22+ lastmod = data .get ("lastmod" , datetime .utcnow ().strftime ("%Y-%m-%d" ))
23+ sitemap_entries .append ({
24+ "url" : url ,
25+ "lastmod" : lastmod ,
26+ "changefreq" : frequency
27+ })
3828
3929 # Generate sitemap XML
4030 sitemap = '<?xml version="1.0" encoding="UTF-8"?>\n '
@@ -47,11 +37,19 @@ def generate_sitemap():
4737 sitemap += " </url>\n "
4838 sitemap += "</urlset>"
4939
50- # Save sitemap
51- with open ("sitemap.xml" , "w" , encoding = "utf-8" ) as f :
40+ # Save sitemap to folder
41+ sitemap_file = os .path .join (folder_path , "sitemap.xml" )
42+ with open (sitemap_file , "w" , encoding = "utf-8" ) as f :
5243 f .write (sitemap )
5344
5445 print ("Sitemap generated successfully." )
5546
47+ def generate_all_sitemaps ():
48+ """Generates sitemaps for all folders in the JSON_FOLDER."""
49+ for folder_name in os .listdir (JSON_FOLDER ):
50+ folder_path = os .path .join (JSON_FOLDER , folder_name )
51+ if os .path .isdir (folder_path ):
52+ generate_sitemap_for_folder (folder_path , folder_name )
53+
5654if __name__ == "__main__" :
57- generate_sitemap ()
55+ generate_all_sitemaps ()
0 commit comments