Spaces:
Sleeping
Sleeping
Update function/dropbox.py
Browse files- function/dropbox.py +12 -13
function/dropbox.py
CHANGED
@@ -1,22 +1,17 @@
|
|
1 |
import dropbox.files
|
2 |
import os
|
3 |
import shutil
|
4 |
-
import requests,
|
5 |
from fastapi import HTTPException
|
6 |
-
from dotenv import load_dotenv
|
7 |
-
import os
|
8 |
-
load_dotenv()
|
9 |
-
DROPBOX_APP_KEY=os.getenv('DROPBOX_APP_KEY')
|
10 |
-
DROPBOX_APP_SECRET=os.getenv('DROPBOX_APP_SECRET')
|
11 |
-
DROPBOX_REFRESH_TOKEN=os.getenv('DROPBOX_REFRESH_TOKEN')
|
12 |
|
13 |
def refresh_token_dropbox():
|
14 |
-
app_key =
|
15 |
-
app_secret =
|
16 |
-
refresh_token =
|
17 |
url = 'https://api.dropbox.com/oauth2/token'
|
18 |
auth_string = f"{app_key}:{app_secret}"
|
19 |
base64authorization = base64.b64encode(auth_string.encode()).decode('utf-8')
|
|
|
20 |
headers = {
|
21 |
'Authorization': f'Basic {base64authorization}',
|
22 |
'Content-Type': 'application/x-www-form-urlencoded'
|
@@ -90,8 +85,9 @@ def download_folder(id):
|
|
90 |
try:
|
91 |
TOKEN = refresh_token_dropbox()
|
92 |
dbx = dropbox.Dropbox(TOKEN)
|
93 |
-
local_path = f"/code/
|
94 |
os.makedirs(local_path, exist_ok=True)
|
|
|
95 |
result = dbx.files_list_folder(f"/{id}")
|
96 |
for entry in result.entries:
|
97 |
if isinstance(entry, dropbox.files.FileMetadata):
|
@@ -107,13 +103,14 @@ def download_file_id(file_name, id):
|
|
107 |
try:
|
108 |
TOKEN = refresh_token_dropbox()
|
109 |
dbx = dropbox.Dropbox(TOKEN)
|
110 |
-
local_folder_path = f"/code/
|
111 |
os.makedirs(local_folder_path, exist_ok=True)
|
112 |
local_file_path = os.path.join(local_folder_path, file_name)
|
113 |
with open(local_file_path, "wb") as f:
|
114 |
metadata, response = dbx.files_download(f"/{id}/{file_name}")
|
115 |
f.write(response.content)
|
116 |
print(f"Downloaded file '{file_name}' to '{local_file_path}'")
|
|
|
117 |
except dropbox.exceptions.ApiError as e:
|
118 |
print(f"Error downloading file '{file_name}': {e}")
|
119 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
@@ -128,13 +125,14 @@ def search_and_download_file(start_char, id):
|
|
128 |
print(f"No file found starting with '{start_char}' in folder '{id}'")
|
129 |
return
|
130 |
file_name = files_starting_with_char[0]
|
131 |
-
local_folder_path = f"/code/
|
132 |
os.makedirs(local_folder_path, exist_ok=True)
|
133 |
local_file_path = os.path.join(local_folder_path, file_name)
|
134 |
with open(local_file_path, "wb") as f:
|
135 |
metadata, response = dbx.files_download(f"/{id}/{file_name}")
|
136 |
f.write(response.content)
|
137 |
print(f"Downloaded file '{file_name}' to '{local_file_path}'")
|
|
|
138 |
except dropbox.exceptions.ApiError as e:
|
139 |
print(f"Error searching or downloading file: {e}")
|
140 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
@@ -149,6 +147,7 @@ def delete_all_files_in_folder(folder_id):
|
|
149 |
file_path = entry.path_display
|
150 |
dbx.files_delete_v2(file_path)
|
151 |
print(f"Deleted file '{file_path}'")
|
|
|
152 |
print(f"All files in folder '{folder_id}' have been deleted.")
|
153 |
except dropbox.exceptions.ApiError as e:
|
154 |
print(f"Error deleting files: {e}")
|
|
|
1 |
import dropbox.files
|
2 |
import os
|
3 |
import shutil
|
4 |
+
import requests,base64
|
5 |
from fastapi import HTTPException
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def refresh_token_dropbox():
|
8 |
+
app_key = "m79mo7gg7mh8pez"
|
9 |
+
app_secret = "by93yed7as4qlmo"
|
10 |
+
refresh_token = 'VG4SP0Ugo4EAAAAAAAAAAbHf7esmak_gieNGvLHxiiAXGfSeKPVXbX1lm28DMz_o'
|
11 |
url = 'https://api.dropbox.com/oauth2/token'
|
12 |
auth_string = f"{app_key}:{app_secret}"
|
13 |
base64authorization = base64.b64encode(auth_string.encode()).decode('utf-8')
|
14 |
+
|
15 |
headers = {
|
16 |
'Authorization': f'Basic {base64authorization}',
|
17 |
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
85 |
try:
|
86 |
TOKEN = refresh_token_dropbox()
|
87 |
dbx = dropbox.Dropbox(TOKEN)
|
88 |
+
local_path = f"/code/temp/{id}"
|
89 |
os.makedirs(local_path, exist_ok=True)
|
90 |
+
clear_local_folder(local_path)
|
91 |
result = dbx.files_list_folder(f"/{id}")
|
92 |
for entry in result.entries:
|
93 |
if isinstance(entry, dropbox.files.FileMetadata):
|
|
|
103 |
try:
|
104 |
TOKEN = refresh_token_dropbox()
|
105 |
dbx = dropbox.Dropbox(TOKEN)
|
106 |
+
local_folder_path = f"/code/temp/{id}"
|
107 |
os.makedirs(local_folder_path, exist_ok=True)
|
108 |
local_file_path = os.path.join(local_folder_path, file_name)
|
109 |
with open(local_file_path, "wb") as f:
|
110 |
metadata, response = dbx.files_download(f"/{id}/{file_name}")
|
111 |
f.write(response.content)
|
112 |
print(f"Downloaded file '{file_name}' to '{local_file_path}'")
|
113 |
+
|
114 |
except dropbox.exceptions.ApiError as e:
|
115 |
print(f"Error downloading file '{file_name}': {e}")
|
116 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
|
|
125 |
print(f"No file found starting with '{start_char}' in folder '{id}'")
|
126 |
return
|
127 |
file_name = files_starting_with_char[0]
|
128 |
+
local_folder_path = f"/code/temp/{id}"
|
129 |
os.makedirs(local_folder_path, exist_ok=True)
|
130 |
local_file_path = os.path.join(local_folder_path, file_name)
|
131 |
with open(local_file_path, "wb") as f:
|
132 |
metadata, response = dbx.files_download(f"/{id}/{file_name}")
|
133 |
f.write(response.content)
|
134 |
print(f"Downloaded file '{file_name}' to '{local_file_path}'")
|
135 |
+
|
136 |
except dropbox.exceptions.ApiError as e:
|
137 |
print(f"Error searching or downloading file: {e}")
|
138 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
|
|
147 |
file_path = entry.path_display
|
148 |
dbx.files_delete_v2(file_path)
|
149 |
print(f"Deleted file '{file_path}'")
|
150 |
+
|
151 |
print(f"All files in folder '{folder_id}' have been deleted.")
|
152 |
except dropbox.exceptions.ApiError as e:
|
153 |
print(f"Error deleting files: {e}")
|