Skip to main content

Yet Another Google Drive API Python wrapper

Project description

yagdrive-logo

Yet Another Google Drive API Python wrapper!

yagdrive is a Python package based on PyDrive which aims to simplify and enhance the operations over the Google Drive API.

Installation

$ pip install yagdrive

Auth

In order to get the proper authorization to your Google Drive, it's necessary to perform a couple of steps:

I. Get client secrets

  1. Go to APIs Console and create a new project (or reuse an existing one).
  2. Go to APIs Dashboard inside your project and click on "Enable API and Services". Search for the "Google Drive API" and activate it.
  3. Go to APIS Credentials on the left panel and click on "Create Credentials". Select "OAuth client ID". Then select "Desktop application" and give a name.
  4. Click on "Download JSON", save the file as client_secrets.json and place it on the current work directory.

II. Get credentials

Having client_secrets.json in the current work directory (as explained at previous section), just run:

$ python -c "from yagdrive import auth; auth.get_credentials()"

A browser is launched and you'll have to authorize the access to Google Drive. Follow the instructions. Note that enabling Less secure apps might be required.

If everything was fine, you'll get two files on the current work directory:

  • gdrive-credentials.json: authorization token next to other relevant fields.
  • gdrive-secrets.json: renamed file from client-secrets.json.

🎒  These two files are required for yagmail to properly work.

⚠️️  You shouldn't share these files or include them in a public respository.

Usage

First of all you must create the handler:

>>> from yagdrive import GDrive

>>> drive = GDrive()

You can pass two arguments to GDrive():

  • credentials_file (default: 'gdrive-credentials.json')
  • secrets_file (default: 'gdrive-secrets.json')

Upload a file

>>> drive.put('/home/yagdrive/hello.json')

Boom easy

put() method returns an instance of pydrive.files.GoogleDriveFile. Many fields are available in that object as a parsed json. For instance you could get the download link:

>>> f = drive.put('/home/yagdrive/hello.json')

>>> f['webContentLink']
'https://drive.google.com/uc?id=2QFfbG1IjBnAndp6gwZD7nQixOlup1Zfm&export=download'

# check f.keys() for the whole list of fields
See more use cases:
# Set a custom title for the uploaded file
>>> drive.put('/home/yagdrive/hello.json', title='Say hi to the world')
# Do not overwrite files with the same name
>>> drive.put('/home/yagdrive/hello.json', overwrite=False)

⚠️️  By default, put() uploads the file to the root of Google Drive. This behaviour can be changed using cd() method.

Change directory

You can change the remote work directory (for yagmail) on Google Drive using the following:

>>> drive.cd('IOx1Q2ZuQb7ZfGjdmwfnD6Fig1pBlnAnp)
'python-scripts'

The only argument is the id of the folder. It returns the title of the folder.

Present work directory

You can check the present work directory using the following code:

>>> drive.pwd
'python-scripts'

Download a file

You can download a file using its identifier:

>>> local_file, remote_file = drive.get_by_id('2QFfbG1IjBnAndp6gwZD7nQixOlup1Zfm')

>>> local_file
PosixPath('hello.json')

>>> type(remote_file)
pydrive.files.GoogleDriveFile

Or you can even download a file using its title:

>>> local_file, remote_file = drive.get_by_title('hello.json')

>>> local_file
PosixPath('hello.json')

>>> type(remote_file)
pydrive.files.GoogleDriveFile

Delete a file

You can delete a file using its identifier:

>>> drive.del_by_id('2QFfbG1IjBnAndp6gwZD7nQixOlup1Zfm')

Or you can even delete a file using its title:

>>> drive.del_by_title('hello.json')

⚠️️  These methods do not move the files to the trash; they definitely removes the file.

List contents of a folder

You can list the files (and directories) within a remote folder on Google Drive as follows:

>>> for file in drive.ls():
...     print(file['title'], '|', file['id'])

Hello world | lV16YGlUV52eapO2QYlmLNQw0pRkvMJLQGIsWtTpk1VxayZtKTZe10W2RtEXiB655XVnMHfO
Salut monde | MVZ1T51mt22p2LiTRGltBnkeQ0YvQlVJfeHYw1GWZREVKOs0OVIaatLkpQpx566XUyNlMW5X
Ahoj světe | JKVM5tpRYtkpGHwY60fOOatak1LTRVWTpZQV5l1y0MlxUWLm5V2Z2eEGe1NQXn6lXIQsi2vB
Ciao mondo | ykZ62R21QMiLsMavYRXfet0OVVtmVWknVI5X5llpKJGQH65YaGTQNwW1l21xBEp0TOtpLUeZ

⚠️️  ls() returns a generator. Keep that in mind!

Changelog

Check the Changelog for bug fixes, enhancements and features.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yagdrive-1.0.0.tar.gz (4.7 kB view hashes)

Uploaded Source

Built Distribution

yagdrive-1.0.0-py3-none-any.whl (5.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page