Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,59 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
| 3 |
import requests
|
| 4 |
-
import
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
else:
|
| 51 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from simple_salesforce import Salesforce
|
|
|
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Salesforce login credentials
|
| 10 |
+
sf_username = os.getenv('SF_USERNAME')
|
| 11 |
+
sf_password = os.getenv('SF_PASSWORD')
|
| 12 |
+
sf_security_token = os.getenv('SF_SECURITY_TOKEN')
|
| 13 |
+
|
| 14 |
+
# Ensure environment variables are set
|
| 15 |
+
if not all([sf_username, sf_password, sf_security_token]):
|
| 16 |
+
raise ValueError("Please set SF_USERNAME, SF_PASSWORD, and SF_SECURITY_TOKEN environment variables.")
|
| 17 |
+
|
| 18 |
+
# Folder where the files will be saved
|
| 19 |
+
save_folder = r'D:\salesforce-usecase\Testimages'
|
| 20 |
+
|
| 21 |
+
# Ensure the folder exists
|
| 22 |
+
if not os.path.exists(save_folder):
|
| 23 |
+
os.makedirs(save_folder)
|
| 24 |
+
|
| 25 |
+
# Authenticate to Salesforce (with production domain)
|
| 26 |
+
sf = Salesforce(username=sf_username, password=sf_password, security_token=sf_security_token, domain='login')
|
| 27 |
+
|
| 28 |
+
# Retrieve CustomerData__c records with non-null picture__c fields
|
| 29 |
+
query = """
|
| 30 |
+
SELECT Id, Name, picture__c
|
| 31 |
+
FROM CustomerData__c
|
| 32 |
+
WHERE picture__c != null
|
| 33 |
+
"""
|
| 34 |
+
results = sf.query_all(query)
|
| 35 |
+
|
| 36 |
+
# Process each record and check the URL
|
| 37 |
+
for record in results['records']:
|
| 38 |
+
picture_url = record['picture__c']
|
| 39 |
+
print(f"Picture URL for {record['Name']}: {picture_url}")
|
| 40 |
+
|
| 41 |
+
# Verify if the URL redirects to an image
|
| 42 |
+
response = requests.head(picture_url, allow_redirects=True)
|
| 43 |
+
if response.status_code == 200:
|
| 44 |
+
content_type = response.headers.get('Content-Type')
|
| 45 |
+
if content_type and 'image' in content_type:
|
| 46 |
+
print(f"Redirects to an image: {picture_url}")
|
| 47 |
+
# Download the image
|
| 48 |
+
response = requests.get(picture_url)
|
| 49 |
+
file_name = f"{record['Name']}.jpg" # Assuming images are JPEG
|
| 50 |
+
file_path = os.path.join(save_folder, file_name)
|
| 51 |
+
with open(file_path, 'wb') as file:
|
| 52 |
+
file.write(response.content)
|
| 53 |
+
print(f"Downloaded {file_name} to {file_path}")
|
| 54 |
+
else:
|
| 55 |
+
print(f"URL does not lead to an image: {picture_url}")
|
| 56 |
else:
|
| 57 |
+
print(f"Failed to access URL: {picture_url}, Status code: {response.status_code}")
|
| 58 |
+
|
| 59 |
+
print("Process complete!")
|