Update file handling to include encoding and correct comments

This commit is contained in:
Matheus Felipe 2025-10-11 15:21:36 -03:00
parent 193de54b6d
commit 79973a58ea
No known key found for this signature in database
GPG Key ID: E6056C95900163D1
1 changed files with 8 additions and 7 deletions

View File

@ -1,28 +1,29 @@
#!/usr/bin/env python #!/usr/bin/env python
# This module generates the listing of supported sites which can be found in # This module generates the listing of supported sites which can be found in
# sites.md. It also organizes all the sites in alphanumeric order # sites.mdx. It also organizes all the sites in alphanumeric order
import json import json
import os import os
DATA_REL_URI: str = "sherlock_project/resources/data.json" DATA_REL_URI: str = "sherlock_project/resources/data.json"
DEFAULT_ENCODING = "utf-8"
# Read the data.json file # Read the data.json file
with open(DATA_REL_URI, "r") as data_file: with open(DATA_REL_URI, "r", encoding=DEFAULT_ENCODING) as data_file:
data: dict = json.load(data_file) data: dict = json.load(data_file)
# Removes schema-specific keywords for proper processing # Removes schema-specific keywords for proper processing
social_networks: dict = data.copy() social_networks = data.copy()
social_networks.pop('$schema', None) social_networks.pop('$schema', None)
# Sort the social networks in alphanumeric order # Sort the social networks in alphanumeric order
social_networks: list = sorted(social_networks.items()) social_networks = sorted(social_networks.items())
# Make output dir where the site list will be written # Make output dir where the site list will be written
os.mkdir("output") os.mkdir("output")
# Write the list of supported sites to sites.mdx # Write the list of supported sites to sites.mdx
with open("output/sites.mdx", "w") as site_file: with open("output/sites.mdx", "w", encoding=DEFAULT_ENCODING) as site_file:
site_file.write("---\n") site_file.write("---\n")
site_file.write("title: 'List of supported sites'\n") site_file.write("title: 'List of supported sites'\n")
site_file.write("sidebarTitle: 'Supported sites'\n") site_file.write("sidebarTitle: 'Supported sites'\n")
@ -36,7 +37,7 @@ with open("output/sites.mdx", "w") as site_file:
site_file.write(f"1. [{social_network}]({url_main}) {is_nsfw}\n") site_file.write(f"1. [{social_network}]({url_main}) {is_nsfw}\n")
# Overwrite the data.json file with sorted data # Overwrite the data.json file with sorted data
with open(DATA_REL_URI, "w") as data_file: with open(DATA_REL_URI, "w", encoding=DEFAULT_ENCODING) as data_file:
sorted_data = json.dumps(data, indent=2, sort_keys=True) sorted_data = json.dumps(data, indent=2, sort_keys=True)
data_file.write(sorted_data) data_file.write(sorted_data)
data_file.write("\n") # Keep the newline after writing data data_file.write("\n") # Keep the newline after writing data