Spaces:
Build error
Build error
Update app.py
Browse files123436745691011127891415161011121314181915161721222324252627282930313233343536373839404142434445464748495051525354555657585918192021222324from simple_salesforce import Salesforceimport requestsimport osfrom dotenv import load_dotenv
# Load environment variables from .env fileload_dotenv()# Salesforce loginsf = Salesforce(username='your_username', password='your_password', security_token='your_security_token')
# Salesforce login credentialssf_username = os.getenv('SF_USERNAME')sf_password = os.getenv('SF_PASSWORD')sf_security_token = os.getenv('SF_SECURITY_TOKEN')# URL of the image you want to downloadurl = 'https://ddl00000ao4u7uaj-dev-ed.develop.my.salesforce.com/sfc/dist/version/renditionDownload?rendition=ORIGINAL_Jpg&versionId=068dL000005Qu25&operationContext=DELIVERY&contentId=05TdL0000060xRF&page=0&d=/a/dL0000002yZZ/L_lC3MGQhWHNFIOXncHjmz6jw6D4fdTgCBu6'
# Ensure environment variables are setif not all([sf_username, sf_password, sf_security_token]): raise ValueError("Please set SF_USERNAME, SF_PASSWORD, and SF_SECURITY_TOKEN environment variables.")# Adding session ID as authorization header to request the imageheaders = { 'Authorization': 'Bearer ' + sf.session_id}
# Folder where the files will be savedsave_folder = r'D:\salesforce-usecase\Testimages'# Sending a request to fetch the imageresponse = requests.get(url, headers=headers)
# Ensure the folder existsif not os.path.exists(save_folder): os.makedirs(save_folder)
# Authenticate to Salesforce (with production domain)sf = Salesforce(username=sf_username, password=sf_password, security_token=sf_security_token, domain='login')
# Retrieve CustomerData__c records with non-null picture__c fieldsquery = """SELECT Id, Name, picture__cFROM CustomerData__cWHERE picture__c != null"""results = sf.query_all(query)
# Process each record and check the URLfor record in results['records']: picture_url = record['picture__c'] print(f"Picture URL for {record['Name']}: {picture_url}")
# Verify if the URL redirects to an image response = requests.head(picture_url, allow_redirects=True) if response.status_code == 200: content_type = response.headers.get('Content-Type') if content_type and 'image' in content_type: print(f"Redirects to an image: {picture_url}") # Download the image response = requests.get(picture_url) file_name = f"{record['Name']}.jpg" # Assuming images are JPEG file_path = os.path.join(save_folder, file_name) with open(file_path, 'wb') as file: file.write(response.content) print(f"Downloaded {file_name} to {file_path}") else: print(f"URL does not lead to an image: {picture_url}") else: print(f"Failed to access URL: {picture_url}, Status code: {response.status_code}")
print("Process complete!")# Save the image if request is successfulif response.status_code == 200: with open('john_image.jpg', 'wb') as f: f.write(response.content) print('Image downloaded successfully')else: print('Failed to download the image. Status code:', response.status_code)app.py+17-52