Ajay98 commited on
Commit
af94afd
·
verified ·
1 Parent(s): 084c2db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -52
app.py CHANGED
@@ -1,59 +1,24 @@
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!")
 
1
  from simple_salesforce import Salesforce
2
  import requests
 
 
3
 
4
+ # Salesforce login
5
+ sf = Salesforce(username='your_username', password='your_password', security_token='your_security_token')
6
 
7
+ # URL of the image you want to download
8
+ url = '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'
 
 
9
 
10
+ # Adding session ID as authorization header to request the image
11
+ headers = {
12
+ 'Authorization': 'Bearer ' + sf.session_id
13
+ }
14
 
15
+ # Sending a request to fetch the image
16
+ response = requests.get(url, headers=headers)
17
 
18
+ # Save the image if request is successful
19
+ if response.status_code == 200:
20
+ with open('john_image.jpg', 'wb') as f:
21
+ f.write(response.content)
22
+ print('Image downloaded successfully')
23
+ else:
24
+ print('Failed to download the image. Status code:', response.status_code)