Spaces:
Sleeping
Sleeping
Update app.py
#1
by
hardik27
- opened
app.py
CHANGED
|
@@ -20,9 +20,9 @@ def get_image_as_base64(image_path):
|
|
| 20 |
# Load credentials from environment variables
|
| 21 |
SERVICE_ACCOUNT_INFO = {
|
| 22 |
"type": "service_account",
|
| 23 |
-
"project_id": os.environ.get("project_id"),
|
| 24 |
"private_key_id": os.environ.get("private_key_id"),
|
| 25 |
-
|
| 26 |
"client_email": os.environ.get("client_email"),
|
| 27 |
"client_id": os.environ.get("client_id"),
|
| 28 |
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
|
@@ -32,7 +32,6 @@ SERVICE_ACCOUNT_INFO = {
|
|
| 32 |
"universe_domain": "googleapis.com"
|
| 33 |
}
|
| 34 |
|
| 35 |
-
|
| 36 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
| 37 |
|
| 38 |
|
|
@@ -183,43 +182,49 @@ def save_mapping_file_to_drive():
|
|
| 183 |
for file in files:
|
| 184 |
# Get the ID and name of the first Excel file found in the folder
|
| 185 |
existing_file_id = file['id']
|
| 186 |
-
existing_file_name = file['name']
|
| 187 |
|
| 188 |
-
#
|
| 189 |
-
|
|
|
|
| 190 |
|
| 191 |
-
file_metadata = {'name': MAPPING_FILENAME }
|
| 192 |
-
media = MediaFileUpload(MAPPING_FILENAME, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
| 193 |
-
service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
# service =
|
| 198 |
-
# creds = Credentials.from_authorized_user_info(credentials_dict)
|
| 199 |
-
# Authenticate with Google Drive API
|
| 200 |
-
service = build('drive', 'v3', credentials=creds)
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
def delete_master_file():
|
| 225 |
creds = authenticate()
|
|
|
|
| 20 |
# Load credentials from environment variables
|
| 21 |
SERVICE_ACCOUNT_INFO = {
|
| 22 |
"type": "service_account",
|
| 23 |
+
"project_id": os.environ.get("project_id",),
|
| 24 |
"private_key_id": os.environ.get("private_key_id"),
|
| 25 |
+
"private_key": os.environ.get("private_key"),
|
| 26 |
"client_email": os.environ.get("client_email"),
|
| 27 |
"client_id": os.environ.get("client_id"),
|
| 28 |
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
|
|
|
| 32 |
"universe_domain": "googleapis.com"
|
| 33 |
}
|
| 34 |
|
|
|
|
| 35 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
| 36 |
|
| 37 |
|
|
|
|
| 182 |
for file in files:
|
| 183 |
# Get the ID and name of the first Excel file found in the folder
|
| 184 |
existing_file_id = file['id']
|
|
|
|
| 185 |
|
| 186 |
+
# Overwrite the existing file
|
| 187 |
+
media = MediaFileUpload(MAPPING_FILENAME, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
| 188 |
+
service.files().update(fileId=existing_file_id,media_body=media).execute()
|
| 189 |
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
# file_metadata = {'name': MAPPING_FILENAME }
|
| 192 |
+
# media = MediaFileUpload(MAPPING_FILENAME, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
| 193 |
+
# service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
+
def pull_mapping_file_from_drive():
|
| 196 |
+
try:
|
| 197 |
+
creds = authenticate()
|
| 198 |
+
# service = build('drive', 'v3', credentials=creds)
|
| 199 |
+
# creds = Credentials.from_authorized_user_info(credentials_dict)
|
| 200 |
+
# Authenticate with Google Drive API
|
| 201 |
+
service = build('drive', 'v3', credentials=creds)
|
| 202 |
+
|
| 203 |
+
results = service.files().list(
|
| 204 |
+
q="mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'",
|
| 205 |
+
fields="files(id, name)").execute()
|
| 206 |
+
|
| 207 |
+
files = results.get('files', [])
|
| 208 |
+
files = [i for i in files if i.get('name')==MAPPING_FILENAME]
|
| 209 |
+
if files:
|
| 210 |
+
file_id = files[0]['id']
|
| 211 |
+
file_name = files[0]['name']
|
| 212 |
+
request = service.files().get_media(fileId=file_id)
|
| 213 |
+
fh = open(file_name, 'wb')
|
| 214 |
+
downloader = MediaIoBaseDownload(fh, request)
|
| 215 |
+
|
| 216 |
+
# Execute the download
|
| 217 |
+
done = False
|
| 218 |
+
while not done:
|
| 219 |
+
status, done = downloader.next_chunk()
|
| 220 |
+
|
| 221 |
+
fh.close()
|
| 222 |
+
return 1
|
| 223 |
+
|
| 224 |
+
return 0
|
| 225 |
+
except Exception as e:
|
| 226 |
+
print(e)
|
| 227 |
+
return 0
|
| 228 |
|
| 229 |
def delete_master_file():
|
| 230 |
creds = authenticate()
|