Skip to content

Commit 62e7f23

Browse files
committed
First commit!
1 parent f958375 commit 62e7f23

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,36 @@ Dropbox-Folder-Size
22
===================
33

44
A small Python script that generates a .txt file of the sizes of your Dropbox folders. Perfect for checking the sizes of folders that you have selectively unsync-ed.
5+
6+
# Script Requirements
7+
===================
8+
9+
1. Python SDK
10+
Installation instructions [here](https://www.dropbox.com/developers/core/sdks/python).
11+
12+
2. Dropbox Access Token
13+
Still new to Dropbox development, so I'm not sure what the proper way of getting an access token is, but the way I did it was by:
14+
15+
1. Visiting [Dropbox's Developer site and going to App Console](https://www.dropbox.com/developers/apps).
16+
2. Create a Dropbox API app with "Files and Datastores", "No", "All file types" options.
17+
3. Get the app key, visit this [website](https://dbxoauth2.site44.com/) and it will generate an access token for you.
18+
19+
# Usage Instructions
20+
21+
Input your access token into the script.
22+
23+
The script accepts 2 variables:
24+
25+
1. Denominator (non-negative integer). 0 for bytes, 1 for KB, 2 for MB, 3 for GB, etc.
26+
27+
2. Levels (non-negative integer). Tells the script how far down the directory tree you want to look. E.g. if set to 1, the script will output the sizes of all your folders in the main Dropbox directory.
28+
29+
# Known Bugs
30+
31+
1. Dropbox still thinks .pages documents are folders. I might or might not get around to fixing this.
32+
33+
# Possible Features
34+
35+
1. Make this script a full-fledged app, with browser redirection for OAuth authentication and stuff.
36+
2. Add support to run script in sub-folders.
37+
3. Add a switch to un-ignore files that appear above the set level.

dropbox_folder_size.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import dropbox
2+
3+
client = dropbox.client.DropboxClient('<INSERT ACCESS TOKEN>')
4+
5+
# choose 0 to express values in bytes, 1 in KB, 2 in MB, 3 in GB.
6+
denominator = 2
7+
8+
# choose how many levels you want to segregate by
9+
# e.g. if levels is 1, will return folder sizes of folders in Dropbox root
10+
# If you just want to know overall usage of account (i.e. levels = 0), we will run a separate query for it
11+
levels = 2
12+
13+
if (levels == 0):
14+
quota_info = client.account_info()['quota_info']
15+
usage = quota_info['shared'] / (1024.0 ** denominator)
16+
quota = quota_info['quota'] / (1024.0 ** denominator)
17+
18+
output.write('You are currently using ' + str(usage) + ' out of ' + str(quota) + ' of space on Dropbox.')
19+
output.close()
20+
quit()
21+
22+
sizes = {}
23+
foldersizes = {}
24+
cursor = None
25+
26+
while cursor is None or result['has_more']:
27+
result = client.delta(cursor)
28+
for path, metadata in result['entries']:
29+
sizes[path] = metadata['bytes'] if metadata else 0
30+
cursor = result['cursor']
31+
32+
for path, size in sizes.items():
33+
34+
segments = path.split('/')
35+
36+
if (len(segments) > levels + 1):
37+
folder = '/'.join(segments[0:levels+1])
38+
else:
39+
folder = '/'.join(segments[0:len(segments)-1])
40+
try:
41+
foldersizes[folder] += size
42+
except KeyError:
43+
foldersizes[folder] = size
44+
45+
if (denominator != 0):
46+
for path,size in foldersizes.items():
47+
if size:
48+
foldersizes[path] = size / (1024.0 ** denominator)
49+
else:
50+
del foldersizes[path]
51+
52+
output = open('dropbox_folder_sizes.txt', 'w')
53+
54+
output.write('Below is a list of your largest Dropbox folders, ordered from largest to smallest. You chose a drill level of 2. Sizes are expressed as "%d", where 0 is in bytes, 1 is in KB, 2 is in MB, and 3 is in GB. \n' % denominator)
55+
56+
for folder in reversed(sorted(foldersizes.keys(), key=lambda x: foldersizes[x])):
57+
output.write('%s: %f' % (folder, foldersizes[folder]) + '\n')
58+
59+
output.close()
60+
quit()

0 commit comments

Comments
 (0)