Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,11 @@ 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']
|
| 12 |
-
SERVICE_ACCOUNT_FILE =
|
| 13 |
credentials = service_account.Credentials.from_service_account_file(
|
| 14 |
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
| 15 |
drive_service = build('drive', 'v3', credentials=credentials)
|
|
@@ -22,13 +23,11 @@ sf = Salesforce(
|
|
| 22 |
domain='login'
|
| 23 |
)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 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)
|
|
@@ -46,9 +45,10 @@ def get_all_salesforce_files(custom_object_api_name):
|
|
| 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)
|
| 51 |
-
file_path = os.path.join(
|
| 52 |
with open(file_path, 'wb') as f:
|
| 53 |
downloader = MediaIoBaseDownload(f, request)
|
| 54 |
done = False
|
|
@@ -58,6 +58,7 @@ def download_google_drive_file(file_id, file_name):
|
|
| 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:
|
|
|
|
| 6 |
from google.oauth2 import service_account
|
| 7 |
from googleapiclient.discovery import build
|
| 8 |
from googleapiclient.http import MediaIoBaseDownload
|
| 9 |
+
import tempfile
|
| 10 |
|
| 11 |
# Google Drive Authentication Setup
|
| 12 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
| 13 |
+
SERVICE_ACCOUNT_FILE = 'service_account.json' # Ensure this file is uploaded to the Hugging Face space
|
| 14 |
credentials = service_account.Credentials.from_service_account_file(
|
| 15 |
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
| 16 |
drive_service = build('drive', 'v3', credentials=credentials)
|
|
|
|
| 23 |
domain='login'
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Using a temporary directory to save retrieved files
|
| 27 |
+
temp_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Function to retrieve all files from the Salesforce custom object
|
| 30 |
def get_all_salesforce_files(custom_object_api_name):
|
|
|
|
| 31 |
# Step 1: Query all record IDs from the custom object
|
| 32 |
query_all_records = f"SELECT Id FROM {custom_object_api_name}"
|
| 33 |
all_records = sf.query(query_all_records)
|
|
|
|
| 45 |
|
| 46 |
return all_files
|
| 47 |
|
| 48 |
+
# Function to download a file from Google Drive and save it to a temporary location
|
| 49 |
def download_google_drive_file(file_id, file_name):
|
| 50 |
request = drive_service.files().get_media(fileId=file_id)
|
| 51 |
+
file_path = os.path.join(temp_dir, file_name)
|
| 52 |
with open(file_path, 'wb') as f:
|
| 53 |
downloader = MediaIoBaseDownload(f, request)
|
| 54 |
done = False
|
|
|
|
| 58 |
print(f"Download progress: {int(status.progress() * 100)}%")
|
| 59 |
return file_path
|
| 60 |
|
| 61 |
+
# Function to save all files linked to records from the Salesforce custom object
|
| 62 |
def save_salesforce_files(custom_object_api_name):
|
| 63 |
files = get_all_salesforce_files(custom_object_api_name)
|
| 64 |
for file in files:
|