Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import json
|
|
| 5 |
from simple_salesforce import Salesforce
|
| 6 |
from google.oauth2 import service_account
|
| 7 |
from googleapiclient.discovery import build
|
|
|
|
| 8 |
|
| 9 |
# Google Drive Authentication Setup
|
| 10 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
|
@@ -15,21 +16,35 @@ drive_service = build('drive', 'v3', credentials=credentials)
|
|
| 15 |
|
| 16 |
# Salesforce Authentication
|
| 17 |
sf = Salesforce(
|
| 18 |
-
username='
|
| 19 |
-
password='
|
| 20 |
-
security_token='
|
| 21 |
domain='login'
|
| 22 |
)
|
| 23 |
|
| 24 |
# Local folder to save retrieved files
|
| 25 |
-
LOCAL_FOLDER =
|
| 26 |
if not os.path.exists(LOCAL_FOLDER):
|
| 27 |
os.makedirs(LOCAL_FOLDER)
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def download_google_drive_file(file_id, file_name):
|
| 35 |
request = drive_service.files().get_media(fileId=file_id)
|
|
@@ -37,13 +52,14 @@ def download_google_drive_file(file_id, file_name):
|
|
| 37 |
with open(file_path, 'wb') as f:
|
| 38 |
downloader = MediaIoBaseDownload(f, request)
|
| 39 |
done = False
|
| 40 |
-
while
|
| 41 |
status, done = downloader.next_chunk()
|
| 42 |
-
|
|
|
|
| 43 |
return file_path
|
| 44 |
|
| 45 |
-
def save_salesforce_files(
|
| 46 |
-
files =
|
| 47 |
for file in files:
|
| 48 |
content_doc_id = file['ContentDocumentId']
|
| 49 |
content_doc = sf.ContentDocument.get(content_doc_id)
|
|
@@ -57,6 +73,6 @@ def save_salesforce_files(custom_object_id):
|
|
| 57 |
print(f"Failed to save file {google_drive_file_name}")
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
# Replace '
|
| 61 |
-
|
| 62 |
-
save_salesforce_files(
|
|
|
|
| 5 |
from simple_salesforce import Salesforce
|
| 6 |
from google.oauth2 import service_account
|
| 7 |
from googleapiclient.discovery import build
|
| 8 |
+
from googleapiclient.http import MediaIoBaseDownload
|
| 9 |
|
| 10 |
# Google Drive Authentication Setup
|
| 11 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
|
|
|
| 16 |
|
| 17 |
# Salesforce Authentication
|
| 18 |
sf = Salesforce(
|
| 19 |
+
username=os.getenv('SF_USERNAME'),
|
| 20 |
+
password=os.getenv('SF_PASSWORD'),
|
| 21 |
+
security_token=os.getenv('SF_SECURITY_TOKEN'),
|
| 22 |
domain='login'
|
| 23 |
)
|
| 24 |
|
| 25 |
# Local folder to save retrieved files
|
| 26 |
+
LOCAL_FOLDER = r"C:\Users\bharg\Documents\Work\Sathkrutha\Retrieval"
|
| 27 |
if not os.path.exists(LOCAL_FOLDER):
|
| 28 |
os.makedirs(LOCAL_FOLDER)
|
| 29 |
|
| 30 |
+
def get_all_salesforce_files(custom_object_api_name):
|
| 31 |
+
custom_object_api_name = 'MyCustomer__c'
|
| 32 |
+
# Step 1: Query all record IDs from the custom object
|
| 33 |
+
query_all_records = f"SELECT Id FROM {custom_object_api_name}"
|
| 34 |
+
all_records = sf.query(query_all_records)
|
| 35 |
+
record_ids = [record['Id'] for record in all_records['records']]
|
| 36 |
+
|
| 37 |
+
all_files = []
|
| 38 |
+
|
| 39 |
+
# Step 2: Loop through each record ID and get linked files
|
| 40 |
+
for record_id in record_ids:
|
| 41 |
+
query_files = f"SELECT Id, Title, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = '{record_id}'"
|
| 42 |
+
results = sf.query(query_files)
|
| 43 |
+
|
| 44 |
+
if results['records']:
|
| 45 |
+
all_files.extend(results['records'])
|
| 46 |
+
|
| 47 |
+
return all_files
|
| 48 |
|
| 49 |
def download_google_drive_file(file_id, file_name):
|
| 50 |
request = drive_service.files().get_media(fileId=file_id)
|
|
|
|
| 52 |
with open(file_path, 'wb') as f:
|
| 53 |
downloader = MediaIoBaseDownload(f, request)
|
| 54 |
done = False
|
| 55 |
+
while not done:
|
| 56 |
status, done = downloader.next_chunk()
|
| 57 |
+
if status:
|
| 58 |
+
print(f"Download progress: {int(status.progress() * 100)}%")
|
| 59 |
return file_path
|
| 60 |
|
| 61 |
+
def save_salesforce_files(custom_object_api_name):
|
| 62 |
+
files = get_all_salesforce_files(custom_object_api_name)
|
| 63 |
for file in files:
|
| 64 |
content_doc_id = file['ContentDocumentId']
|
| 65 |
content_doc = sf.ContentDocument.get(content_doc_id)
|
|
|
|
| 73 |
print(f"Failed to save file {google_drive_file_name}")
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
# Replace 'YOUR_CUSTOM_OBJECT_API_NAME' with the Salesforce custom object API name
|
| 77 |
+
custom_object_api_name = 'MyCustomer__c'
|
| 78 |
+
save_salesforce_files(custom_object_api_name)
|