Spaces:
Build error
Build error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859from simple_salesforce import Salesforceimport requestsimport osfrom dotenv import load_dotenv# Load environment variables from .env fileload_dotenv()# Salesforce login credentialssf_username = os.getenv('SF_USERNAME')sf_password = os.getenv('SF_PASSWORD')sf_security_token = os.getenv('SF_SECURITY_TOKEN')# 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.")# Folder where the files will be savedsave_folder = r'D:\salesforce-usecase\Testimages'# 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 imageresponse = 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 imageresponse = requests.get(picture_url)file_name = f"{record['Name']}.jpg" # Assuming images are JPEGfile_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!")123456789101112131415161718192021222324from simple_salesforce import Salesforceimport requests# Salesforce loginsf = Salesforce(username='your_username', password='your_password', security_token='your_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'# Adding session ID as authorization header to request the imageheaders = {'Authorization': 'Bearer ' + sf.session_id}# Sending a request to fetch the imageresponse = requests.get(url, headers=headers)# 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