Here is my code
import requests
import uuid # Import the UUID library for generating unique keys
# Configure the API endpoint URL and your API key
api_url = ‘https://www.downloadbrandlogos.com/wp-json/wpdm/v1/packages’
api_key = ‘your API key’
# Set up headers with API key for authentication
headers = {
‘Authorization’: f’Bearer {api_key}’
}
# Define a list of dictionaries, each specifying a file with its title and file path
files_data = [
{
‘title’: ‘Kerala Blasters FC.png’,
‘path’: ‘/storage/emulated/0/Download/photo.jpg’,
},
# Add more files as needed
]
# Generate unique keys for files and associate them with file paths
file_key_mapping = {}
for file_info in files_data:
key = str(uuid.uuid4()) # Generate a unique key using UUID
file_path = file_info[‘path’]
file_key_mapping[key] = file_path
# Use the generated keys in download_params
download_params = {
‘title’: ‘Kerala Blasters FC.pdf’,
‘author’: ‘Ken’,
‘status’: ‘publish’, # Set the status to “publish”
‘files’: file_key_mapping # Use the generated keys as file identifiers
}
# Create an empty list to store file attachments
files_to_upload = []
# Iterate through the list of files and create attachments
for key, file_path in file_key_mapping.items():
files_to_upload.append((‘file[]’, (key, open(file_path, ‘rb’))))
# Make a POST request to create the downloadable files
response = requests.post(api_url, headers=headers, data=download_params, files=files_to_upload)
# Check the response
if response.status_code == 201:
print(‘Downloadable files created successfully!’)
# Extract the package ID from the API response
package_id = response.json().get(‘id’)
if package_id:
# Use the package ID to attach the file to the “Attach Files” section
attachment_url = f'{api_url}/{package_id}/attach’
attachment_data = {
‘media’: ‘1’ # Attach to the “Attach Files” section (1 for yes, 0 for no)
}
attachment_response = requests.post(attachment_url, headers=headers, data=attachment_data)
if attachment_response.status_code == 200:
print(‘File attached to “Attach Files” section successfully!’)
else:
print(‘Failed to attach file to “Attach Files” section.’)
print(attachment_response.text)
else:
print(‘Package ID not found in API response.’)
else:
print(‘Failed to create the downloadable files.’)
print(response.text)
How can I achieve this please