Skip to content

Instantly share code, notes, and snippets.

@tinkerbotfoo
Last active December 25, 2015 09:49
Show Gist options
  • Save tinkerbotfoo/6956964 to your computer and use it in GitHub Desktop.
Save tinkerbotfoo/6956964 to your computer and use it in GitHub Desktop.
Fetch the list of all files in your dropbox account. This might be useful if you wanted to do something with the data. Recently only of my dropbox client that i installed on a virtual machine went wacky and started truncating files already stored in dropbox. I disabled the rogue client. However it has already done some damage. However dropbox st…
#!pip install dropbox pandas
# Register for dropbox developer account for this to work and get the auth access code as explained in the get_dropbox_auth_access_code.py
# Include the Dropbox SDK
import dropbox
import pandas as pd
# get the access token from the get_dropbox_auth_access_code.py - below code is fake
access_token = "9TskpJ1jKCwAAAAAAAAAAVTAzOoY1VWDMGjKCwAAAg-Wu3uoYfd39ZZDvBkc179"
#Instantiate the dropbox client
client = dropbox.client.DropboxClient(access_token)
print 'linked account: ', client.account_info()
# Dropbox provides the filelist in chunks of 2000, hence we need to send multiple request.
dlist=[]
do_i_fetch_again = True
while do_i_fetch_again:
if len(dlist) < 1:
delta = client.delta()
else:
delta = client.delta(delta['cursor'])
for i in range(0,len(delta['entries'])):
ddict = {}
ddict['path'] = delta['entries'][i][1]['path']
ddict['modified'] = delta['entries'][i][1]['modified']
ddict['root'] = delta['entries'][i][1]['root']
ddict['revision'] = delta['entries'][i][1]['revision']
ddict['bytes'] = delta['entries'][i][1]['bytes']
ddict['is_dir'] = delta['entries'][i][1]['is_dir']
ddict['size'] = delta['entries'][i][1]['size']
if delta['entries'][1][1].has_key('mime_type'):
ddict['mime_type'] = delta['entries'][1][1]['mime_type']
else:
ddict['mime_type'] = ""
dlist.append(ddict)
print len(dlist)
do_i_fetch_again = delta['has_more']
#Copying the file list into pandas dataframe for easy manipulation
df = pd.DataFrame(dlist)
#convert text datetime to pandas datetime field
df['mod_datetime'] = pd.to_datetime(df['modified'])
# Filtering for files that were modified since yesterday
df2 = df[df.mod_datetime > (pd.to_datetime(['10-11-2013 10:00']))[0]]
# I wanted to flag the files from a certain folder
df2['scan_status'] = map(lambda x: str(x).find('/vmdboxshared/c9/lgf/'), df2['path'])
df3 = df2[df2['scan_status'] < 0]
print df3
# Refer to pandas documentation if you want to download this list as a csv or excel
#Use this code to get the dropbox authorization access code which will be later used in the application
# Include the Dropbox SDK
import dropbox
# Get your app key and secret from the Dropbox developer website - below codes are fake
app_key = 'k1vwsdezefrjwpd'
app_secret = 'mnwerfpjpygwx1n'
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
# Have the user sign in and authorize this token
authorize_url = flow.start()
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code.'
code = raw_input("Enter the authorization code here: ").strip()
# This will fail if the user enters an invalid authorization code
access_token, user_id = flow.finish(code)
#Printing the access token, this is what you need in the above code
print access_token
#testing the access token
client = dropbox.client.DropboxClient(access_token)
print 'linked account: ', client.account_info()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment