Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
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.readonly']
|
| 11 |
+
SERVICE_ACCOUNT_FILE = 'service_account.json'
|
| 12 |
+
credentials = service_account.Credentials.from_service_account_file(
|
| 13 |
+
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
| 14 |
+
drive_service = build('drive', 'v3', credentials=credentials)
|
| 15 |
+
|
| 16 |
+
# Salesforce Authentication
|
| 17 |
+
sf = Salesforce(
|
| 18 |
+
username='YOUR_SALESFORCE_USERNAME',
|
| 19 |
+
password='YOUR_SALESFORCE_PASSWORD',
|
| 20 |
+
security_token='YOUR_SALESFORCE_SECURITY_TOKEN',
|
| 21 |
+
domain='login'
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Local folder to save retrieved files
|
| 25 |
+
LOCAL_FOLDER = 'retrieved_files'
|
| 26 |
+
if not os.path.exists(LOCAL_FOLDER):
|
| 27 |
+
os.makedirs(LOCAL_FOLDER)
|
| 28 |
+
|
| 29 |
+
def get_salesforce_files(custom_object_id):
|
| 30 |
+
query = f"SELECT Id, Title, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = '{custom_object_id}'"
|
| 31 |
+
results = sf.query(query)
|
| 32 |
+
return results['records']
|
| 33 |
+
|
| 34 |
+
def download_google_drive_file(file_id, file_name):
|
| 35 |
+
request = drive_service.files().get_media(fileId=file_id)
|
| 36 |
+
file_path = os.path.join(LOCAL_FOLDER, file_name)
|
| 37 |
+
with open(file_path, 'wb') as f:
|
| 38 |
+
downloader = MediaIoBaseDownload(f, request)
|
| 39 |
+
done = False
|
| 40 |
+
while done is False:
|
| 41 |
+
status, done = downloader.next_chunk()
|
| 42 |
+
print(f"Download progress: {int(status.progress() * 100)}%")
|
| 43 |
+
return file_path
|
| 44 |
+
|
| 45 |
+
def save_salesforce_files(custom_object_id):
|
| 46 |
+
files = get_salesforce_files(custom_object_id)
|
| 47 |
+
for file in files:
|
| 48 |
+
content_doc_id = file['ContentDocumentId']
|
| 49 |
+
content_doc = sf.ContentDocument.get(content_doc_id)
|
| 50 |
+
google_drive_id = content_doc['Title'] # Assuming Title is the Google Drive file ID
|
| 51 |
+
google_drive_file_name = content_doc['Title'] + '.jpg' # Replace '.jpg' with actual extension
|
| 52 |
+
print(f"Downloading file {google_drive_file_name} from Google Drive...")
|
| 53 |
+
file_path = download_google_drive_file(google_drive_id, google_drive_file_name)
|
| 54 |
+
if os.path.exists(file_path):
|
| 55 |
+
print(f"File {google_drive_file_name} saved successfully to {file_path}")
|
| 56 |
+
else:
|
| 57 |
+
print(f"Failed to save file {google_drive_file_name}")
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
# Replace 'YOUR_CUSTOM_OBJECT_ID' with the Salesforce custom object Id
|
| 61 |
+
custom_object_id = 'YOUR_CUSTOM_OBJECT_ID'
|
| 62 |
+
save_salesforce_files(custom_object_id)
|