File size: 3,442 Bytes
6629df8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d702a65
15d56b7
6629df8
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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))