import dropbox import dropbox.exceptions as AuthError import cv2 import pandas as pd refresh_token='cw3B-1islHAAAAAAAAAAAfeF5wMByriKqKR2fPNLRWLQaxlF1ZX01KAxCBsBmihM' ################################################################################################################### def dropbox_connect(): """Create a connection to Dropbox.""" try: dbx = dropbox.Dropbox( app_key='cujlb5a2hoesvpy', app_secret='eb8khe4fuqda1kh', oauth2_refresh_token = refresh_token ) except AuthError as e: print('Error connecting to Dropbox with access token: ' + str(e)) return dbx ################################################################################################################ def dropbox_list_files(path): """Return a Pandas dataframe of files in a given Dropbox folder path in the Apps directory. """ dbx = dropbox_connect() try: files = dbx.files_list_folder(path).entries files_list = [] for file in files: if isinstance(file, dropbox.files.FileMetadata): metadata = { 'name': file.name, 'path_display': file.path_display, 'client_modified': file.client_modified, 'server_modified': file.server_modified } files_list.append(metadata) df = pd.DataFrame.from_records(files_list) return df.sort_values(by='server_modified', ascending=False) except Exception as e: print('Error getting list of files from Dropbox: ' + str(e)) ##################################################################################################################### def dropbox_download_file(dropbox_file_path, local_file_path): """Download a file from Dropbox to the local machine.""" try: dbx = dropbox_connect() with open(local_file_path, 'wb') as f: metadata, result = dbx.files_download(path=dropbox_file_path) f.write(result.content) except Exception as e: print('Error downloading file from Dropbox: ' + str(e)) ######################################################################################################################### def dropbox_upload_file(local_path, local_file, dropbox_file_path): """Upload a file from the local machine to a path in the Dropbox app directory. Args: local_path (str): The path to the local file. local_file (str): The name of the local file. dropbox_file_path (str): The path to the file in the Dropbox app directory. Example: dropbox_upload_file('.', 'test.csv', '/stuff/test.csv') Returns: meta: The Dropbox file metadata. """ try: dbx = dropbox_connect() #local_file_path = pathlib.Path(local_path) / local_file if isinstance(local_file, pd.DataFrame): file=local_file.to_xml(index=False).encode() # file=local_file.to_csv(index=False).encode() else: success,encoded=cv2.imencode('.png',local_file) file=encoded.tobytes() # with local_file_path.open("rb") as f: meta = dbx.files_upload(file, dropbox_file_path, mode=dropbox.files.WriteMode("overwrite")) return meta except Exception as e: print('Error uploading file to Dropbox: ' + str(e))