Ajay98 commited on
Commit
ef6d3fd
·
verified ·
1 Parent(s): 367787a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -49
app.py CHANGED
@@ -1,51 +1,59 @@
1
-
2
- import os
3
  import requests
4
- import json
5
- from salesforce import Salesforce
6
- from jwt import encode
7
- import base64
8
- from cryptography.hazmat.primitives import serialization
9
- from cryptography.hazmat.backends import default_backend
10
-
11
- # Salesforce credentials
12
- CONSUMER_KEY = os.environ['SALESFORCE_CONSUMER_KEY']
13
- CONSUMER_SECRET = os.environ['SALESFORCE_CONSUMER_SECRET']
14
- USERNAME = os.environ['SALESFORCE_USERNAME']
15
- PASSWORD = os.environ['SALESFORCE_PASSWORD']
16
- SECURITY_TOKEN = os.environ['SALESFORCE_SECURITY_TOKEN']
17
-
18
- # Custom object and field
19
- OBJECT_NAME = 'Guest__c'
20
- FIELD_NAME = 'Adhar_Card_Photo__c'
21
-
22
- # Authenticate and get access token
23
- def get_access_token():
24
- payload = {
25
- 'grant_type': 'password',
26
- 'client_id': CONSUMER_KEY,
27
- 'client_secret': CONSUMER_SECRET,
28
- 'username': USERNAME,
29
- 'password': PASSWORD + SECURITY_TOKEN
30
- }
31
- response = requests.post('(link unavailable)', data=payload)
32
- return response.json()['access_token']
33
-
34
- # Upload image to Salesforce
35
- def upload_image(image_path, image_name):
36
- access_token = get_access_token()
37
- sf = Salesforce(instance_url='(link unavailable)',
38
- session_id=access_token)
39
-
40
- # Read image content
41
- with open(image_path, 'rb') as image_file:
42
- image_content = image_file.read()
43
- image_base64 = base64.b64encode(image_content).decode('utf-8')
44
-
45
- # Create or update record
46
- record = sf.query(f"SELECT Id FROM {OBJECT_NAME} WHERE Name = '{image_name}'")
47
- if record['records']:
48
- record_id = record['records'][0]['Id']
49
- sf.update(OBJECT_NAME, {'Id': record_id, FIELD_NAME: image_base64})
 
 
 
 
 
 
 
50
  else:
51
- sf.create(OBJECT_NAME, {FIELD_NAME: image_base64, 'Name': image_name})
 
 
 
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!")