Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,33 @@
|
|
| 1 |
from simple_salesforce import Salesforce
|
| 2 |
import requests
|
| 3 |
-
|
| 4 |
|
| 5 |
-
# Salesforce
|
| 6 |
-
sf = Salesforce(username='alwaysajay411@gmail.com',
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from simple_salesforce import Salesforce
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Salesforce credentials
|
| 6 |
+
sf = Salesforce(username='alwaysajay411@gmail.com',
|
| 7 |
+
password='Anusha@310819',
|
| 8 |
+
security_token='Anusha@310819')
|
| 9 |
|
| 10 |
+
# Query Salesforce custom object for image file details
|
| 11 |
+
query = "SELECT Id, Name, FileExtension, ContentDocumentId FROM CustomObjectName__c WHERE Name = 'RecordName'"
|
| 12 |
+
response = sf.query(query)
|
| 13 |
+
records = response['records']
|
| 14 |
|
| 15 |
+
# Create a folder to save images locally if it doesn't exist
|
| 16 |
+
local_folder = 'salesforce_images'
|
| 17 |
+
if not os.path.exists(local_folder):
|
| 18 |
+
os.makedirs(local_folder)
|
| 19 |
|
| 20 |
+
# Iterate through records and download images
|
| 21 |
+
for record in records:
|
| 22 |
+
file_id = record['ContentDocumentId']
|
| 23 |
+
file_details = sf.ContentDocument.get(file_id)
|
| 24 |
+
file_name = file_details['Title'] + '.' + file_details['FileExtension']
|
| 25 |
+
file_url = f'/services/data/v50.0/sobjects/ContentVersion/{file_id}/VersionData'
|
| 26 |
+
|
| 27 |
+
# Download file content
|
| 28 |
+
file_response = requests.get(sf.base_url + file_url, headers={'Authorization': 'Bearer ' + sf.session_id})
|
| 29 |
+
|
| 30 |
+
# Save the image file locally
|
| 31 |
+
with open(os.path.join(local_folder, file_name), 'wb') as f:
|
| 32 |
+
f.write(file_response.content)
|
| 33 |
+
print(f'Downloaded {file_name}')
|