import json
import os
def load_cookies(filepath):
"""Loads cookies from a JSON file.
Args:
filepath (str): The path to the JSON file containing cookies.
Returns:
dict: A dictionary of cookies, or an empty dictionary if the file
doesn't exist or is invalid.
"""
try:
with open(filepath, 'r') as f:
cookies = json.load(f)
return cookies
except FileNotFoundError:
print(f"Cookie file not found: {filepath}")
return {}
except json.JSONDecodeError:
print(f"Invalid JSON format in cookie file: {filepath}")
return {}
def create_dummy_cookies(filepath):
"""Creates a dummy cookie file for testing.
Args:
filepath (str): The path to the file to create.
"""
cookies = {
"example_site": {"session_id": "1234567890", "user_id": "user123"},
"another_site": {"auth_token": "abcdef123456"}
}
with open(filepath, 'w') as f:
json.dump(cookies, f, indent=4)
if __name__ == '__main__':
cookie_file = "cookies.json" # Define the cookie file path
# Create a dummy cookie file if it doesn't exist
if not os.path.exists(cookie_file):
create_dummy_cookies(cookie_file)
cookies = load_cookies(cookie_file)
print(cookies)
Add your comment