Git Product home page Git Product logo

dropbox-pytools's Introduction

Dropbox-PyTools

Helpful functions that leverage the Dropbox API v2 (HTTP endpoints). The main goal of this project was to download all users Dropbox data via the API. However, I've added tools for performing bulk restores of deleted files. I will continue to add other functionality as needed.

Get all Dropbox users
List all the contents of a users account with paignation (If needed)
Check if a list of files have been deleted
Download all the content from a users account
Download all the content for every user account (From function or CSV)
Restore all files specified in a CSV

Functions return JSON

Any tips or advice are much appreciated.

How to get started with Python and the Dropbox REST API

The first thing you will need to do is generate your API key. There are basically several methods to do this, one is to generate a personal key and the latter is to generate a buisness key. It just depends on what your end goal is. I will be using the buisness key.

alt text

The Dropbox REST API documentation examples are in curl. We can use this very helpful site to convert these examples in to Python requests.

Lets look at the below function which gets 1000 users and does not include removed users.

import requests

# API token from https://www.dropbox.com/developers/apps
# (I'm using a Dropbox Business API with Team member file access)
token = 'Your Key'

# Endpoint documentation https://www.dropbox.com/developers/documentation/http/teams#team-members-list

def get_all_users():
    headers = {
        'Authorization': 'Bearer {}'.format(token),
        'Content-Type': 'application/json',
    }

    data = '{"limit": 1000,"include_removed": false}'

    response = requests.post('https://api.dropboxapi.com/2/team/members/list', headers=headers, data=data)
    json = response.json()
    return json['members']

get_all_users() # Run the above function

This function returns a dictonary of members which we then can iterate over to expose other details. Keep in mind that if you have more users than the current set limit "1000", you will need to paginate.

users = get_all_users()

for user in users:
  print(user['dbmid'])

Here is an example in which pagination was needed. You will need to pass the dbmid in order for this function to work. The json['cursor'] value lets us know if we need to keep calling the list_folder/continue endpoint to get more data.

import requests

# API token from https://www.dropbox.com/developers/apps
# (I'm using a Dropbox Business API with Team member file access)
token = 'Your Key'

def get_user_files(dbmid):
    alluserfiles = []	
	
    headers = {
        'Authorization': 'Bearer {}'.format(token),
        'Content-Type': 'application/json; charset=utf-8',
        'Dropbox-API-Select-User': dbmid
    }

    data = '{"path": "","recursive": true,"include_media_info": false,"include_deleted": false,"include_has_explicit_shared_members": false,"include_mounted_folders": true}'

    response = requests.post('https://api.dropboxapi.com/2/files/list_folder', headers=headers, data=data)
    json = response.json()

    for i in json['entries']:
        alluserfiles.append(i)
    
    while json['has_more']:

                cursorline = json['cursor']
                cursor = '{"cursor": "' + cursorline + '"}'
                data = '{}'.format(cursor)

                headers = {
                    'Authorization': 'Bearer {}'.format(token),
                    'Content-Type': 'application/json; charset=utf-8',
                    'Dropbox-API-Select-User': dbmid
                }
            
                response = requests.post('https://api.dropboxapi.com/2/files/list_folder/continue', headers=headers, data=data)
                
                if response.status_code == requests.codes.ok: # Keep trying to process the results of the response variable
                # until we are successfull
                    print('Status was okay. {}'.format(response.status_code))
                    
                    json = response.json()
                    for i in json['entries']:
                        alluserfiles.append(i)
    return(alluserfiles)

user_files = get_user_files(dbmid)

dropbox-pytools's People

Contributors

time4116 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.